Skip to content
Myr

feat(validator): check for json subtypes in validator (#2634)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* check for json subtypes in validator * formatting and deno_dist added * review fix, string#match -> regexp#test * format & lint

authored by Moritz Rebbert · committed by GitHub · parent f7faf38 · Browse files · 3 files+15 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+15 added−2 removed

  • tests:+13 added−0 removed
  • other:+2 added−2 removed

3 files+15 added−2 removed

modified:deno_dist/validator/validator.ts+1 added-1 removed
modified:src/validator/validator.test.ts+13 added
115 unmodified lines
116 })117 expect(res.status).toBe(400)118 })119120 it('Should return 200 response, for request with Content-Type which is a subtype like application/merge-patch+json', async () => {121 const res = await app.request('http://localhost/post', {122 method: 'POST',123 headers: {124 'Content-Type': 'application/merge-patch+json',125 },126 body: JSON.stringify({127 any: 'thing',128 }),129 })130 expect(res.status).toBe(200)131 })132133 it('Should return 400 response, if Content-Type header does not start with application/json', async () => {134 const res = await app.request('http://localhost/post', {135 method: 'POST',136 headers: {
892 unmodified lines
modified:src/validator/validator.ts+1 added-1 removed
65 unmodified lines
66 const contentType = c.req.header('Content-Type')6768 switch (target) {69 case 'json':70 if (!contentType || !contentType.startsWith('application/json')) {70 if (!contentType || !/^application\/([a-z-]+\+)?json/.test(contentType)) {71 const message = `Invalid HTTP header: Content-Type=${contentType}`72 throw new HTTPException(400, { message })73 }74 try {
69 unmodified lines