Skip to content
Myr

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

6 files+44 added−10 removed

modified:deno_dist/middleware/validator/middleware.ts+1 added-1 removed
9 unmodified lines
10 | Record<string, string | number>11 | ValidatorMessage12type Rule = Function | [Function, ...Param[]]13type Rules = Rule | Rule[]14type Done = (resultSet: ResultSet, context: Context) => Response | undefined14type Done = (resultSet: ResultSet, context: Context) => Response | void1516type RuleSet = {17 body: Record<string, Rules>18 json: Record<string, Rules>
196 unmodified lines
modified:deno_dist/middleware/validator/validator.ts+11 added-4 removed
modified:src/middleware/validator/middleware.test.ts+4 added
251 unmodified lines
252 '/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 lines
277278 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 lines
modified:src/middleware/validator/middleware.ts+1 added-1 removed
9 unmodified lines
10 | Record<string, string | number>11 | ValidatorMessage12type Rule = Function | [Function, ...Param[]]13type Rules = Rule | Rule[]14type Done = (resultSet: ResultSet, context: Context) => Response | undefined14type Done = (resultSet: ResultSet, context: Context) => Response | void1516type RuleSet = {17 body: Record<string, Rules>18 json: Record<string, Rules>
196 unmodified lines
modified:src/middleware/validator/validator.test.ts+16 added
modified:src/middleware/validator/validator.ts+11 added-4 removed
8 unmodified lines
9}1011export 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 },1718 optional(): boolean {19 return true20 },2122 isFalsy(value: unknown): boolean {23 return !value24 },2526 isNotFalsy(value: unknown): boolean {27 return !!value28 },2930 isAlpha(value: string): boolean {31 return isString(value) && value.match(/^[A-Z]+$/i) ? true : false32 },33
66 unmodified lines