Skip to content

Commit

Permalink
fix(zod-openapi): supports required for JSON and Form body (#689)
Browse files Browse the repository at this point in the history
* fix(zod-openapi): supports `required` for JSON and Form body

* changeset
  • Loading branch information
yusukebe authored Aug 11, 2024
1 parent 2d14b1c commit c3d4886
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-singers-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---

fix: supports `required` for JSON and Form body
36 changes: 22 additions & 14 deletions packages/zod-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,29 +392,37 @@ export class OpenAPIHono<
}
if (isJSONContentType(mediaType)) {
const validator = zValidator('json', schema, hook as any)
const mw: MiddlewareHandler = async (c, next) => {
if (c.req.header('content-type')) {
if (isJSONContentType(c.req.header('content-type')!)) {
return await validator(c, next)
if (route.request?.body?.required) {
validators.push(validator)
} else {
const mw: MiddlewareHandler = async (c, next) => {
if (c.req.header('content-type')) {
if (isJSONContentType(c.req.header('content-type')!)) {
return await validator(c, next)
}
}
c.req.addValidatedData('json', {})
await next()
}
c.req.addValidatedData('json', {})
await next()
validators.push(mw)
}
validators.push(mw)
}
if (isFormContentType(mediaType)) {
const validator = zValidator('form', schema, hook as any)
const mw: MiddlewareHandler = async (c, next) => {
if (c.req.header('content-type')) {
if (isFormContentType(c.req.header('content-type')!)) {
return await validator(c, next)
if (route.request?.body?.required) {
validators.push(validator)
} else {
const mw: MiddlewareHandler = async (c, next) => {
if (c.req.header('content-type')) {
if (isFormContentType(c.req.header('content-type')!)) {
return await validator(c, next)
}
}
c.req.addValidatedData('form', {})
await next()
}
c.req.addValidatedData('form', {})
await next()
validators.push(mw)
}
validators.push(mw)
}
}
}
Expand Down
68 changes: 67 additions & 1 deletion packages/zod-openapi/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,41 @@ describe('JSON', () => {
expect(res.status).toBe(400)
})
})

describe('required', () => {
const route = createRoute({
method: 'post',
path: '/posts',
request: {
body: {
content: {
'application/json': {
schema: RequestSchema,
},
},
required: true,
},
},
responses: {
200: {
description: 'Post a post',
},
},
})
const app = new OpenAPIHono()
app.openapi(route, (c) => {
return c.text('Post success')
})

it('Should return 400 response since body is required', async () => {
const res = await app.request('/posts', {
method: 'POST',
})
expect(res.status).toBe(400)
})
})
})
// application/vnd.api+json

describe('Form', () => {
const RequestSchema = z.object({
id: z.string().openapi({}),
Expand Down Expand Up @@ -656,6 +689,39 @@ describe('Form', () => {
const res = await app.request(req)
expect(res.status).toBe(200)
})

describe('required', () => {
const route = createRoute({
method: 'post',
path: '/posts',
request: {
body: {
content: {
'application/x-www-form-urlencoded': {
schema: RequestSchema,
},
},
required: true,
},
},
responses: {
200: {
description: 'Post a post',
},
},
})
const app = new OpenAPIHono()
app.openapi(route, (c) => {
return c.text('Post success')
})

it('Should return 400 response since body is required', async () => {
const res = await app.request('/posts', {
method: 'POST',
})
expect(res.status).toBe(400)
})
})
})

describe('JSON and Form', () => {
Expand Down

0 comments on commit c3d4886

Please sign in to comment.