Skip to content

Commit

Permalink
fix(validator): enable checking undefined in rules (honojs#708)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Dec 8, 2022
1 parent 6d5de04 commit a5b8b44
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
7 changes: 7 additions & 0 deletions deno_dist/validator/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const rule = {
ignore_whitespace: boolean
} = { ignore_whitespace: false }
): boolean {
if (value === undefined) return false
return (options.ignore_whitespace ? value.trim().length : value.length) === 0
},

Expand All @@ -20,6 +21,7 @@ export const rule = {
options: Partial<{ min: number; max: number }> | number,
arg2?: number
) => {
if (value === undefined) return false
let min: number
let max: number | undefined
if (typeof options === 'object') {
Expand All @@ -37,10 +39,12 @@ export const rule = {
},

isAlpha: (value: string): boolean => {
if (value === undefined) return false
return /^[A-Z]+$/i.test(value)
},

isNumeric: (value: string): boolean => {
if (value === undefined) return false
return /^[0-9]+$/.test(value)
},

Expand All @@ -52,6 +56,7 @@ export const rule = {
minOccurrences: 1,
}
): boolean => {
if (value === undefined || elem === undefined) return false
options.ignoreCase ||= false
options.minOccurrences ||= 1
if (options.ignoreCase) {
Expand All @@ -61,6 +66,7 @@ export const rule = {
},

isIn: (value: string, options: string[]): boolean => {
if (value === undefined) return false
if (typeof options === 'object') {
for (const elem of options) {
if (elem === value) return true
Expand All @@ -70,6 +76,7 @@ export const rule = {
},

match: (value: string, regExp: RegExp): boolean => {
if (value === undefined || regExp === undefined) return false
return regExp.test(value)
},

Expand Down
20 changes: 20 additions & 0 deletions src/validator/rule.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { rule } from './rule'

describe('Rules', () => {
// string
test('isEmpty', () => {
/** @ts-ignore */
expect(rule.isEmpty()).toBeFalsy()
expect(rule.isEmpty('')).toBeTruthy()
expect(rule.isEmpty('abcdef')).toBeFalsy()
expect(rule.isEmpty('')).toBeTruthy()
expect(rule.isEmpty(' ')).toBeFalsy()
expect(rule.isEmpty(' ', { ignore_whitespace: true })).toBeTruthy()
})
test('isLength', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
/** @ts-ignore */
expect(rule.isLength(undefined, { min: 1 })).toBeFalsy()
expect(rule.isLength('abcde', { min: 1, max: 10 })).toBeTruthy()
expect(rule.isLength('abcde', { min: 1, max: 4 })).toBeFalsy()
expect(rule.isLength('abcde', { max: 10 })).toBeTruthy()
Expand All @@ -20,34 +26,48 @@ describe('Rules', () => {
expect(rule.isLength('abcde', 1, 4)).toBeFalsy()
})
test('isAlpha', () => {
/** @ts-ignore */
expect(rule.isAlpha()).toBeFalsy()
expect(rule.isAlpha('abcdef')).toBeTruthy()
expect(rule.isAlpha('abc123')).toBeFalsy()
})
test('isNumeric', () => {
/** @ts-ignore */
expect(rule.isNumeric()).toBeFalsy()
expect(rule.isNumeric('01234')).toBeTruthy()
expect(rule.isNumeric('012abc')).toBeFalsy()
})
test('contains', () => {
/** @ts-ignore */
expect(rule.contains()).toBeFalsy()
expect(rule.contains('barfoobaz', 'foo')).toBeTruthy()
expect(rule.contains('barfoobaz', 'fooo')).toBeFalsy()
expect(rule.contains('barfoobaz', 'Foo')).toBeFalsy()
expect(rule.contains('barfoobaz', 'Foo', { ignoreCase: true })).toBeTruthy()
})
test('isIn', () => {
/** @ts-ignore */
expect(rule.isIn()).toBeFalsy()
expect(rule.isIn('foo', ['foo', 'bar'])).toBeTruthy()
expect(rule.isIn('foo', ['zzz', 'yyy'])).toBeFalsy()
})
test('match', () => {
/** @ts-ignore */
expect(rule.match()).toBeFalsy()
expect(rule.match('abcdef', /def$/)).toBeTruthy()
expect(rule.match('abcdef', /^def$/)).toBeFalsy()
})
// number
test('isGte', () => {
/** @ts-ignore */
expect(rule.isGte()).toBeFalsy()
expect(rule.isGte(20, 10)).toBeTruthy()
expect(rule.isGte(10, 20)).toBeFalsy()
expect(rule.isGte(10, 10)).toBeTruthy()
})
test('isLte', () => {
/** @ts-ignore */
expect(rule.isLte()).toBeFalsy()
expect(rule.isLte(10, 20)).toBeTruthy()
expect(rule.isLte(20, 10)).toBeFalsy()
expect(rule.isLte(10, 10)).toBeTruthy()
Expand Down
7 changes: 7 additions & 0 deletions src/validator/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const rule = {
ignore_whitespace: boolean
} = { ignore_whitespace: false }
): boolean {
if (value === undefined) return false
return (options.ignore_whitespace ? value.trim().length : value.length) === 0
},

Expand All @@ -20,6 +21,7 @@ export const rule = {
options: Partial<{ min: number; max: number }> | number,
arg2?: number
) => {
if (value === undefined) return false
let min: number
let max: number | undefined
if (typeof options === 'object') {
Expand All @@ -37,10 +39,12 @@ export const rule = {
},

isAlpha: (value: string): boolean => {
if (value === undefined) return false
return /^[A-Z]+$/i.test(value)
},

isNumeric: (value: string): boolean => {
if (value === undefined) return false
return /^[0-9]+$/.test(value)
},

Expand All @@ -52,6 +56,7 @@ export const rule = {
minOccurrences: 1,
}
): boolean => {
if (value === undefined || elem === undefined) return false
options.ignoreCase ||= false
options.minOccurrences ||= 1
if (options.ignoreCase) {
Expand All @@ -61,6 +66,7 @@ export const rule = {
},

isIn: (value: string, options: string[]): boolean => {
if (value === undefined) return false
if (typeof options === 'object') {
for (const elem of options) {
if (elem === value) return true
Expand All @@ -70,6 +76,7 @@ export const rule = {
},

match: (value: string, regExp: RegExp): boolean => {
if (value === undefined || regExp === undefined) return false
return regExp.test(value)
},

Expand Down

0 comments on commit a5b8b44

Please sign in to comment.