You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Previously, we generated a separate schema for each endpoint parameter and request body. In v0.74.0, a single request schema is generated for the whole endpoint. It may contain a request body, parameters, and headers.
10
+
11
+
```ts
12
+
const zData =z.object({
13
+
body: z.object({
14
+
foo: z.string().optional(),
15
+
bar: z.union([z.number(), z.null()]).optional(),
16
+
}).optional(),
17
+
headers: z.never().optional(),
18
+
path: z.object({
19
+
baz: z.string()
20
+
}),
21
+
query: z.never().optional()
22
+
});
23
+
```
24
+
25
+
If you need to access individual fields, you can do so using the [`.shape`](https://zod.dev/api?id=shape) API. For example, we can get the request body schema with `zData.shape.body`.
Copy file name to clipboardExpand all lines: docs/openapi-ts/migrating.md
+24Lines changed: 24 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -27,6 +27,30 @@ This config option is deprecated and will be removed in favor of [clients](./cli
27
27
28
28
This config option is deprecated and will be removed.
29
29
30
+
## v0.74.0
31
+
32
+
### Single Zod schema per request
33
+
34
+
Previously, we generated a separate schema for each endpoint parameter and request body. In v0.74.0, a single request schema is generated for the whole endpoint. It may contain a request body, parameters, and headers.
35
+
36
+
```ts
37
+
const zData =z.object({
38
+
body: z
39
+
.object({
40
+
foo: z.string().optional(),
41
+
bar: z.union([z.number(), z.null()]).optional(),
42
+
})
43
+
.optional(),
44
+
headers: z.never().optional(),
45
+
path: z.object({
46
+
baz: z.string(),
47
+
}),
48
+
query: z.never().optional(),
49
+
});
50
+
```
51
+
52
+
If you need to access individual fields, you can do so using the [`.shape`](https://zod.dev/api?id=shape) API. For example, we can get the request body schema with `zData.shape.body`.
Copy file name to clipboardExpand all lines: docs/openapi-ts/plugins/zod.md
+33-24Lines changed: 33 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ Launch demo
26
26
## Features
27
27
28
28
- seamless integration with `@hey-api/openapi-ts` ecosystem
29
-
- Zod schemas for request payloads, parameters, and responses
29
+
- Zod schemas for requests, responses, and reusable definitions
30
30
31
31
## Installation
32
32
@@ -66,6 +66,32 @@ export default {
66
66
67
67
The Zod plugin will generate the following artifacts, depending on the input specification.
68
68
69
+
## Requests
70
+
71
+
A single request schema is generated for each endpoint. It may contain a request body, parameters, and headers.
72
+
73
+
```ts
74
+
const zData =z.object({
75
+
body: z
76
+
.object({
77
+
foo: z.string().optional(),
78
+
bar: z.union([z.number(), z.null()]).optional(),
79
+
})
80
+
.optional(),
81
+
headers: z.never().optional(),
82
+
path: z.object({
83
+
baz: z.string(),
84
+
}),
85
+
query: z.never().optional(),
86
+
});
87
+
```
88
+
89
+
::: tip
90
+
If you need to access individual fields, you can do so using the [`.shape`](https://zod.dev/api?id=shape) API. For example, we can get the request body schema with `zData.shape.body`.
91
+
:::
92
+
93
+
You can customize the naming and casing pattern for requests using the `requests.name` and `requests.case` options.
94
+
69
95
## Responses
70
96
71
97
A single Zod schema is generated for all endpoint's responses. If the endpoint describes multiple responses, the generated schema is a union of all possible response shapes.
@@ -81,30 +107,11 @@ const zResponse = z.union([
81
107
]);
82
108
```
83
109
84
-
## Request Bodies
85
-
86
-
If an endpoint describes a request body, we will generate a Zod schema representing its shape.
87
-
88
-
```ts
89
-
const zData =z.object({
90
-
foo: z.string().optional(),
91
-
bar: z.union([z.number(), z.null()]).optional(),
92
-
});
93
-
```
94
-
95
-
## Parameters
110
+
You can customize the naming and casing pattern for responses using the `responses.name` and `responses.case` options.
96
111
97
-
A separate Zod schema is generated for every request parameter.
112
+
## Definitions
98
113
99
-
```ts
100
-
const zParameterFoo =z.number().int();
101
-
102
-
const zParameterBar =z.string();
103
-
```
104
-
105
-
## Schemas
106
-
107
-
A separate Zod schema is generated for every reusable schema.
114
+
A Zod schema is generated for every reusable definition from your input.
108
115
109
116
```ts
110
117
const zFoo =z.number().int();
@@ -114,9 +121,11 @@ const zBar = z.object({
114
121
});
115
122
```
116
123
124
+
You can customize the naming and casing pattern for definitions using the `definitions.name` and `definitions.case` options.
125
+
117
126
## Metadata
118
127
119
-
It's often useful to associate a schema with some additional metadata for documentation, code generation, AI structured outputs, form validation, and other purposes. If this is your use case, you can set `metadata` to `true` to generate additional metadata about schemas.
128
+
It's often useful to associate a schema with some additional [metadata](https://zod.dev/metadata) for documentation, code generation, AI structured outputs, form validation, and other purposes. If this is your use case, you can set `metadata` to `true` to generate additional metadata about schemas.
0 commit comments