Skip to content

Commit

Permalink
fix(validator): fix unexpect validate with empty format (#2926)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Mar 12, 2022
1 parent 6e9a8ae commit 7da2628
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
13 changes: 13 additions & 0 deletions packages/validator/src/__tests__/validator.spec.ts
Expand Up @@ -392,6 +392,19 @@ test('validate custom formats', async () => {
noError(await validate('涓枃', 'custom'))
})

test('validate undefined format', async () => {
expect(
(
await validate('a', {
required: false,
pattern: '(\\d{3,4}-\\d{7,8}-\\d{4})|(4\\d{4,9})|(\\d{3,4}-\\d{7,8})',
format: undefined,
message: 'error',
})
).error
).toEqual(['error'])
})

test('validator return boolean', async () => {
hasError(
await validate('123', {
Expand Down
10 changes: 7 additions & 3 deletions packages/validator/src/rules.ts
Expand Up @@ -46,9 +46,13 @@ const extendSameRules = (
const RULES: IRegistryRules = {
format(value, rule) {
if (isValidateEmpty(value)) return ''
return !new RegExp(getValidateFormats(rule.format) || '').test(value)
? rule.message
: ''
if (rule.format) {
const format = getValidateFormats(rule.format)
if (format) {
return !new RegExp(format).test(value) ? rule.message : ''
}
}
return ''
},
required(value, rule) {
if (rule.required === false) return ''
Expand Down

0 comments on commit 7da2628

Please sign in to comment.