From ed171fc24c79551197755406a56a7c79177a8363 Mon Sep 17 00:00:00 2001 From: foxhound87 Date: Wed, 17 Jan 2024 18:25:17 +0100 Subject: [PATCH] fix: introduced `validate()` input types --- CHANGELOG.md | 4 ++++ src/Base.ts | 4 ++-- src/Validator.ts | 11 ++++++----- src/models/ValidatorInterface.ts | 20 +++++++++++++------- tests/fixes.values.ts | 4 ++-- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cbacaca..90aaedff 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 6.9.1 (master) +- introduced `validate()` input types. +- introduced `ValidateOptions` & `ValidateOptionsInterface` models. + # 6.9.0 (master) - Validator types improvements. - introduced `ValidationPlugin`, `ExtendPlugin` and `ValidationPluginConfig` models. diff --git a/src/Base.ts b/src/Base.ts index a9934d41..8c9228ed 100755 --- a/src/Base.ts +++ b/src/Base.ts @@ -39,7 +39,7 @@ import { } from "./parser"; import { AllowedFieldPropsTypes, FieldPropsEnum, SeparatedPropsMode } from "./models/FieldProps"; import { OptionsEnum } from "./models/OptionsModel"; -import { ValidationHooks } from "./models/ValidatorInterface"; +import { ValidateOptions, ValidationHooks } from "./models/ValidatorInterface"; export default class Base implements BaseInterface { noop = () => {}; @@ -326,7 +326,7 @@ export default class Base implements BaseInterface { Actions */ - validate(opt: any = {}, obj: any = {}): Promise { + validate(opt?: ValidateOptions, obj?: ValidateOptions): Promise { const $opt = _.merge(opt, { path: this.path }); return this.state.form.validator.validate($opt, obj); } diff --git a/src/Validator.ts b/src/Validator.ts index 4974bd02..4a3ca836 100755 --- a/src/Validator.ts +++ b/src/Validator.ts @@ -3,6 +3,7 @@ import _ from "lodash"; import { $try } from "./utils"; import ValidatorInterface, { DriversMap, + ValidateOptions, ValidationPlugin, ValidationPluginInterface, ValidationPlugins, @@ -53,11 +54,11 @@ export default class Validator implements ValidatorInterface { ); } - validate(opt: any = {}, obj: any = {}): Promise { - const path: string = $try(opt.path, opt); - const instance = $try(opt.field, this.form.select(path, null, false), this.form); - const related: boolean = $try(opt.related, obj.related, true); - const showErrors: boolean = $try(opt.showErrors, obj.showErrors, false); + validate(opt: ValidateOptions, obj: ValidateOptions): Promise { + const path: string = $try((opt as any)?.path, opt); + const instance = $try((opt as any)?.field, this.form.select(path, null, false), this.form); + const related: boolean = $try((opt as any)?.related, (obj as any)?.related, true); + const showErrors: boolean = $try((opt as any)?.showErrors, (obj as any)?.showErrors, false); instance.$validating = true; instance.$validated += 1; diff --git a/src/models/ValidatorInterface.ts b/src/models/ValidatorInterface.ts index f835a479..95d417c8 100644 --- a/src/models/ValidatorInterface.ts +++ b/src/models/ValidatorInterface.ts @@ -1,7 +1,18 @@ +import Form from "../Form"; +import Field from "../Field"; import {FieldInterface} from "./FieldInterface"; import {FormInterface} from "./FormInterface"; import {StateInterface} from "./StateInterface"; +export interface ValidateOptionsInterface { + showErrors?: boolean, + related?: boolean, + field?: FieldInterface, + path?: string, +} + +export type ValidateOptions = string | ValidateOptionsInterface | Form | Field; + export interface ValidatorInterface { promises: Promise[]; form: FormInterface; @@ -9,13 +20,8 @@ export interface ValidatorInterface { plugins: ValidationPlugins; error: string | null; initDrivers(): void; - validate(opt?: any, obj?: any): Promise; - validateField(opt: { - showErrors: boolean, - related: boolean, - field: FieldInterface, - path: string, - }): void; + validate(opt?: ValidateOptions, obj?: ValidateOptions): Promise; + validateField(opt: ValidateOptionsInterface): void; validateRelatedFields(field: any, showErrors: boolean): void; checkSVKValidationPlugin(): void; } diff --git a/tests/fixes.values.ts b/tests/fixes.values.ts index 7d24a3f7..f0154773 100755 --- a/tests/fixes.values.ts +++ b/tests/fixes.values.ts @@ -706,7 +706,7 @@ describe('#611 validated with value\'s nested property', () => { const $form = new Form({fields, values, rules, validatedWith, related}, { plugins, name: 'form'}) - $form.validate({showError: true}).then(({isValid}) => { + $form.validate({ showErrors: true}).then(({isValid}) => { expect(isValid).to.be.false done() }) @@ -743,7 +743,7 @@ describe('#611 validated with value\'s nested property', () => { const $form = new Form({fields, values, rules, validatedWith, related}, { plugins, name: 'form'}) - $form.validate({showError: true}).then(({isValid}) => { + $form.validate({ showErrors: true }).then(({isValid}) => { expect(isValid).to.be.true done() })