fix(zod-openapi): supports `required` for JSON and Form body (#689)
* fix(zod-openapi): supports `required` for JSON and Form body * changesetpull/690/head
parent
2d14b1c8a4
commit
c3d4886800
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@hono/zod-openapi': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: supports `required` for JSON and Form body
|
|
@ -392,29 +392,37 @@ export class OpenAPIHono<
|
||||||
}
|
}
|
||||||
if (isJSONContentType(mediaType)) {
|
if (isJSONContentType(mediaType)) {
|
||||||
const validator = zValidator('json', schema, hook as any)
|
const validator = zValidator('json', schema, hook as any)
|
||||||
const mw: MiddlewareHandler = async (c, next) => {
|
if (route.request?.body?.required) {
|
||||||
if (c.req.header('content-type')) {
|
validators.push(validator)
|
||||||
if (isJSONContentType(c.req.header('content-type')!)) {
|
} else {
|
||||||
return await validator(c, next)
|
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', {})
|
validators.push(mw)
|
||||||
await next()
|
|
||||||
}
|
}
|
||||||
validators.push(mw)
|
|
||||||
}
|
}
|
||||||
if (isFormContentType(mediaType)) {
|
if (isFormContentType(mediaType)) {
|
||||||
const validator = zValidator('form', schema, hook as any)
|
const validator = zValidator('form', schema, hook as any)
|
||||||
const mw: MiddlewareHandler = async (c, next) => {
|
if (route.request?.body?.required) {
|
||||||
if (c.req.header('content-type')) {
|
validators.push(validator)
|
||||||
if (isFormContentType(c.req.header('content-type')!)) {
|
} else {
|
||||||
return await validator(c, next)
|
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', {})
|
validators.push(mw)
|
||||||
await next()
|
|
||||||
}
|
}
|
||||||
validators.push(mw)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -579,8 +579,41 @@ describe('JSON', () => {
|
||||||
expect(res.status).toBe(400)
|
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', () => {
|
describe('Form', () => {
|
||||||
const RequestSchema = z.object({
|
const RequestSchema = z.object({
|
||||||
id: z.string().openapi({}),
|
id: z.string().openapi({}),
|
||||||
|
@ -656,6 +689,39 @@ describe('Form', () => {
|
||||||
const res = await app.request(req)
|
const res = await app.request(req)
|
||||||
expect(res.status).toBe(200)
|
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', () => {
|
describe('JSON and Form', () => {
|
||||||
|
|
Loading…
Reference in New Issue