Skip to content

Commit

Permalink
fix(validateInOrder, validateInOrderSync): 支持未定义的键
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Aug 17, 2020
1 parent 01a7a10 commit 7296494
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/validator/__snapshots__/yup.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ Object {
}
`;

exports[`yup validatePlus 正常 4`] = `
Object {
"data": Object {
"name": "8",
"pass": 101,
"xxx": "sss",
},
}
`;

exports[`yup validatePlusSync 正常 1`] = `
Object {
"data": Object {
Expand Down Expand Up @@ -65,3 +75,13 @@ Object {
},
}
`;

exports[`yup validatePlusSync 正常 4`] = `
Object {
"data": Object {
"name": "8",
"pass": 101,
"xxx": "sss",
},
}
`;
16 changes: 16 additions & 0 deletions src/validator/yup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ describe('yup', () => {
pass: 101,
}),
).toMatchSnapshot()
expect(
await rule.validatePlus({
name: '8',
pass: 101,
// @ts-ignore
xxx: 'sss',
}),
).toMatchSnapshot()
})

test('validatePlusSync 正常', async () => {
Expand All @@ -144,5 +152,13 @@ describe('yup', () => {
pass: 101,
}),
).toMatchSnapshot()
expect(
rule.validatePlusSync({
name: '8',
pass: 101,
// @ts-ignore
xxx: 'sss',
}),
).toMatchSnapshot()
})
})
16 changes: 14 additions & 2 deletions src/validator/yup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ yup.addMethod(yup.object, 'validateInOrder', function (
) {
return Object.keys(data)
.reduce((prev, key) => {
return prev.then(() => this.validateAt(key, data, options))
return prev.then(() => {
let schema: yup.MixedSchema | undefined
try {
schema = yup.reach(this as any, key)
} catch (e) {}
return schema ? this.validateAt(key, data, options) : undefined
})
}, Promise.resolve())
.then(() => this.cast(data)) as any
})
Expand All @@ -58,7 +64,13 @@ yup.addMethod(yup.object, 'validateInOrderSync', function (
options: any,
) {
for (const key of Object.keys(data)) {
this.validateSyncAt(key, data, options)
let schema: yup.MixedSchema | undefined
try {
schema = yup.reach(this as any, key)
} catch (e) {}
if (schema) {
this.validateSyncAt(key, data, options)
}
}
return this.cast(data)
})
Expand Down

0 comments on commit 7296494

Please sign in to comment.