Skip to content

Commit

Permalink
feat(yup): 新增 allowEmptyString
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Apr 11, 2022
1 parent 4e2affd commit 43b7c99
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/validator/__snapshots__/yup.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`yup allowEmptyString 正常 1`] = `
Object {
"data": "",
"error": [ValidationError: 此项必须是一个数字],
}
`;

exports[`yup allowEmptyString 正常 2`] = `
Object {
"data": "",
}
`;

exports[`yup number 支持 id() 1`] = `
Object {
"data": 1,
Expand Down
10 changes: 10 additions & 0 deletions src/validator/yup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,14 @@ describe('yup', () => {
)
expect(rule2.validatePlusSync(2)).toMatchSnapshot()
})

test('allowEmptyString 正常', () => {
const rule1 = yup.number()
// @ts-expect-error
expect(rule1.validatePlusSync('')).toMatchSnapshot()

const rule2 = yup.number().allowEmptyString()
// @ts-expect-error
expect(rule2.validatePlusSync('')).toMatchSnapshot()
})
})
7 changes: 7 additions & 0 deletions src/validator/yupSource/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ const proto = (SchemaType.prototype = {

isType(v) {
if (this._nullable && v === null) return true
if (this._allowEmptyString && v === '') return true
return !this._typeCheck || this._typeCheck(v)
},

Expand Down Expand Up @@ -413,6 +414,12 @@ const proto = (SchemaType.prototype = {
return next
},

allowEmptyString() {
var next = this.clone()
next._allowEmptyString = true
return next
},

transform(fn) {
var next = this.clone()
next.transforms.push(fn)
Expand Down
14 changes: 9 additions & 5 deletions src/validator/yupSource/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ export default function NumberSchema() {
this.transform(function (value) {
let parsed = value

if (typeof parsed === 'string') {
parsed = parsed.replace(/\s/g, '')
if (parsed === '') return NaN
// don't use parseFloat to avoid positives on alpha-numeric strings
parsed = +parsed
if (this._allowEmptyString && parsed === '') {
parsed = ''
} else {
if (typeof parsed === 'string') {
parsed = parsed.replace(/\s/g, '')
if (parsed === '') return NaN
// don't use parseFloat to avoid positives on alpha-numeric strings
parsed = +parsed
}
}

if (this.isType(parsed)) return parsed
Expand Down
2 changes: 2 additions & 0 deletions src/validator/yupTypes/mixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ export interface MixedSchema<T = any> {

nullable(isNullable?: boolean): this

allowEmptyString(): this

required(message?: MixedLocale['required']): this

notRequired(): this
Expand Down

0 comments on commit 43b7c99

Please sign in to comment.