feat(validator): add `isFalsy` and `isNotFalsy` (#523)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent e2d704f · Browse files · 6 files·+44 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+44 added−10 removed
- tests:+20 added−0 removed
- other:+24 added−10 removed
Provenance
- Commit
62e3292- Tree
8f6182a- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
e2d704f
6 files changed+44 added−10 removed
modified:deno_dist/middleware/validator/middleware.ts+1 added-1 removed
9 unmodified lines10 | Record<string, string | number>11 | ValidatorMessage12 type Rule = Function | [Function, ...Param[]]13 type Rules = Rule | Rule[]14-type Done = (resultSet: ResultSet, context: Context) => Response | undefined14+type Done = (resultSet: ResultSet, context: Context) => Response | void15 16 type RuleSet = {17 body: Record<string, Rules>18 json: Record<string, Rules>196 unmodified linesmodified:deno_dist/middleware/validator/validator.ts+11 added-4 removed
8 unmodified lines91011121314151613141516171819202122232425262728293031323366 unmodified lines8 unmodified lines}
export const validator = { required(value: unknown): boolean { if (value) { return true } return false if (value === undefined) return false if (isString(value) && value === '') return false return true },
optional(): boolean { return true },
isFalsy(value: unknown): boolean { return !value },
isNotFalsy(value: unknown): boolean { return !!value },
isAlpha(value: string): boolean { return isString(value) && value.match(/^[A-Z]+$/i) ? true : false },
66 unmodified linesmodified:src/middleware/validator/middleware.test.ts+4 added
251 unmodified lines252 '/json',253 validation((v) => ({254 json: {255 value: v.required,256+ foo: v.isFalsy,257+ bar: v.isNotFalsy,258 },259 })),260 async (c) => {261 const json = (await c.req.json()) as { value: string }15 unmodified lines277 278 it('Should return 200 response - clone body/JSON', async () => {279 const json = {280 value: 'foo',281+ foo: false,282+ bar: true,283 }284 const req = new Request('http://localhost/json', {285 method: 'POST',286 body: JSON.stringify(json),154 unmodified linesmodified:src/middleware/validator/middleware.ts+1 added-1 removed
9 unmodified lines10 | Record<string, string | number>11 | ValidatorMessage12 type Rule = Function | [Function, ...Param[]]13 type Rules = Rule | Rule[]14-type Done = (resultSet: ResultSet, context: Context) => Response | undefined14+type Done = (resultSet: ResultSet, context: Context) => Response | void15 16 type RuleSet = {17 body: Record<string, Rules>18 json: Record<string, Rules>196 unmodified linesmodified:src/middleware/validator/validator.test.ts+16 added
1234567891011121314151617181920212223242526272829303166 unmodified linesimport { validator } from './validator'
describe('Test the validator rules', () => { test('required', () => { expect(validator.required('a')).toBeTruthy() expect(validator.required(0)).toBeTruthy() expect(validator.required(undefined)).toBeFalsy() expect(validator.required('')).toBeFalsy() })
test('optional', () => { expect(validator.optional).toBeTruthy() })
test('isFalsy', () => { expect(validator.isFalsy(undefined)).toBeTruthy() expect(validator.isFalsy(0)).toBeTruthy() expect(validator.isFalsy('')).toBeTruthy() expect(validator.isFalsy(null)).toBeTruthy() })
test('isNotFalsy', () => { expect(validator.isNotFalsy('abc')).toBeTruthy() expect(validator.isNotFalsy(123)).toBeTruthy() expect(validator.isNotFalsy(true)).toBeTruthy() })
test('isAlpha', () => { expect(validator.isAlpha('abcdef')).toBeTruthy() expect(validator.isAlpha('abc123')).toBeFalsy() // eslint-disable-next-line @typescript-eslint/ban-ts-comment66 unmodified linesmodified:src/middleware/validator/validator.ts+11 added-4 removed
8 unmodified lines9 }10 11 export const validator = {12 required(value: unknown): boolean {13- if (value) {14- return true15- }16- return false13+ if (value === undefined) return false14+ if (isString(value) && value === '') return false15+ return true16 },17 18 optional(): boolean {19 return true20 },21 22+ isFalsy(value: unknown): boolean {23+ return !value24+ },25+26+ isNotFalsy(value: unknown): boolean {27+ return !!value28+ },29+30 isAlpha(value: string): boolean {31 return isString(value) && value.match(/^[A-Z]+$/i) ? true : false32 },33 66 unmodified lines