Skip to content
Myr

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

4 files+100 added−2 removed

modified:deno_dist/validator/validator.ts+9 added-1 removed
2 unmodified lines
3import { rule } from './rule.ts'4import { sanitizer } from './sanitizer.ts'5import type { Schema } from './schema.ts'67type Target = 'query' | 'header' | 'body' | 'json'7type Target = 'query' | 'queries' | 'header' | 'body' | 'json'8type Type = JSONPrimitive | JSONObject | JSONArray | File9type RuleFunc = (value: Type) => boolean10type Rule = {11 name: string
77 unmodified lines
8990export 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 lines
216217 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 lines
330 private validateValue = (func: (value: Type) => boolean, value: Type): boolean => {331 if (this._optional && typeof value === 'undefined') return true332333 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 lines
366 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
modified:src/middleware/validator/middleware.test.ts+37 added
44 unmodified lines
45 )46 })47})4849describe('Basic - queries', () => {50 const app = new Hono()5152 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 )6263 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 })6768 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 })7778 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})8586describe('Basic - body', () => {87 const app = new Hono()8889 const middleware = validator((v) => ({
790 unmodified lines
modified:src/validator/validator.test.ts+45 added
41 unmodified lines
42 expect(results[1].message).toBe(messages.join('\n'))43 })44})4546describe('Basic - queries', () => {47 const v = new Validator()4849 const req = new Request('http://localhost/?tag=foo&tag=bar&id=123&id=456')5051 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 })6061 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 })7172 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 })8081 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})9091describe('Basic - header', () => {92 const v = new Validator()9394 const req = new Request('http://localhost/', {
724 unmodified lines
modified:src/validator/validator.ts+9 added-1 removed
2 unmodified lines
3import { rule } from './rule'4import { sanitizer } from './sanitizer'5import type { Schema } from './schema'67type Target = 'query' | 'header' | 'body' | 'json'7type Target = 'query' | 'queries' | 'header' | 'body' | 'json'8type Type = JSONPrimitive | JSONObject | JSONArray | File9type RuleFunc = (value: Type) => boolean10type Rule = {11 name: string
77 unmodified lines
8990export 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 lines
216217 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 lines
330 private validateValue = (func: (value: Type) => boolean, value: Type): boolean => {331 if (this._optional && typeof value === 'undefined') return true332333 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 lines
366 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