Skip to content

Commit

Permalink
feat(yup): test 第一个参数可传函数或正则并更新类型定义
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Sep 27, 2021
1 parent 48babb6 commit d68da3c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
29 changes: 29 additions & 0 deletions src/validator/__snapshots__/yup.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`yup test 第一个参数可传函数或正则 1`] = `
Object {
"data": Object {
"name": "de",
"pass": "dss",
},
"error": [ValidationError: 姓名应该包含f],
}
`;

exports[`yup test 第一个参数可传函数或正则 2`] = `
Object {
"data": Object {
"name": "defl",
"pass": "dss",
},
"error": [ValidationError: 密码长度应大于10],
}
`;

exports[`yup test 第一个参数可传函数或正则 3`] = `
Object {
"data": Object {
"name": "defl",
"pass": "dssdllsal;;",
},
}
`;

exports[`yup validateInOrder 正常: 姓名必填 1`] = `[ValidationError: 姓名必填]`;

exports[`yup validateInOrder 正常: 密码有误 1`] = `[ValidationError: 密码必须大于或等于100]`;
Expand Down
29 changes: 29 additions & 0 deletions src/validator/yup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,33 @@ describe('yup', () => {
}
>()
})

test('test 第一个参数可传函数或正则', () => {
const rule = yup.object({
name: yup.string().label('姓名').required().test(/f/, '姓名应该包含f'),
pass: yup
.string()
.label('密码')
.required()
.test(value => value.length > 10, '密码长度应大于10'),
})
expect(
rule.validatePlusSync({
name: 'de',
pass: 'dss',
}),
).toMatchSnapshot()
expect(
rule.validatePlusSync({
name: 'defl',
pass: 'dss',
}),
).toMatchSnapshot()
expect(
rule.validatePlusSync({
name: 'defl',
pass: 'dssdllsal;;',
}),
).toMatchSnapshot()
})
})
24 changes: 16 additions & 8 deletions src/validator/yupSource/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,16 +435,24 @@ const proto = (SchemaType.prototype = {
test(...args) {
let opts

if (args.length === 1) {
if (typeof args[0] === 'function') {
opts = { test: args[0] }
if (typeof args[0] === 'function') {
opts = { test: args[0], message: args[1] }
} else if (args[0] instanceof RegExp) {
opts = { test: value => args[0].test(value), message: args[1] }
}

if (!opts) {
if (args.length === 1) {
if (typeof args[0] === 'function') {
opts = { test: args[0] }
} else {
opts = args[0]
}
} else if (args.length === 2) {
opts = { name: args[0], test: args[1] }
} else {
opts = args[0]
opts = { name: args[0], message: args[1], test: args[2] }
}
} else if (args.length === 2) {
opts = { name: args[0], test: args[1] }
} else {
opts = { name: args[0], message: args[1], test: args[2] }
}

if (opts.message === undefined) opts.message = locale.default
Expand Down
5 changes: 2 additions & 3 deletions src/validator/yupTypes/mixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,8 @@ declare module 'yup/es' {
when(builder: (value: T, schema: this) => this): this

test(
name: SchemaTestOptions<this, T>['name'],
message: SchemaTestOptions<this, T>['message'],
test: SchemaTestOptions<this, T>['test'],
test: SchemaTestOptions<this, T>['test'] | RegExp,
message?: SchemaTestOptions<this, T>['message'],
): this

test<TParams = {}>(options: SchemaTestOptions<this, T, TParams>): this
Expand Down

0 comments on commit d68da3c

Please sign in to comment.