feat(validator): support `v.queries` (#636)
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 6a96ace · Browse files · 4 files·+100 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+100 added−2 removed
- tests:+82 added−0 removed
- other:+18 added−2 removed
Provenance
- Commit
c14dd5a- Tree
ee4f20c- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
6a96ace
4 files changed+100 added−2 removed
modified:deno_dist/validator/validator.ts+9 added-1 removed
2 unmodified lines3 import { rule } from './rule.ts'4 import { sanitizer } from './sanitizer.ts'5 import type { Schema } from './schema.ts'6 7-type Target = 'query' | 'header' | 'body' | 'json'7+type Target = 'query' | 'queries' | 'header' | 'body' | 'json'8 type Type = JSONPrimitive | JSONObject | JSONArray | File9 type RuleFunc = (value: Type) => boolean10 type Rule = {11 name: string77 unmodified lines89 90 export class Validator {91 isArray: boolean = false92 query = (key: string): VString => new VString({ target: 'query', key: key })93+ queries = (key: string): VStringArray => new VStringArray({ target: 'queries', key: key })94 header = (key: string): VString => new VString({ target: 'header', key: key })95 body = (key: string): VString => new VString({ target: 'body', key: key })96 json = (key: string) => {97 if (this.isArray) {118 unmodified lines216 217 if (this.target === 'query') {218 value = req.query(this.key)219 }220+ if (this.target === 'queries') {221+ value = req.queries(this.key)222+ }223 if (this.target === 'header') {224 value = req.header(this.key)225 }226 if (this.target === 'body') {103 unmodified lines330 private validateValue = (func: (value: Type) => boolean, value: Type): boolean => {331 if (this._optional && typeof value === 'undefined') return true332 333 if (Array.isArray(value)) {334+ if (value.length === 0 && !this._optional) return false335 // Sanitize336 for (const sanitizer of this.sanitizers) {337 value = value.map((innerVal: any) => sanitizer(innerVal)) as JSONArray338 }27 unmodified lines366 switch (this.target) {367 case 'query':368 keyText = `the query parameter "${this.key}"`369 break370+ case 'queries':371+ keyText = `the query parameters "${this.key}"`372+ break373 case 'header':374 keyText = `the request header "${this.key}"`375 break376 case 'body':125 unmodified linesmodified:src/middleware/validator/middleware.test.ts+37 added
44 unmodified lines45 )46 })47 })48 49+describe('Basic - queries', () => {50+ const app = new Hono()51+52+ app.get(53+ '/',54+ validator((v) => ({55+ ids: v.queries('id').isNumeric().isRequired(),56+ tags: v.queries('tag').isOptional(),57+ })),58+ (c) => {59+ return c.text('Valid')60+ }61+ )62+63+ it('Should return 200 response', async () => {64+ const res = await app.request('http://localhost/?id=123&id=456')65+ expect(res.status).toBe(200)66+ })67+68+ it('Should return 400 response - id is not set', async () => {69+ const res = await app.request('http://localhost/')70+ expect(res.status).toBe(400)71+ const messages = [72+ 'Invalid Value []: the query parameters "id" is invalid - isNumeric',73+ 'Invalid Value []: the query parameters "id" is invalid - isRequired',74+ ]75+ expect(await res.text()).toBe(messages.join('\n'))76+ })77+78+ it('Should return 400 response - is is not numeric', async () => {79+ const res = await app.request('http://localhost/?id=one')80+ expect(res.status).toBe(400)81+ const messages = ['Invalid Value ["one"]: the query parameters "id" is invalid - isNumeric']82+ expect(await res.text()).toBe(messages.join('\n'))83+ })84+})85+86 describe('Basic - body', () => {87 const app = new Hono()88 89 const middleware = validator((v) => ({790 unmodified linesmodified:src/validator/validator.test.ts+45 added
41 unmodified lines42 expect(results[1].message).toBe(messages.join('\n'))43 })44 })45 46+describe('Basic - queries', () => {47+ const v = new Validator()48+49+ const req = new Request('http://localhost/?tag=foo&tag=bar&id=123&id=456')50+51+ it('Should be valid - tag', async () => {52+ const validator = v.queries('tag').isRequired()53+ const results = await validator.validate(req)54+ expect(results[0].isValid).toBe(true)55+ expect(results[0].message).toBeUndefined()56+ expect(results[0].value).toEqual(['foo', 'bar'])57+ expect(results[1].isValid).toBe(true)58+ expect(results[1].message).toBeUndefined()59+ })60+61+ it('Should be invalid - ids', async () => {62+ const validator = v.queries('ids').isRequired()63+ const results = await validator.validate(req)64+ console.log(results)65+ expect(results[0].isValid).toBe(true)66+ expect(results[1].isValid).toBe(false)67+ expect(results[1].message).toBe(68+ 'Invalid Value []: the query parameters "ids" is invalid - isRequired'69+ )70+ })71+72+ it('Should be valid - id', async () => {73+ const validator = v.queries('id').isNumeric()74+ const results = await validator.validate(req)75+ expect(results[0].isValid).toBe(true)76+ expect(results[0].message).toBeUndefined()77+ expect(results[1].isValid).toBe(true)78+ expect(results[1].message).toBeUndefined()79+ })80+81+ it('Should be valid - comment is options', async () => {82+ const validator = v.queries('comment').isOptional()83+ const results = await validator.validate(req)84+ expect(results[0].isValid).toBe(true)85+ expect(results[0].message).toBeUndefined()86+ expect(results[1].isValid).toBe(true)87+ expect(results[1].message).toBeUndefined()88+ })89+})90+91 describe('Basic - header', () => {92 const v = new Validator()93 94 const req = new Request('http://localhost/', {724 unmodified linesmodified:src/validator/validator.ts+9 added-1 removed
2 unmodified lines3 import { rule } from './rule'4 import { sanitizer } from './sanitizer'5 import type { Schema } from './schema'6 7-type Target = 'query' | 'header' | 'body' | 'json'7+type Target = 'query' | 'queries' | 'header' | 'body' | 'json'8 type Type = JSONPrimitive | JSONObject | JSONArray | File9 type RuleFunc = (value: Type) => boolean10 type Rule = {11 name: string77 unmodified lines89 90 export class Validator {91 isArray: boolean = false92 query = (key: string): VString => new VString({ target: 'query', key: key })93+ queries = (key: string): VStringArray => new VStringArray({ target: 'queries', key: key })94 header = (key: string): VString => new VString({ target: 'header', key: key })95 body = (key: string): VString => new VString({ target: 'body', key: key })96 json = (key: string) => {97 if (this.isArray) {118 unmodified lines216 217 if (this.target === 'query') {218 value = req.query(this.key)219 }220+ if (this.target === 'queries') {221+ value = req.queries(this.key)222+ }223 if (this.target === 'header') {224 value = req.header(this.key)225 }226 if (this.target === 'body') {103 unmodified lines330 private validateValue = (func: (value: Type) => boolean, value: Type): boolean => {331 if (this._optional && typeof value === 'undefined') return true332 333 if (Array.isArray(value)) {334+ if (value.length === 0 && !this._optional) return false335 // Sanitize336 for (const sanitizer of this.sanitizers) {337 value = value.map((innerVal: any) => sanitizer(innerVal)) as JSONArray338 }27 unmodified lines366 switch (this.target) {367 case 'query':368 keyText = `the query parameter "${this.key}"`369 break370+ case 'queries':371+ keyText = `the query parameters "${this.key}"`372+ break373 case 'header':374 keyText = `the request header "${this.key}"`375 break376 case 'body':125 unmodified lines