Skip to content

Commit

Permalink
feat(validator): number 新增 positiveInteger, negativeInteger, nonposit…
Browse files Browse the repository at this point in the history
…iveInteger, nonnegativeInteger
  • Loading branch information
fjc0k committed Nov 11, 2021
1 parent 7937f43 commit 56c0abf
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 0 deletions.
181 changes: 181 additions & 0 deletions src/validator/__snapshots__/yup.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,187 @@ Object {
}
`;

exports[`yup number 支持 negativeInteger() 1`] = `
Object {
"data": 1,
"error": [ValidationError: 此项必须是一个负整数],
}
`;

exports[`yup number 支持 negativeInteger() 2`] = `
Object {
"data": -1,
}
`;

exports[`yup number 支持 negativeInteger() 3`] = `
Object {
"data": -0,
"error": [ValidationError: 此项必须是一个负整数],
}
`;

exports[`yup number 支持 negativeInteger() 4`] = `
Object {
"data": 0.5,
"error": [ValidationError: 此项必须是一个负整数],
}
`;

exports[`yup number 支持 negativeInteger() 5`] = `
Object {
"data": 1.5,
"error": [ValidationError: 此项必须是一个负整数],
}
`;

exports[`yup number 支持 negativeInteger() 6`] = `
Object {
"data": 100,
"error": [ValidationError: 此项必须是一个负整数],
}
`;

exports[`yup number 支持 negativeInteger() 7`] = `
Object {
"data": -100.3,
"error": [ValidationError: 此项必须是一个负整数],
}
`;

exports[`yup number 支持 nonnegativeInteger() 1`] = `
Object {
"data": 1,
}
`;

exports[`yup number 支持 nonnegativeInteger() 2`] = `
Object {
"data": -1,
"error": [ValidationError: 此项必须是一个非负整数],
}
`;

exports[`yup number 支持 nonnegativeInteger() 3`] = `
Object {
"data": -0,
}
`;

exports[`yup number 支持 nonnegativeInteger() 4`] = `
Object {
"data": 0.5,
"error": [ValidationError: 此项必须是一个非负整数],
}
`;

exports[`yup number 支持 nonnegativeInteger() 5`] = `
Object {
"data": 1.5,
"error": [ValidationError: 此项必须是一个非负整数],
}
`;

exports[`yup number 支持 nonnegativeInteger() 6`] = `
Object {
"data": 100,
}
`;

exports[`yup number 支持 nonnegativeInteger() 7`] = `
Object {
"data": -100.3,
"error": [ValidationError: 此项必须是一个非负整数],
}
`;

exports[`yup number 支持 nonpositiveInteger() 1`] = `
Object {
"data": 1,
"error": [ValidationError: 此项必须是一个非正整数],
}
`;

exports[`yup number 支持 nonpositiveInteger() 2`] = `
Object {
"data": -1,
}
`;

exports[`yup number 支持 nonpositiveInteger() 3`] = `
Object {
"data": -0,
}
`;

exports[`yup number 支持 nonpositiveInteger() 4`] = `
Object {
"data": 0.5,
"error": [ValidationError: 此项必须是一个非正整数],
}
`;

exports[`yup number 支持 nonpositiveInteger() 5`] = `
Object {
"data": 1.5,
"error": [ValidationError: 此项必须是一个非正整数],
}
`;

exports[`yup number 支持 nonpositiveInteger() 6`] = `
Object {
"data": 100,
"error": [ValidationError: 此项必须是一个非正整数],
}
`;

exports[`yup number 支持 nonpositiveInteger() 7`] = `
Object {
"data": -100.3,
"error": [ValidationError: 此项必须是一个非正整数],
}
`;

exports[`yup number 支持 positiveInteger() 1`] = `
Object {
"data": 1,
}
`;

exports[`yup number 支持 positiveInteger() 2`] = `
Object {
"data": -1,
"error": [ValidationError: 此项必须是一个正整数],
}
`;

exports[`yup number 支持 positiveInteger() 3`] = `
Object {
"data": 0,
"error": [ValidationError: 此项必须是一个正整数],
}
`;

exports[`yup number 支持 positiveInteger() 4`] = `
Object {
"data": 0.5,
"error": [ValidationError: 此项必须是一个正整数],
}
`;

exports[`yup number 支持 positiveInteger() 5`] = `
Object {
"data": 1.5,
"error": [ValidationError: 此项必须是一个正整数],
}
`;

exports[`yup number 支持 positiveInteger() 6`] = `
Object {
"data": 100,
}
`;

exports[`yup test 第一个参数可传函数或正则 1`] = `
Object {
"data": Object {
Expand Down
4 changes: 4 additions & 0 deletions src/validator/locale/enUS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export const enUS: yup.Locale = {
negative: '${path} must be a negative number',
integer: '${path} must be an integer',
id: '${path} must be a positive integer',
positiveInteger: '${path} must be a positive integer',
negativeInteger: '${path} must be a negative integer',
nonpositiveInteger: '${path} must be a nonpositive integer',
nonnegativeInteger: '${path} must be a nonnegative integer',
},
date: {
min: '${path} field must be later than ${min}',
Expand Down
4 changes: 4 additions & 0 deletions src/validator/locale/zhCN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export const zhCN: yup.Locale = {
negative: ({ label }) => `${label || '此项'}必须是一个负数`,
integer: ({ label }) => `${label || '此项'}必须是一个整数`,
id: ({ label }) => `${label || '此项'}必须是一个正整数`,
positiveInteger: ({ label }) => `${label || '此项'}必须是一个正整数`,
negativeInteger: ({ label }) => `${label || '此项'}必须是一个负整数`,
nonpositiveInteger: ({ label }) => `${label || '此项'}必须是一个非正整数`,
nonnegativeInteger: ({ label }) => `${label || '此项'}必须是一个非负整数`,
},
date: {
min: ({ label, min }) => `${label || '此项'}必须晚于${min}`,
Expand Down
43 changes: 43 additions & 0 deletions src/validator/yup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,47 @@ describe('yup', () => {
expect(rule.validatePlusSync(1.5)).toMatchSnapshot()
expect(rule.validatePlusSync(100)).toMatchSnapshot()
})

test('number 支持 positiveInteger()', () => {
const rule = yup.number().positiveInteger()
expect(rule.validatePlusSync(1)).toMatchSnapshot()
expect(rule.validatePlusSync(-1)).toMatchSnapshot()
expect(rule.validatePlusSync(0)).toMatchSnapshot()
expect(rule.validatePlusSync(0.5)).toMatchSnapshot()
expect(rule.validatePlusSync(1.5)).toMatchSnapshot()
expect(rule.validatePlusSync(100)).toMatchSnapshot()
})

test('number 支持 negativeInteger()', () => {
const rule = yup.number().negativeInteger()
expect(rule.validatePlusSync(1)).toMatchSnapshot()
expect(rule.validatePlusSync(-1)).toMatchSnapshot()
expect(rule.validatePlusSync(-0)).toMatchSnapshot()
expect(rule.validatePlusSync(0.5)).toMatchSnapshot()
expect(rule.validatePlusSync(1.5)).toMatchSnapshot()
expect(rule.validatePlusSync(100)).toMatchSnapshot()
expect(rule.validatePlusSync(-100.3)).toMatchSnapshot()
})

test('number 支持 nonpositiveInteger()', () => {
const rule = yup.number().nonpositiveInteger()
expect(rule.validatePlusSync(1)).toMatchSnapshot()
expect(rule.validatePlusSync(-1)).toMatchSnapshot()
expect(rule.validatePlusSync(-0)).toMatchSnapshot()
expect(rule.validatePlusSync(0.5)).toMatchSnapshot()
expect(rule.validatePlusSync(1.5)).toMatchSnapshot()
expect(rule.validatePlusSync(100)).toMatchSnapshot()
expect(rule.validatePlusSync(-100.3)).toMatchSnapshot()
})

test('number 支持 nonnegativeInteger()', () => {
const rule = yup.number().nonnegativeInteger()
expect(rule.validatePlusSync(1)).toMatchSnapshot()
expect(rule.validatePlusSync(-1)).toMatchSnapshot()
expect(rule.validatePlusSync(-0)).toMatchSnapshot()
expect(rule.validatePlusSync(0.5)).toMatchSnapshot()
expect(rule.validatePlusSync(1.5)).toMatchSnapshot()
expect(rule.validatePlusSync(100)).toMatchSnapshot()
expect(rule.validatePlusSync(-100.3)).toMatchSnapshot()
})
})
4 changes: 4 additions & 0 deletions src/validator/yupSource/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export let number = {
negative: '${path} must be a negative number',
integer: '${path} must be an integer',
id: '${path} must be a positive integer',
positiveInteger: '${path} must be a positive integer',
negativeInteger: '${path} must be a negative integer',
nonpositiveInteger: '${path} must be a nonpositive integer',
nonnegativeInteger: '${path} must be a nonnegative integer',
}

export let date = {
Expand Down
26 changes: 26 additions & 0 deletions src/validator/yupSource/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,32 @@ inherits(NumberSchema, MixedSchema, {
return this.positive(msg).integer(msg)
},

positiveInteger(msg = locale.positiveInteger) {
return this.positive(msg).integer(msg)
},

negativeInteger(msg = locale.negativeInteger) {
return this.negative(msg).integer(msg)
},

nonpositiveInteger(msg = locale.nonpositiveInteger) {
return this.test({
name: 'nonpositiveInteger',
message: msg,
exclusive: true,
test: val => isAbsent(val) || (Number.isInteger(val) && val <= 0),
})
},

nonnegativeInteger(msg = locale.nonnegativeInteger) {
return this.test({
name: 'nonnegativeInteger',
message: msg,
exclusive: true,
test: val => isAbsent(val) || (Number.isInteger(val) && val >= 0),
})
},

truncate() {
return this.transform(value => (!isAbsent(value) ? value | 0 : value))
},
Expand Down
4 changes: 4 additions & 0 deletions src/validator/yupTypes/Locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export interface NumberLocale {
negative: LocaleValue<{ less: number }>
integer: LocaleValue
id: LocaleValue
positiveInteger: LocaleValue
negativeInteger: LocaleValue
nonpositiveInteger: LocaleValue
nonnegativeInteger: LocaleValue
}

export interface DateLocale {
Expand Down
8 changes: 8 additions & 0 deletions src/validator/yupTypes/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ export interface NumberSchema<T extends number = number>

id(message?: NumberLocale['id']): this

positiveInteger(message?: NumberLocale['positiveInteger']): this

negativeInteger(message?: NumberLocale['negativeInteger']): this

nonpositiveInteger(message?: NumberLocale['nonpositiveInteger']): this

nonnegativeInteger(message?: NumberLocale['nonnegativeInteger']): this

truncate(): this

round(type?: 'floor' | 'ceil' | 'trunc' | 'round'): this
Expand Down

0 comments on commit 56c0abf

Please sign in to comment.