-
-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(zod): add guide for
zod
(#1308)
* chore: fix format * docs: add guide for `zod` * chore: add `zod` routes * Update zod.md * Update zod.md --------- Co-authored-by: Melloware <[email protected]>
- Loading branch information
1 parent
03721e6
commit 39d9682
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- | ||
id: zod | ||
title: zod | ||
--- | ||
|
||
To create a `zod` schema, specify the client property as `zod`, and it will automatically generate in the target file. Ensure to configure `zod` properly in your project by referring to <a href="https://zod.dev/" target="_blank">Zod</a>. | ||
|
||
#### Example of orval.config.js | ||
|
||
```js | ||
module.exports = { | ||
petstore: { | ||
output: { | ||
client: 'zod', | ||
mode: 'single', | ||
target: './src/gen/zod', | ||
}, | ||
input: { | ||
target: './petstore.yaml', | ||
}, | ||
}, | ||
}; | ||
``` | ||
|
||
An implementation file will be created, containing a `zod` object for each schema in your OpenAPI Specification, as illustrated below: | ||
|
||
```ts | ||
export const createPetsBody = zod.object({ | ||
id: zod.number(), | ||
name: zod.string(), | ||
tag: zod.string().optional(), | ||
}); | ||
``` | ||
|
||
### How use generated `zod` object | ||
|
||
The `zod` object generated automatically can be utilized in the usual manner. | ||
|
||
```ts | ||
import type { z } from 'zod'; | ||
import { createPetsBody } from './src/gen/zod/swaggerPetstore.ts'; | ||
|
||
const pet = { id: 1, name: 'pet name', tag: 'tag' }; | ||
|
||
// parsing | ||
const parsedPet = createPetsBodyItem.parse(); | ||
console.log(parsedPet); | ||
// => Object { id: 1, name: "pet name", tag: "tag" } | ||
|
||
// inferred type | ||
type Pet = z.infer<typeof createPetsBodyItem>; | ||
console.log(pet as Pet); | ||
// => Object { id: 1, name: "pet name", tag: "tag" } | ||
``` |