Skip to content
Myr

fix(validator): use the cached content (#2234)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(validator): use the cached content * denoify

authored by Yusuke Wada · committed by GitHub · parent d67babf · Browse files · 3 files+74 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+74 added−4 removed

  • tests:+56 added−0 removed
  • other:+18 added−4 removed

3 files+74 added−4 removed

modified:deno_dist/validator/validator.ts+9 added-2 removed
modified:src/validator/validator.test.ts+56 added
170 unmodified lines
171 })172 })173})174175describe('Cached contents', () => {176 const app = new Hono()177178 app.post(179 '/json',180 async (c, next) => {181 await c.req.json()182 await next()183 },184 validator('json', (value) => {185 return value186 }),187 async (c) => {188 const data = await c.req.json()189 return c.json(data, 200)190 }191 )192193 app.post(194 '/form',195 async (c, next) => {196 await c.req.formData()197 await next()198 },199 validator('form', (value) => {200 return value201 }),202 async (c) => {203 return c.json({ message: 'OK' }, 200)204 }205 )206207 it('Should return the cached JSON content', async () => {208 const res = await app.request('/json', {209 method: 'POST',210 body: JSON.stringify({ message: 'Hello' }),211 headers: {212 'Content-Type': 'application/json',213 },214 })215 expect(res.status).toBe(200)216 expect(await res.json()).toEqual({ message: 'Hello' })217 })218219 it('Should return the cached form content', async () => {220 const form = new FormData()221 form.append('message', 'Hello')222 const res = await app.request('/form', {223 method: 'POST',224 body: form,225 })226 expect(res.status).toBe(200)227 expect(await res.json()).toEqual({ message: 'OK' })228 })229})230231describe('Validator middleware with a custom validation function', () => {232 const app = new Hono()233234 const validationFunction: ValidationFunction<{ id: string }, { id: number }> = (v) => {
694 unmodified lines
modified:src/validator/validator.ts+9 added-2 removed
65 unmodified lines
66 if (!contentType || !contentType.startsWith('application/json')) {67 const message = `Invalid HTTP header: Content-Type=${contentType}`68 throw new HTTPException(400, { message })69 }70 if (c.req.bodyCache.json) {71 value = await c.req.bodyCache.json72 break73 }74 /**71 * Get the arrayBuffer first, create JSON object via Response,72 * and cache the arrayBuffer in the c.req.bodyCache.75 * Get the arrayBuffer, create JSON object via Response, and cache the arrayBuffer in the c.req.bodyCache if there is no cached content in c.req.bodyCache.json.76 */77 try {78 const arrayBuffer = c.req.bodyCache.arrayBuffer ?? (await c.req.raw.arrayBuffer())79 value = await new Response(arrayBuffer).json()
4 unmodified lines
84 throw new HTTPException(400, { message })85 }86 break87 case 'form': {88 if (c.req.bodyCache.formData) {89 value = c.req.bodyCache.formData90 break91 }92 try {93 const contentType = c.req.header('Content-Type')94 if (contentType) {95 const arrayBuffer = c.req.bodyCache.arrayBuffer ?? (await c.req.raw.arrayBuffer())
45 unmodified lines