Skip to content

Commit c378dd9

Browse files
authored
fix(validator): Fixed a bug in hono/validator where URL Encoded Data could not be validated if the Content-Type included charset. (#3297)
1 parent 17c3b9e commit c378dd9

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/validator/validator.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,22 @@ describe('FormData', () => {
247247
})
248248
})
249249

250+
it('Should validate if Content-Type is a application/x-www-form-urlencoded with a charset', async () => {
251+
const params = new URLSearchParams()
252+
params.append('foo', 'bar')
253+
const res = await app.request('/post', {
254+
method: 'POST',
255+
body: params,
256+
headers: {
257+
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
258+
},
259+
})
260+
expect(res.status).toBe(200)
261+
expect(await res.json()).toEqual({
262+
foo: 'bar',
263+
})
264+
})
265+
250266
it('Should return `foo[]` as an array', async () => {
251267
const form = new FormData()
252268
form.append('foo[]', 'bar1')

src/validator/validator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type ExcludeResponseType<T> = T extends Response & TypedResponse<any> ? never :
2525

2626
const jsonRegex = /^application\/([a-z-\.]+\+)?json(;\s*[a-zA-Z0-9\-]+\=([^;]+))*$/
2727
const multipartRegex = /^multipart\/form-data(;\s?boundary=[a-zA-Z0-9'"()+_,\-./:=?]+)?$/
28-
const urlencodedRegex = /^application\/x-www-form-urlencoded$/
28+
const urlencodedRegex = /^application\/x-www-form-urlencoded(;\s*[a-zA-Z0-9\-]+\=([^;]+))*$/
2929

3030
export const validator = <
3131
InputType,

0 commit comments

Comments
 (0)