fix(validator): don't return a FormData if formData is cached (#3067)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(validator): don't return a FormData if formData is cached * fixed typo * test the value
authored by Yusuke Wada · committed by GitHub · parent 94f47ec · Browse files · 2 files·+55 added−23 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+55 added−23 removed
- tests:+30 added−0 removed
- other:+25 added−23 removed
Provenance
- Commit
2d452f2- Tree
a98dba3- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
94f47ec
2 files changed+55 added−23 removed
modified:src/validator/validator.test.ts+30 added
908 unmodified lines909 return c.text('foo')910 }911 )912 913+ app.post(914+ '/cached',915+ async (c, next) => {916+ await c.req.parseBody()917+ await next()918+ },919+ validator('form', (value) => {920+ if (value instanceof FormData) {921+ throw new Error('The value should not be a FormData')922+ }923+ return value924+ }),925+ (c) => {926+ const v = c.req.valid('form')927+ return c.json(v)928+ }929+ )930+931 it('Should not throw the error with c.req.parseBody()', async () => {932 const body = new FormData()933 body.append('foo', 'bar')934 const req = new Request('http://localhost', {2 unmodified lines937 })938 const res = await app.request(req)939 expect(res.status).toBe(200)940 })941+942+ it('Should not be an instance of FormData if the formData is cached', async () => {943+ const body = new FormData()944+ body.append('foo', 'bar')945+ const req = new Request('http://localhost/cached', {946+ method: 'POST',947+ body: body,948+ })949+ const res = await app.request(req)950+ expect(res.status).toBe(200)951+ expect(await res.json()).toEqual({ foo: 'bar' })952+ })953 })954 })955 956 describe('Async validator function', () => {112 unmodified linesmodified:src/validator/validator.ts+25 added-23 removed
81 unmodified lines828384858687888788899091929394959697989910010191929394959697989910010210310410510610710810110210310910510610710810911011111211011111211311411511611711827 unmodified lines81 unmodified lines if (!contentType) { 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) 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 } }) 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': value = Object.fromEntries(27 unmodified lines