Skip to content

Commit

Permalink
feat(validator): number 新增 nonPositive, nonNegative 规则
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Nov 12, 2021
1 parent ad3c31b commit a0d1f9c
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 32 deletions.
118 changes: 104 additions & 14 deletions src/validator/__snapshots__/yup.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -88,93 +88,183 @@ Object {
}
`;

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

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

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

exports[`yup number 支持 nonNegative() 4`] = `
Object {
"data": 0.5,
}
`;

exports[`yup number 支持 nonNegative() 5`] = `
Object {
"data": 1.5,
}
`;

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

exports[`yup number 支持 nonNegative() 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`] = `
exports[`yup number 支持 nonNegativeInteger() 3`] = `
Object {
"data": -0,
}
`;

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

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

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

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

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

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

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

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

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

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

exports[`yup number 支持 nonPositive() 7`] = `
Object {
"data": -100.3,
}
`;

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

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

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

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

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

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

exports[`yup number 支持 nonpositiveInteger() 7`] = `
exports[`yup number 支持 nonPositiveInteger() 7`] = `
Object {
"data": -100.3,
"error": [ValidationError: 此项必须是一个非正整数],
Expand Down
6 changes: 4 additions & 2 deletions src/validator/locale/enUS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ export const enUS: yup.Locale = {
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',
nonPositive: '${path} must be a non-positive number',
nonNegative: '${path} must be a non-negative number',
nonPositiveInteger: '${path} must be a non-positive integer',
nonNegativeInteger: '${path} must be a non-negative integer',
},
date: {
min: '${path} field must be later than ${min}',
Expand Down
6 changes: 4 additions & 2 deletions src/validator/locale/zhCN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ export const zhCN: yup.Locale = {
id: ({ label }) => `${label || '此项'}必须是一个正整数`,
positiveInteger: ({ label }) => `${label || '此项'}必须是一个正整数`,
negativeInteger: ({ label }) => `${label || '此项'}必须是一个负整数`,
nonpositiveInteger: ({ label }) => `${label || '此项'}必须是一个非正整数`,
nonnegativeInteger: ({ label }) => `${label || '此项'}必须是一个非负整数`,
nonPositive: ({ label }) => `${label || '此项'}必须是一个非正数`,
nonNegative: ({ label }) => `${label || '此项'}必须是一个非负数`,
nonPositiveInteger: ({ label }) => `${label || '此项'}必须是一个非正整数`,
nonNegativeInteger: ({ label }) => `${label || '此项'}必须是一个非负整数`,
},
date: {
min: ({ label, min }) => `${label || '此项'}必须晚于${min}`,
Expand Down
30 changes: 26 additions & 4 deletions src/validator/yup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ describe('yup', () => {
expect(rule.validatePlusSync(-100.3)).toMatchSnapshot()
})

test('number 支持 nonpositiveInteger()', () => {
const rule = yup.number().nonpositiveInteger()
test('number 支持 nonPositive()', () => {
const rule = yup.number().nonPositive()
expect(rule.validatePlusSync(1)).toMatchSnapshot()
expect(rule.validatePlusSync(-1)).toMatchSnapshot()
expect(rule.validatePlusSync(-0)).toMatchSnapshot()
Expand All @@ -250,8 +250,30 @@ describe('yup', () => {
expect(rule.validatePlusSync(-100.3)).toMatchSnapshot()
})

test('number 支持 nonnegativeInteger()', () => {
const rule = yup.number().nonnegativeInteger()
test('number 支持 nonNegative()', () => {
const rule = yup.number().nonNegative()
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()
Expand Down
6 changes: 4 additions & 2 deletions src/validator/yupSource/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ export let number = {
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',
nonPositive: '${path} must be a non-positive number',
nonNegative: '${path} must be a non-negative number',
nonPositiveInteger: '${path} must be a non-positive integer',
nonNegativeInteger: '${path} must be a non-negative integer',
}

export let date = {
Expand Down
26 changes: 22 additions & 4 deletions src/validator/yupSource/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,36 @@ inherits(NumberSchema, MixedSchema, {
return this.negative(msg).integer(msg)
},

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

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

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) {
nonNegativeInteger(msg = locale.nonNegativeInteger) {
return this.test({
name: 'nonnegativeInteger',
name: 'nonNegativeInteger',
message: msg,
exclusive: true,
test: val => isAbsent(val) || (Number.isInteger(val) && val >= 0),
Expand Down
6 changes: 4 additions & 2 deletions src/validator/yupTypes/Locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ export interface NumberLocale {
id: LocaleValue
positiveInteger: LocaleValue
negativeInteger: LocaleValue
nonpositiveInteger: LocaleValue
nonnegativeInteger: LocaleValue
nonPositive: LocaleValue
nonNegative: LocaleValue
nonPositiveInteger: LocaleValue
nonNegativeInteger: LocaleValue
}

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

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

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

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

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

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

truncate(): this

Expand Down

0 comments on commit a0d1f9c

Please sign in to comment.