Skip to content

Commit

Permalink
Update types.md (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
chitalian committed Mar 2, 2024
1 parent 978c412 commit c8843a2
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/pages/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,25 @@ export class Example extends OpenAPIRoute {
```

Read the Zod documentation [here](https://zod.dev/)!

## Generating Zod types for your requests

Often, you need to retrieve the request body while maintaining type definitions for that body. If you are using Zod for type validation, this becomes very straightforward!

Here's how you can create a TypeScript type for your Zod schema:
```ts
const Task = z.object({
name: z.string(),
description: z.string().or(z.string().array()),
steps: z.string().array(),
});
type MyTaskType = z.infer<typeof Task>;
```

Now, you can simply execute the following:

```
const body = request.json<MyTaskType>();
```

Your body is now correctly typed!

0 comments on commit c8843a2

Please sign in to comment.