validator.ts
This file
Path
src/validator/validator.ts
119 lines · 3.5 KiB · TypeScript
Last changed by
fix(validator): cache `arrayBuffer` to use after validation (#1393)Yusuke Wadabefore adoption5cbd89c
History of this fileTake it
The walk stopped after 200 commits: lines older than 309c38a point at it.
denoifyYusuke Wadabefore adoption309c38a
line 1why, one step back1import type { Context } from '../context'feat: introduce "Helpers" (#1353)Yusuke Wadabefore adoptione070191
line 2why, one step back2import { getCookie } from '../helper/cookie'denoifyYusuke Wadabefore adoption309c38a
line 3why, one step back3import type { Env, ValidationTargets, MiddlewareHandler } from '../types'fix(validator): cache `arrayBuffer` to use after validation (#1393)Yusuke Wadabefore adoption5cbd89c
lines 4–5why, one step back4import type { BodyData } from '../utils/body'5import { bufferToFormData } from '../utils/buffer'denoifyYusuke Wadabefore adoption309c38a
lines 6–16why, one step back67type ValidationTargetKeysWithBody = 'form' | 'json'8type ValidationTargetByMethod<M> = M extends 'get' | 'head' // GET and HEAD request must not have a body content.9 ? Exclude<keyof ValidationTargets, ValidationTargetKeysWithBody>10 : keyof ValidationTargets1112export type ValidationFunction<13 InputType,14 OutputType,15 E extends Env = {},16 P extends string = stringfix(validator): support async validator func (#1303)Yusuke Wadabefore adoptionf7bce19
lines 17–20why, one step back17> = (18 value: InputType,19 c: Context<E, P>20) => OutputType | Response | Promise<OutputType> | Promise<Response>denoifyYusuke Wadabefore adoption309c38a
lines 21–52why, one step back2122export const validator = <23 InputType,24 P extends string,25 M extends string,26 U extends ValidationTargetByMethod<M>,27 OutputType = ValidationTargets[U],28 P2 extends string = P,29 V extends {30 in: { [K in U]: unknown extends InputType ? OutputType : InputType }31 out: { [K in U]: OutputType }32 } = {33 in: { [K in U]: unknown extends InputType ? OutputType : InputType }34 out: { [K in U]: OutputType }35 },36 // eslint-disable-next-line @typescript-eslint/no-explicit-any37 E extends Env = any38>(39 target: U,40 validationFunc: ValidationFunction<41 unknown extends InputType ? ValidationTargets[U] : InputType,42 OutputType,43 E,44 P245 >46): MiddlewareHandler<E, P, V> => {47 return async (c, next) => {48 let value = {}4950 switch (target) {51 case 'json':52 try {fix(validator): cache `arrayBuffer` to use after validation (#1393)Yusuke Wadabefore adoption5cbd89c
lines 53–60why, one step back53 /**54 * Get the arrayBuffer first, create JSON object via Response,55 * and cache the arrayBuffer in the c.req.bodyCache.56 */57 const arrayBuffer = c.req.bodyCache.arrayBuffer ?? (await c.req.raw.arrayBuffer())58 value = await new Response(arrayBuffer).json()59 c.req.bodyCache.json = value60 c.req.bodyCache.arrayBuffer = arrayBufferdenoifyYusuke Wadabefore adoption309c38a
lines 61–71why, one step back61 } catch {62 console.error('Error: Malformed JSON in request body')63 return c.json(64 {65 success: false,66 message: 'Malformed JSON in request body',67 },68 40069 )70 }71 breakfix(validator): cache `arrayBuffer` to use after validation (#1393)Yusuke Wadabefore adoption5cbd89c
lines 72–84why, one step back72 case 'form': {73 const contentType = c.req.header('Content-Type')74 if (contentType) {75 const arrayBuffer = c.req.bodyCache.arrayBuffer ?? (await c.req.raw.arrayBuffer())76 const formData = bufferToFormData(arrayBuffer, contentType)77 const form: BodyData = {}78 formData.forEach((value, key) => {79 form[key] = value80 })81 value = form82 c.req.bodyCache.formData = formData83 c.req.bodyCache.arrayBuffer = arrayBuffer84 }denoifyYusuke Wadabefore adoption309c38a
line 85why, one step back85 breakfix(validator): cache `arrayBuffer` to use after validation (#1393)Yusuke Wadabefore adoption5cbd89c
line 86why, one step back86 }denoifyYusuke Wadabefore adoption309c38a
lines 87–95why, one step back87 case 'query':88 value = Object.fromEntries(89 Object.entries(c.req.queries()).map(([k, v]) => {90 return v.length === 1 ? [k, v[0]] : [k, v]91 })92 )93 break94 case 'queries':95 value = c.req.queries()feat(validator): deprecate `queries` (use `query` instead) (#1350)Yusuke Wadabefore adoption9e391cf
line 96why, one step back96 console.log('Warnings: Validate type `queries` is deprecated. Use `query` instead.')denoifyYusuke Wadabefore adoption309c38a
lines 97–99why, one step back97 break98 case 'param':99 value = c.req.param() as Record<string, string>feat(validator): supports `header` and `cookie` (#1352)Yusuke Wadabefore adoption8adc8c1
lines 100–105why, one step back100 break101 case 'header':102 value = c.req.header()103 break104 case 'cookie':105 value = getCookie(c)denoifyYusuke Wadabefore adoption309c38a
lines 106–108why, one step back106 break107 }108fix(validator): support async validator func (#1303)Yusuke Wadabefore adoptionf7bce19
line 109why, one step back109 const res = await validationFunc(value as never, c as never)denoifyYusuke Wadabefore adoption309c38a
line 110why, one step back110fix(validator): support async validator func (#1303)Yusuke Wadabefore adoptionf7bce19
line 111why, one step back111 if (res instanceof Response) {denoifyYusuke Wadabefore adoption309c38a
lines 112–119why, one step back112 return res113 }114115 c.req.addValidatedData(target, res as never)116117 await next()118 }119}