Skip to content

Commit

Permalink
fix(validator): Allow form data will mutliple values appended
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksrandall committed Aug 15, 2024
1 parent 986db29 commit 434f13e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/validator/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ import type { ValidationFunction } from './validator'
import { validator } from './validator'

// Reference implementation for only testing
const zodValidator = <
T extends ZodSchema,
E extends {},
P extends string,
Target extends keyof ValidationTargets
>(
const zodValidator = <T extends ZodSchema, E extends {}, P extends string>(
target: Target,

Check failure on line 20 in src/validator/validator.test.ts

View workflow job for this annotation

GitHub Actions / Main

Cannot find name 'Target'.
schema: T
): MiddlewareHandler<
Expand Down Expand Up @@ -260,6 +255,20 @@ describe('FormData', () => {
'foo[]': ['bar1', 'bar2'],
})
})

it('Should return `foo` as an array if multiple values are appended', async () => {
const form = new FormData()
form.append('foo', 'bar1')
form.append('foo', 'bar2')
const res = await app.request('/post', {
method: 'POST',
body: form,
})
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
foo: ['bar1', 'bar2'],
})
})
})

describe('Malformed FormData request', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/validator/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ export const validator = <
form[key] = [value]
} else if (Array.isArray(form[key])) {
;(form[key] as unknown[]).push(value)
} else if (key in form) {
form[key] = [form[key] as string | File, value]
}
} else {
form[key] = value
Expand Down

0 comments on commit 434f13e

Please sign in to comment.