Skip to content

Commit

Permalink
fix(validator): support application/json with a charset (#3199)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Jul 28, 2024
1 parent 0417830 commit cdd48f4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/validator/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ describe('JSON', () => {
expect(res.status).toBe(200)
})

it('Should validate if Content-Type is a application/json with a charset', async () => {
const res = await app.request('http://localhost/post', {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf8',
},
body: JSON.stringify({ foo: 'bar' }),
})
expect(res.status).toBe(200)
expect(await res.json()).toEqual({ foo: 'bar' })
})

it('Should validate if Content-Type is a subtype like application/merge-patch+json', async () => {
const res = await app.request('http://localhost/post', {
method: 'POST',
Expand Down
4 changes: 2 additions & 2 deletions src/validator/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export type ValidationFunction<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ExcludeResponseType<T> = T extends Response & TypedResponse<any> ? never : T

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

export const validator = <
Expand Down

0 comments on commit cdd48f4

Please sign in to comment.