Skip to content

Commit

Permalink
fix: introduced validate() input types
Browse files Browse the repository at this point in the history
  • Loading branch information
foxhound87 committed Jan 17, 2024
1 parent 415cc6a commit ed171fc
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {};

Expand Down Expand Up @@ -326,7 +326,7 @@ export default class Base implements BaseInterface {
Actions
*/

validate(opt: any = {}, obj: any = {}): Promise<any> {
validate(opt?: ValidateOptions, obj?: ValidateOptions): Promise<any> {
const $opt = _.merge(opt, { path: this.path });
return this.state.form.validator.validate($opt, obj);
}
Expand Down
11 changes: 6 additions & 5 deletions src/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import _ from "lodash";
import { $try } from "./utils";
import ValidatorInterface, {
DriversMap,
ValidateOptions,
ValidationPlugin,
ValidationPluginInterface,
ValidationPlugins,
Expand Down Expand Up @@ -53,11 +54,11 @@ export default class Validator implements ValidatorInterface {
);
}

validate(opt: any = {}, obj: any = {}): Promise<any> {
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<any> {
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;

Expand Down
20 changes: 13 additions & 7 deletions src/models/ValidatorInterface.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
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<any>[];
form: FormInterface;
drivers: any;
plugins: ValidationPlugins;
error: string | null;
initDrivers(): void;
validate(opt?: any, obj?: any): Promise<any>;
validateField(opt: {
showErrors: boolean,
related: boolean,
field: FieldInterface,
path: string,
}): void;
validate(opt?: ValidateOptions, obj?: ValidateOptions): Promise<any>;
validateField(opt: ValidateOptionsInterface): void;
validateRelatedFields(field: any, showErrors: boolean): void;
checkSVKValidationPlugin(): void;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/fixes.values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down Expand Up @@ -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()
})
Expand Down

0 comments on commit ed171fc

Please sign in to comment.