Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nested objects returned checkResult, execution of async rules on arrays #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions src/ArrayType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ export class ArrayType<DataType = any, E = ErrorMessageType> extends MixedType<

of(type: MixedType<any, DataType, E>) {
super.pushRule({
onValid: (items, data, filedName) => {
onValid: (items, data, fieldName) => {
const checkResults = items.map((value, index) => {
const name = Array.isArray(filedName)
? [...filedName, `[${index}]`]
: [filedName, `[${index}]`];
const name = Array.isArray(fieldName)
? [...fieldName, `[${index}]`]
: [fieldName, `[${index}]`];

return type.check(value, data, name as string[]);
});
Expand All @@ -85,6 +85,29 @@ export class ArrayType<DataType = any, E = ErrorMessageType> extends MixedType<
}
});

super.pushRule({
onValid: (items, data, fieldName) => {
return new Promise(resolve => {
const checkAll: Promise<CheckResult<string | E, PlainObject<any>>>[] =
items.map((value, index) => {
const name = Array.isArray(fieldName)
? [...fieldName, `[${index}]`]
: [fieldName, `[${index}]`];

return type.checkAsync(value, data, name as string[]);
});

return Promise.all(checkAll).then(checkResults => {
resolve({
hasError: !!checkResults.find(item => item?.hasError),
array: checkResults
});
});
})
},
isAsync: true
});

return this;
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/ObjectType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ export class ObjectType<DataType = any, E = ErrorMessageType> extends MixedType<
});

return Promise.all(checkAll).then(values => {
values.forEach((v, index) => {
let hasError = false;
values.forEach((v: any, index: number) => {
if (v?.hasError) {
hasError = true;
}
checkResult[keys[index]] = v;
});

resolve({ object: checkResult });
resolve({ hasError, object: checkResult });
});
}

Expand Down
29 changes: 29 additions & 0 deletions test/ArrayTypeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,33 @@ describe('#ArrayType', () => {
.checkForField('data', { data: [1] })
.errorMessage.should.equal('data field must have at least 2 items');
});

it('Should call async check', done => {
const schema = new Schema({
arr1: ArrayType().addAsyncRule(() => {
return new Promise(resolve => {
setTimeout(() => {
resolve(false);
}, 1000);
});
}, 'error1'),
arr2: ArrayType().addAsyncRule(() => {
return new Promise(resolve => {
setTimeout(() => {
resolve(true);
}, 1000);
});
}, 'error2'),
});

schema.checkAsync({ arr1: [1, 2, 3], arr2: [1, 2, 3] }).then(status => {
if (
status.arr1.hasError &&
status.arr1.errorMessage === 'error1' &&
!status.arr2.hasError
) {
done();
}
});
});
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code added in ArrayType().of, but missing tests for it.

1 change: 1 addition & 0 deletions test/ObjectTypeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ describe('#ObjectType', () => {
schema.checkAsync({ url: 'url', user: { email: 'a', age: '10' } }).then(status => {
const user = status.user.object;
if (
status.user.hasError &&
user.age.hasError &&
user.age.errorMessage === 'error2' &&
user.email.hasError &&
Expand Down
Loading