Skip to content

Commit

Permalink
feat: supports validate field without @Valid (#2)
Browse files Browse the repository at this point in the history
* feat: supports validate field without @Valid

* Apply suggestions from code review

Co-Authored-By: JounQin <admin@1stg.me>

* fix: parameter field's type

* chore: remove `any` cast


Co-authored-by: JounQin <admin@1stg.me>
  • Loading branch information
foreleven and JounQin committed Aug 27, 2019
1 parent e581734 commit 2b8ae8d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stated-form-bean",
"version": "0.1.0-beta.2",
"version": "0.1.0-beta.3",
"description": "",
"main": "dist/cjs",
"module": "dist/es",
Expand Down
23 changes: 13 additions & 10 deletions src/core/FormField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import get from 'lodash.get';
import set from 'lodash.set';

export class FormField<Values> {
private readonly _schema: yup.Schema<Values>;

private _errors: FormError<Values> = {};

private _touched: { [K in keyof Values]?: FormTouched<Values[K]> } = {};

constructor(schema: yup.Schema<Values>) {
this._schema = schema;
}
constructor(
public readonly field: string | symbol,
private readonly _schema?: yup.Schema<Values>,
) {}

get errors() {
return this._errors;
Expand All @@ -23,8 +22,12 @@ export class FormField<Values> {
return this._touched;
}

validate(data: Values): Promise<boolean> {
return this._schema
validate(data: Values, schema?: yup.Schema<Values>): Promise<boolean> {
const yupSchema = schema || this._schema;
if (yupSchema === undefined) {
throw new Error('miss yup schema for ' + String(this.field));
}
return yupSchema
.validate(data, {
abortEarly: false,
})
Expand All @@ -42,18 +45,18 @@ export class FormField<Values> {
this._errors = {};
}

hasError(path?: keyof Values & string): boolean {
hasError(path?: keyof Values): boolean {
if (path === undefined) {
return Object.keys(this._errors).length > 0;
}
return get(this._errors, path) !== undefined;
}

getError(path: keyof Values & string) {
getError(path: keyof Values) {
return get(this._errors, path);
}

hasTouched(path: keyof Values & string): boolean {
hasTouched(path: keyof Values): boolean {
return get(this._touched, path) !== undefined;
}

Expand Down
29 changes: 18 additions & 11 deletions src/core/FormModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,42 @@ export class FormModel<Values> {
field: T,
schema: yup.Schema<Values[T]>,
): void {
this[fields][field] = new FormField(schema);
this[fields][field] = new FormField(field as (string | symbol), schema);
}

getFormField<T extends keyof Values>(field: T): FormField<Values[T]> {
return this[fields][field];
}

validate<T extends keyof Values>(field: T): Promise<boolean> {
validate<T extends keyof Values>(
this: FormModel<Values> &
Values & {
[ForceUpdate]: (field: T) => void;
},
field: T,
schema?: yup.Schema<Values[T]>,
): Promise<boolean> {
if (this[fields][field] === undefined) {
this[fields][field] = new FormField(field as (string | symbol), schema);
}
const formField = this[fields][field];

const newLocal = (this as any) as Values;
return formField.validate(newLocal[field]).then(valid => {
if (Object.prototype.hasOwnProperty.call(newLocal, ForceUpdate)) {
(newLocal as any)[ForceUpdate](field);
const self = this;
return formField.validate(self[field], schema).then(valid => {
if (Object.prototype.hasOwnProperty.call(self, ForceUpdate)) {
self[ForceUpdate](field);
}
return valid;
});
}

[_clearErrors](field: keyof Values & (string | symbol)) {
[_clearErrors](field: keyof Values) {
if (this[fields] !== undefined && this[fields][field] !== undefined) {
this[fields][field]._clearErrors();
}
}

[_addError](
field: keyof Values & (string | symbol),
error: yup.ValidationError,
) {
[_addError](field: keyof Values, error: yup.ValidationError) {
const formField = this.getFormField(field);

formField._addError(error);
Expand Down
12 changes: 0 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1437,13 +1437,6 @@
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=

"@types/lodash-es@^4.17.3":
version "4.17.3"
resolved "https://registry.yarnpkg.com/@types/lodash-es/-/lodash-es-4.17.3.tgz#87eb0b3673b076b8ee655f1890260a136af09a2d"
integrity sha512-iHI0i7ZAL1qepz1Y7f3EKg/zUMDwDfTzitx+AlHhJJvXwenP682ZyGbgPSc5Ej3eEAKVbNWKFuwOadCj5vBbYQ==
dependencies:
"@types/lodash" "*"

"@types/lodash.get@^4.4.6":
version "4.4.6"
resolved "https://registry.yarnpkg.com/@types/lodash.get/-/lodash.get-4.4.6.tgz#0c7ac56243dae0f9f09ab6f75b29471e2e777240"
Expand Down Expand Up @@ -5974,11 +5967,6 @@ locate-path@^5.0.0:
dependencies:
p-locate "^4.1.0"

lodash-es@^4.17.15:
version "4.17.15"
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.15.tgz#21bd96839354412f23d7a10340e5eac6ee455d78"
integrity sha512-rlrc3yU3+JNOpZ9zj5pQtxnx2THmvRykwL4Xlxoa8I9lHBlVbbyPhgyPMioxVZ4NqyxaVVtaJnzsyOidQIhyyQ==

lodash._reinterpolate@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
Expand Down

0 comments on commit 2b8ae8d

Please sign in to comment.