Skip to content

Commit cd6c667

Browse files
authored
docs(zod-openapi): add docs for lacking proper Content-Type in a request (#893)
1 parent f30f7ee commit cd6c667

File tree

1 file changed

+71
-5
lines changed

1 file changed

+71
-5
lines changed

packages/zod-openapi/README.md

+71-5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const UserSchema = z
5252
```
5353

5454
> [!TIP]
55+
>
5556
> `UserSchema` schema will be registered as `"#/components/schemas/User"` refs in the OpenAPI document.
5657
> If you want to register the schema as referenced components, use `.openapi()` method.
5758
@@ -114,6 +115,71 @@ You can start your app just like you would with Hono. For Cloudflare Workers and
114115
export default app
115116
```
116117

118+
> [!IMPORTANT]
119+
> The request must have the proper `Content-Type` to ensure the validation. For example, if you want to validate a JSON body, the request must have the `Content-Type` to `application/json` in the request. Otherwise, the value of `c.req.valid('json')` will be `{}`.
120+
>
121+
> ```ts
122+
> import { createRoute, z, OpenAPIHono } from '@hono/zod-openapi'
123+
>
124+
> const route = createRoute({
125+
> method: 'post',
126+
> path: '/books',
127+
> request: {
128+
> body: {
129+
> content: {
130+
> 'application/json': {
131+
> schema: z.object({
132+
> title: z.string(),
133+
> }),
134+
> },
135+
> },
136+
> },
137+
> },
138+
> responses: {
139+
> 200: {
140+
> description: 'Success message',
141+
> },
142+
> },
143+
> })
144+
>
145+
> const app = new OpenAPIHono()
146+
>
147+
> app.openapi(route, (c) => {
148+
> const validatedBody = c.req.valid('json')
149+
> return c.json(validatedBody) // validatedBody is {}
150+
> })
151+
>
152+
> const res = await app.request('/books', {
153+
> method: 'POST',
154+
> body: JSON.stringify({ title: 'foo' }),
155+
> // The Content-Type header is lacking.
156+
> })
157+
>
158+
> const data = await res.json()
159+
> console.log(data) // {}
160+
> ```
161+
>
162+
> If you want to force validation of requests that do not have the proper `Content-Type`, set the value of `request.body.required` to `true`.
163+
>
164+
> ```ts
165+
> const route = createRoute({
166+
> method: 'post',
167+
> path: '/books',
168+
> request: {
169+
> body: {
170+
> content: {
171+
> 'application/json': {
172+
> schema: z.object({
173+
> title: z.string(),
174+
> }),
175+
> },
176+
> },
177+
> required: true, // <== add
178+
> },
179+
> },
180+
> })
181+
> ```
182+
117183
### Handling Validation Errors
118184
119185
Validation errors can be handled as follows:
@@ -241,7 +307,10 @@ You can generate OpenAPI v3.1 spec using the following methods:
241307

242308
```ts
243309
app.doc31('/docs', { openapi: '3.1.0', info: { title: 'foo', version: '1' } }) // new endpoint
244-
app.getOpenAPI31Document({ openapi: '3.1.0', info: { title: 'foo', version: '1' } }) // schema object
310+
app.getOpenAPI31Document({
311+
openapi: '3.1.0',
312+
info: { title: 'foo', version: '1' },
313+
}) // schema object
245314
```
246315

247316
### The Registry
@@ -285,10 +354,7 @@ const route = createRoute({
285354
request: {
286355
params: ParamsSchema,
287356
},
288-
middleware: [
289-
prettyJSON(),
290-
cache({ cacheName: 'my-cache' })
291-
] as const, // Use `as const` to ensure TypeScript infers the middleware's Context.
357+
middleware: [prettyJSON(), cache({ cacheName: 'my-cache' })] as const, // Use `as const` to ensure TypeScript infers the middleware's Context.
292358
responses: {
293359
200: {
294360
content: {

0 commit comments

Comments
 (0)