Skip to content

Commit

Permalink
fix(validator): don't return a FormData if formData is cached (#3067)
Browse files Browse the repository at this point in the history
* fix(validator): don't return a FormData if formData is cached

* fixed typo

* test the value
  • Loading branch information
yusukebe authored Jul 1, 2024
1 parent 94f47ec commit 2d452f2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 23 deletions.
30 changes: 30 additions & 0 deletions src/validator/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,24 @@ describe('Clone Request object', () => {
}
)

app.post(
'/cached',
async (c, next) => {
await c.req.parseBody()
await next()
},
validator('form', (value) => {
if (value instanceof FormData) {
throw new Error('The value should not be a FormData')
}
return value
}),
(c) => {
const v = c.req.valid('form')
return c.json(v)
}
)

it('Should not throw the error with c.req.parseBody()', async () => {
const body = new FormData()
body.append('foo', 'bar')
Expand All @@ -920,6 +938,18 @@ describe('Clone Request object', () => {
const res = await app.request(req)
expect(res.status).toBe(200)
})

it('Should not be an instance of FormData if the formData is cached', async () => {
const body = new FormData()
body.append('foo', 'bar')
const req = new Request('http://localhost/cached', {
method: 'POST',
body: body,
})
const res = await app.request(req)
expect(res.status).toBe(200)
expect(await res.json()).toEqual({ foo: 'bar' })
})
})
})

Expand Down
48 changes: 25 additions & 23 deletions src/validator/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,35 @@ export const validator = <
break
}

let formData: FormData

if (c.req.bodyCache.formData) {
value = await c.req.bodyCache.formData
break
formData = await c.req.bodyCache.formData
} else {
try {
const arrayBuffer = await c.req.arrayBuffer()
formData = await bufferToFormData(arrayBuffer, contentType)
c.req.bodyCache.formData = formData
} catch (e) {
let message = 'Malformed FormData request.'
message += e instanceof Error ? ` ${e.message}` : ` ${String(e)}`
throw new HTTPException(400, { message })
}
}

try {
const arrayBuffer = await c.req.arrayBuffer()
const formData = await bufferToFormData(arrayBuffer, contentType)
const form: BodyData<{ all: true }> = {}
formData.forEach((value, key) => {
if (key.endsWith('[]')) {
if (form[key] === undefined) {
form[key] = [value]
} else if (Array.isArray(form[key])) {
;(form[key] as unknown[]).push(value)
}
} else {
form[key] = value
const form: BodyData<{ all: true }> = {}
formData.forEach((value, key) => {
if (key.endsWith('[]')) {
if (form[key] === undefined) {
form[key] = [value]
} else if (Array.isArray(form[key])) {
;(form[key] as unknown[]).push(value)
}
})
value = form
c.req.bodyCache.formData = formData
} catch (e) {
let message = 'Malformed FormData request.'
message += e instanceof Error ? ` ${e.message}` : ` ${String(e)}`
throw new HTTPException(400, { message })
}
} else {
form[key] = value
}
})
value = form
break
}
case 'query':
Expand Down

0 comments on commit 2d452f2

Please sign in to comment.