zod-openapi 5.0.0-beta.9 → 5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +299 -63
- package/dist/api.d.mts +2 -11
- package/dist/api.d.ts +2 -11
- package/dist/api.js +1 -2
- package/dist/api.mjs +2 -2
- package/dist/{components-26wns3Hd.d.mts → components-CCPXuBcU.d.mts} +187 -91
- package/dist/components-CUMu4QeI.mjs +900 -0
- package/dist/{components-CokUEQbL.d.ts → components-D8Br4Oqx.d.ts} +187 -91
- package/dist/components-DpACv95L.js +958 -0
- package/dist/index.d.mts +11 -3
- package/dist/index.d.ts +11 -3
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +22 -19
- package/dist/components-0QHfGwg0.mjs +0 -791
- package/dist/components-CB3cj8u3.js +0 -855
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
<h1 align="center">zod-openapi</h1>
|
|
4
4
|
</p>
|
|
5
5
|
<p align="center">
|
|
6
|
-
A TypeScript library which uses <a href="https://github.com/colinhacks/zod">Zod</a> schemas to generate OpenAPI v3.
|
|
6
|
+
A TypeScript library which uses <a href="https://github.com/colinhacks/zod">Zod</a> schemas to generate OpenAPI v3.1x documentation.
|
|
7
7
|
</p>
|
|
8
8
|
<div align="center">
|
|
9
9
|
<a href="https://www.npmjs.com/package/zod-openapi"><img src="https://img.shields.io/npm/v/zod-openapi"/></a>
|
|
@@ -31,33 +31,93 @@ pnpm install zod zod-openapi
|
|
|
31
31
|
|
|
32
32
|
### `.meta()`
|
|
33
33
|
|
|
34
|
-
Use the `.meta()` method to add metadata to a Zod schema. It accepts an object with the following options:
|
|
34
|
+
Use the `.meta()` method to add OpenAPI metadata to a Zod schema. It accepts an object with the following options:
|
|
35
35
|
|
|
36
36
|
| Option | Description |
|
|
37
37
|
| ---------- | ---------------------------------------------------------------------------------------------------------------- |
|
|
38
38
|
| `id` | Registers a schema as a reusable OpenAPI component. |
|
|
39
39
|
| `header` | Adds metadata for [response headers](#response-headers). |
|
|
40
40
|
| `param` | Adds metadata for [request parameters](#parameters). |
|
|
41
|
-
| `override` | Allows you to override the rendered OpenAPI schema. This takes either an object or a function
|
|
41
|
+
| `override` | Allows you to override the rendered OpenAPI schema. This takes either an object or a function. |
|
|
42
42
|
| `outputId` | Allows you to set a different ID for the output schema. This is useful when the input and output schemas differ. |
|
|
43
|
-
| `unusedIO` | Allows you to set the `io` for an unused schema added to the components section. Defaults to `output
|
|
43
|
+
| `unusedIO` | Allows you to set the `io` for an unused schema added to the components section. Defaults to `output`. |
|
|
44
|
+
|
|
45
|
+
You can also set standard OpenAPI properties directly in the `.meta()` method, such as:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
z.string().meta({
|
|
49
|
+
description: 'A text field',
|
|
50
|
+
example: 'Example value',
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
You can set additional metadata to the rendered schema in a few different ways. Zod's `.meta()` method allows you to directly set OpenAPI directly to the schema. However, if you wanted to override any field that Zod generates, this may not work as expected. In this case, you can use the `override` option to customize the rendered OpenAPI schema.
|
|
55
|
+
|
|
56
|
+
eg.
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
z.string().datetime().meta({
|
|
60
|
+
description: 'A date field',
|
|
61
|
+
format: 'MY-FORMAT',
|
|
62
|
+
'x-custom-field': 'custom value',
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Would render the following OpenAPI schema. Note that format is not overridden with our custom format value.
|
|
67
|
+
|
|
68
|
+
```json
|
|
69
|
+
{
|
|
70
|
+
"type": "string",
|
|
71
|
+
"format": "date-time",
|
|
72
|
+
"description": "A date field",
|
|
73
|
+
"x-custom-field": "custom value"
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
In order to override fields which are generated by Zod or this library, you can use the `override` option in the `.meta()` method. This allows you to customize the rendered OpenAPI schema after we have both processed it.
|
|
78
|
+
|
|
79
|
+
##### override object
|
|
80
|
+
|
|
81
|
+
This does a naive merge of the override object with the rendered OpenAPI schema. This is useful for simple overrides where you want to add or modify properties without complex logic.
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
z.string.datetime().meta({
|
|
85
|
+
description: 'A date field',
|
|
86
|
+
override: {
|
|
87
|
+
format: 'MY-FORMAT',
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
##### override function
|
|
93
|
+
|
|
94
|
+
The `override` function allows you to deeply customize the rendered OpenAPI schema. Eg. if you wanted to render a Zod union as a `oneOf` instead of `anyOf` for a single schema, you can do so by using the `override` function. View the documentation for `override` in the [CreateDocumentOptions](#createdocumentoptions) section for more information.
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
z.union([z.string(), z.number()]).meta({
|
|
98
|
+
description: 'A union of string and number',
|
|
99
|
+
override: ({ jsonSchema }) => {
|
|
100
|
+
jsonSchema.anyOf = jsonSchema.oneOf;
|
|
101
|
+
delete jsonSchema.oneOf;
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
```
|
|
44
105
|
|
|
45
106
|
### `createDocument`
|
|
46
107
|
|
|
47
108
|
Generates an OpenAPI documentation object.
|
|
48
109
|
|
|
49
110
|
```typescript
|
|
50
|
-
import 'zod
|
|
51
|
-
import { z } from 'zod/v4';
|
|
111
|
+
import * as z from 'zod/v4';
|
|
52
112
|
import { createDocument } from 'zod-openapi';
|
|
53
113
|
|
|
54
|
-
const jobId = z.string().
|
|
114
|
+
const jobId = z.string().meta({
|
|
55
115
|
description: 'A unique identifier for a job',
|
|
56
116
|
example: '12345',
|
|
57
117
|
id: 'jobId',
|
|
58
118
|
});
|
|
59
119
|
|
|
60
|
-
const title = z.string().
|
|
120
|
+
const title = z.string().meta({
|
|
61
121
|
description: 'Job title',
|
|
62
122
|
example: 'My job',
|
|
63
123
|
});
|
|
@@ -170,37 +230,109 @@ const document = createDocument({
|
|
|
170
230
|
```
|
|
171
231
|
</details>
|
|
172
232
|
|
|
233
|
+
`createDocument` takes an optional options argument which can be used to modify how the document is created
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
createDocument(doc, {
|
|
237
|
+
override: ({ jsonSchema, zodSchema, io }) => {
|
|
238
|
+
const def = zodSchema._zod.def;
|
|
239
|
+
if (def.type === 'date' && io === 'output') {
|
|
240
|
+
jsonSchema.type = 'string';
|
|
241
|
+
jsonSchema.format = 'date-time';
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
allowEmptySchema: {
|
|
245
|
+
custom: true,
|
|
246
|
+
},
|
|
247
|
+
});
|
|
248
|
+
```
|
|
249
|
+
|
|
173
250
|
#### CreateDocumentOptions
|
|
174
251
|
|
|
175
|
-
|
|
252
|
+
| Option | Type | Description |
|
|
253
|
+
| ------------------ | ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
|
|
254
|
+
| `override` | `Function` | Override rendered schema with a function`` |
|
|
255
|
+
| `outputIdSuffix` | `string` | Suffix for output schema IDs when the schema is used in both a request and response. Defaults to ``'Output'` |
|
|
256
|
+
| `allowEmptySchema` | `Object` | Control whether empty schemas are allowed for specific Zod schema types |
|
|
257
|
+
| `cycles` | `'ref' \| 'throw'` | How to handle cycles in schemas.<br>- `'ref'` — Break cycles using $defs<br>- `'throw'` — Error on cycles. Defaults to `'ref'` |
|
|
258
|
+
| `reused` | `'ref' \| 'inline'` | How to handle reused schemas.<br>- `'ref'` — Reused schemas as references<br>- `'inline'` — Inline reused schemas. Defaults to `'inline'` |
|
|
259
|
+
| |
|
|
260
|
+
|
|
261
|
+
##### `override`
|
|
262
|
+
|
|
263
|
+
The `override` function allows you to customize the rendered OpenAPI schema. It receives an object with the following properties:
|
|
264
|
+
|
|
265
|
+
| Property | Type | Description |
|
|
266
|
+
| ------------ | --------------------- | ----------------------------------------------------------------------------------------------------------------- |
|
|
267
|
+
| `jsonSchema` | `Object` | The OpenAPI schema object being generated. You can modify this object to change the rendered schema. |
|
|
268
|
+
| `zodSchema` | `ZodType` | The original Zod schema being converted to OpenAPI. You can use this to access Zod-specific properties. |
|
|
269
|
+
| `io` | `'input' \| 'output'` | The context in which the schema is being rendered. `'input'` for request bodies/params, `'output'` for responses. |
|
|
270
|
+
|
|
271
|
+
This allows you to transform the schema based on your own rules or requirements. For example, if you
|
|
272
|
+
|
|
273
|
+
1. Wanted to change how dates are rendered in the output context because a typical JSON serializer will transform them to a string
|
|
274
|
+
2. Change all union outputs to be oneOf instead of anyOf
|
|
176
275
|
|
|
177
276
|
```typescript
|
|
178
|
-
|
|
179
|
-
override: ({ jsonSchema, zodSchema }) => {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
277
|
+
createDocument(doc, {
|
|
278
|
+
override: ({ jsonSchema, zodSchema, io }) => {
|
|
279
|
+
const def = zodSchema._zod.def;
|
|
280
|
+
if (def.type === 'date' && io === 'output') {
|
|
281
|
+
jsonSchema.type = 'string';
|
|
282
|
+
jsonSchema.format = 'date-time';
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
if (def.type === 'union') {
|
|
286
|
+
jsonSchema.oneOf = jsonSchema.anyOf;
|
|
287
|
+
delete jsonSchema.anyOf;
|
|
288
|
+
return;
|
|
183
289
|
}
|
|
184
290
|
},
|
|
185
291
|
});
|
|
186
292
|
```
|
|
187
293
|
|
|
294
|
+
##### `outputIdSuffix`
|
|
295
|
+
|
|
296
|
+
The `outputIdSuffix` option allows you to set a suffix for output schema IDs when the schema is used in both a request and response context. This is useful for distinguishing between input and output schemas.
|
|
297
|
+
|
|
298
|
+
##### `allowEmptySchema`
|
|
299
|
+
|
|
300
|
+
The `allowEmptySchema` option allows you to control whether or not this library will throw when it encounters an empty schema. By default this library will not throw if it encounters an `z.any()` or `z.unknown()` schema.
|
|
301
|
+
|
|
302
|
+
This allows you to customize how this library handles [unrepresentable](https://zod.dev/json-schema?id=unrepresentable) Zod schemas.
|
|
303
|
+
|
|
304
|
+
eg. If you wanted to allow `z.custom()` to be rendered as an empty schema, you can set the `allowEmptySchema` option as follows:
|
|
305
|
+
|
|
306
|
+
```typescript
|
|
307
|
+
createDocument(doc, {
|
|
308
|
+
allowEmptySchema: {
|
|
309
|
+
custom: true, // Allow empty schemas for `z.custom()` in all contexts
|
|
310
|
+
set: { output: true }, // Allow empty schemas for `z.set()` only in an output context
|
|
311
|
+
},
|
|
312
|
+
});
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
##### `cycles` and `reused`
|
|
316
|
+
|
|
317
|
+
These options are exposed directly from the Zod. Read the [documentation](https://zod.dev/json-schema?id=unrepresentable#cycles) for more information on how to use these options.
|
|
318
|
+
|
|
319
|
+
#####
|
|
320
|
+
|
|
188
321
|
### `createSchema`
|
|
189
322
|
|
|
190
323
|
Creates an OpenAPI Schema Object along with any registered components. OpenAPI 3.1.0 Schema Objects are fully compatible with JSON Schema.
|
|
191
324
|
|
|
192
325
|
```typescript
|
|
193
|
-
import 'zod
|
|
194
|
-
import { z } from 'zod/v4';
|
|
326
|
+
import * as z from 'zod/v4';
|
|
195
327
|
import { createSchema } from 'zod-openapi';
|
|
196
328
|
|
|
197
|
-
const jobId = z.string().
|
|
329
|
+
const jobId = z.string().meta({
|
|
198
330
|
description: 'A unique identifier for a job',
|
|
199
331
|
example: '12345',
|
|
200
332
|
id: 'jobId',
|
|
201
333
|
});
|
|
202
334
|
|
|
203
|
-
const title = z.string().
|
|
335
|
+
const title = z.string().meta({
|
|
204
336
|
description: 'Job title',
|
|
205
337
|
example: 'My job',
|
|
206
338
|
});
|
|
@@ -245,15 +377,17 @@ const { schema, components } = createSchema(job);
|
|
|
245
377
|
|
|
246
378
|
#### CreateSchemaOptions
|
|
247
379
|
|
|
248
|
-
`createSchema` takes an optional `CreateSchemaOptions` parameter which
|
|
380
|
+
`createSchema` takes an optional `CreateSchemaOptions` parameter which includes all options from [CreateDocumentOptions](#createdocumentoptions) plus the following:
|
|
249
381
|
|
|
250
382
|
```typescript
|
|
251
383
|
const { schema, components } = createSchema(job, {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
384
|
+
// Input/Output context - controls how schemas are generated
|
|
385
|
+
io: 'input', // 'input' for request bodies/params, 'output' for responses
|
|
386
|
+
// Component handling
|
|
387
|
+
schemaComponents: { jobId: z.string() }, // Pre-defined components to use
|
|
388
|
+
schemaComponentRefPath: '#/definitions/', // Custom path prefix for component references
|
|
389
|
+
opts: {}, // Create Document Options,
|
|
390
|
+
});
|
|
257
391
|
```
|
|
258
392
|
|
|
259
393
|
### Request Parameters
|
|
@@ -285,7 +419,7 @@ createDocument({
|
|
|
285
419
|
'/jobs/{a}': {
|
|
286
420
|
put: {
|
|
287
421
|
parameters: [
|
|
288
|
-
z.string().
|
|
422
|
+
z.string().meta({
|
|
289
423
|
param: {
|
|
290
424
|
name: 'job-header',
|
|
291
425
|
in: 'header',
|
|
@@ -396,7 +530,7 @@ If we take the example in `createDocument` and instead create `title` as follows
|
|
|
396
530
|
##### Auto Registering Schema
|
|
397
531
|
|
|
398
532
|
```typescript
|
|
399
|
-
const title = z.string().
|
|
533
|
+
const title = z.string().meta({
|
|
400
534
|
description: 'Job title',
|
|
401
535
|
example: 'My job',
|
|
402
536
|
id: 'jobTitle', // <- new field
|
|
@@ -434,14 +568,14 @@ Another way to register schema instead of adding a `ref` is to add it to the com
|
|
|
434
568
|
eg.
|
|
435
569
|
|
|
436
570
|
```typescript
|
|
437
|
-
const title = z.string().
|
|
571
|
+
const title = z.string().meta({
|
|
438
572
|
description: 'Job title',
|
|
439
573
|
example: 'My job',
|
|
440
574
|
});
|
|
441
575
|
createDocument({
|
|
442
576
|
components: {
|
|
443
577
|
schemas: {
|
|
444
|
-
jobTitle: title, // this will register this Zod Schema as jobTitle unless `
|
|
578
|
+
jobTitle: title, // this will register this Zod Schema as jobTitle unless `id` in `.meta()` is specified on the type
|
|
445
579
|
},
|
|
446
580
|
},
|
|
447
581
|
});
|
|
@@ -453,7 +587,7 @@ Query, Path, Header & Cookie parameters can be similarly registered:
|
|
|
453
587
|
|
|
454
588
|
```typescript
|
|
455
589
|
// Easy auto registration
|
|
456
|
-
const jobId = z.string().
|
|
590
|
+
const jobId = z.string().meta({
|
|
457
591
|
description: 'Job ID',
|
|
458
592
|
example: '1234',
|
|
459
593
|
param: { id: 'jobRef' },
|
|
@@ -474,7 +608,7 @@ createDocument({
|
|
|
474
608
|
});
|
|
475
609
|
|
|
476
610
|
// or more verbose auto registration
|
|
477
|
-
const jobId = z.string().
|
|
611
|
+
const jobId = z.string().meta({
|
|
478
612
|
description: 'Job ID',
|
|
479
613
|
example: '1234',
|
|
480
614
|
param: { in: 'header', name: 'jobId', id: 'jobRef' },
|
|
@@ -490,8 +624,8 @@ createDocument({
|
|
|
490
624
|
},
|
|
491
625
|
});
|
|
492
626
|
|
|
493
|
-
// or manual
|
|
494
|
-
const otherJobId = z.string().
|
|
627
|
+
// or manual registration
|
|
628
|
+
const otherJobId = z.string().meta({
|
|
495
629
|
description: 'Job ID',
|
|
496
630
|
example: '1234',
|
|
497
631
|
param: { in: 'header', name: 'jobId' },
|
|
@@ -511,7 +645,7 @@ createDocument({
|
|
|
511
645
|
Response headers can be similarly registered:
|
|
512
646
|
|
|
513
647
|
```typescript
|
|
514
|
-
const header = z.string().
|
|
648
|
+
const header = z.string().meta({
|
|
515
649
|
description: 'Job ID',
|
|
516
650
|
example: '1234',
|
|
517
651
|
header: { id: 'some-header' },
|
|
@@ -519,7 +653,7 @@ const header = z.string().openapi({
|
|
|
519
653
|
|
|
520
654
|
// or
|
|
521
655
|
|
|
522
|
-
const jobIdHeader = z.string().
|
|
656
|
+
const jobIdHeader = z.string().meta({
|
|
523
657
|
description: 'Job ID',
|
|
524
658
|
example: '1234',
|
|
525
659
|
});
|
|
@@ -615,6 +749,121 @@ createDocument({
|
|
|
615
749
|
});
|
|
616
750
|
```
|
|
617
751
|
|
|
752
|
+
#### Path Items
|
|
753
|
+
|
|
754
|
+
Path Items can also be registered
|
|
755
|
+
|
|
756
|
+
```typescript
|
|
757
|
+
const pathItem: ZodOpenApiPathItemObject = {
|
|
758
|
+
id: 'some-path-item',
|
|
759
|
+
get: {
|
|
760
|
+
responses: {
|
|
761
|
+
200: {
|
|
762
|
+
description: '200 OK',
|
|
763
|
+
content: {
|
|
764
|
+
'application/json': {
|
|
765
|
+
schema: z.object({ a: z.string() }),
|
|
766
|
+
},
|
|
767
|
+
},
|
|
768
|
+
},
|
|
769
|
+
},
|
|
770
|
+
},
|
|
771
|
+
};
|
|
772
|
+
|
|
773
|
+
// or
|
|
774
|
+
|
|
775
|
+
createDocument({
|
|
776
|
+
components: {
|
|
777
|
+
pathItems: {
|
|
778
|
+
'some-path-item': pathItem,
|
|
779
|
+
},
|
|
780
|
+
},
|
|
781
|
+
});
|
|
782
|
+
```
|
|
783
|
+
|
|
784
|
+
#### Security Schemes
|
|
785
|
+
|
|
786
|
+
Security Schemes can be registered for authentication methods:
|
|
787
|
+
|
|
788
|
+
```typescript
|
|
789
|
+
createDocument({
|
|
790
|
+
components: {
|
|
791
|
+
securitySchemes: {
|
|
792
|
+
bearerAuth: {
|
|
793
|
+
type: 'http',
|
|
794
|
+
scheme: 'bearer',
|
|
795
|
+
bearerFormat: 'JWT',
|
|
796
|
+
description: 'JWT Authentication',
|
|
797
|
+
},
|
|
798
|
+
},
|
|
799
|
+
},
|
|
800
|
+
});
|
|
801
|
+
```
|
|
802
|
+
|
|
803
|
+
#### Links
|
|
804
|
+
|
|
805
|
+
Links can be registered to describe relationships between operations:
|
|
806
|
+
|
|
807
|
+
```typescript
|
|
808
|
+
const link: ZodOpenApiLinkObject = {
|
|
809
|
+
id: 'getUserById',
|
|
810
|
+
operationId: 'getUser',
|
|
811
|
+
parameters: {
|
|
812
|
+
userId: '$request.path.id',
|
|
813
|
+
},
|
|
814
|
+
description: 'Link to get user by id',
|
|
815
|
+
};
|
|
816
|
+
|
|
817
|
+
// or
|
|
818
|
+
|
|
819
|
+
createDocument({
|
|
820
|
+
components: {
|
|
821
|
+
links: {
|
|
822
|
+
getUserById: {
|
|
823
|
+
operationId: 'getUser',
|
|
824
|
+
parameters: {
|
|
825
|
+
userId: '$request.path.id',
|
|
826
|
+
},
|
|
827
|
+
description: 'Link to get user by id',
|
|
828
|
+
},
|
|
829
|
+
},
|
|
830
|
+
},
|
|
831
|
+
});
|
|
832
|
+
```
|
|
833
|
+
|
|
834
|
+
#### Examples
|
|
835
|
+
|
|
836
|
+
Examples can be registered to provide sample values for schemas:
|
|
837
|
+
|
|
838
|
+
```typescript
|
|
839
|
+
const example: ZodOpenApiExampleObject = {
|
|
840
|
+
id: 'userExample',
|
|
841
|
+
summary: 'A sample user',
|
|
842
|
+
value: {
|
|
843
|
+
id: '123',
|
|
844
|
+
name: 'Jane Doe',
|
|
845
|
+
email: 'jane@example.com',
|
|
846
|
+
},
|
|
847
|
+
};
|
|
848
|
+
|
|
849
|
+
// or
|
|
850
|
+
|
|
851
|
+
createDocument({
|
|
852
|
+
components: {
|
|
853
|
+
examples: {
|
|
854
|
+
userExample: {
|
|
855
|
+
summary: 'A sample user',
|
|
856
|
+
value: {
|
|
857
|
+
id: '123',
|
|
858
|
+
name: 'Jane Doe',
|
|
859
|
+
email: 'jane@example.com',
|
|
860
|
+
},
|
|
861
|
+
},
|
|
862
|
+
},
|
|
863
|
+
},
|
|
864
|
+
});
|
|
865
|
+
```
|
|
866
|
+
|
|
618
867
|
### Zod Types
|
|
619
868
|
|
|
620
869
|
Zod types are composed of two different parts: the input and the output. This library decides which type to create based on if it is used in a request or response context.
|
|
@@ -631,13 +880,13 @@ Output:
|
|
|
631
880
|
|
|
632
881
|
In general, you want to avoid using a registered input schema in an output context and vice versa. This is because the rendered input and output schemas of a simple Zod schema will differ, even with a simple Zod schema like `z.object()`.
|
|
633
882
|
|
|
634
|
-
```
|
|
883
|
+
```typescript
|
|
635
884
|
const schema = z.object({
|
|
636
885
|
name: z.string(),
|
|
637
886
|
});
|
|
638
887
|
```
|
|
639
888
|
|
|
640
|
-
Input:
|
|
889
|
+
Input schemas (request bodies, parameters):
|
|
641
890
|
|
|
642
891
|
```json
|
|
643
892
|
{
|
|
@@ -651,7 +900,7 @@ Input:
|
|
|
651
900
|
}
|
|
652
901
|
```
|
|
653
902
|
|
|
654
|
-
Output:
|
|
903
|
+
Output schemas (responses):
|
|
655
904
|
|
|
656
905
|
```json
|
|
657
906
|
{
|
|
@@ -666,25 +915,30 @@ Output:
|
|
|
666
915
|
}
|
|
667
916
|
```
|
|
668
917
|
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
If the schemas are not equal, it will automatically handle this by outputting the `output` schema with an `Output` suffix.
|
|
918
|
+
When the same schema is referenced in both input and output contexts, the library generates two separate component schemas. This happens automatically when a schema with an ID is used in both contexts.
|
|
672
919
|
|
|
673
|
-
You can
|
|
920
|
+
You can customize the output schema name by providing an `outputId`:
|
|
674
921
|
|
|
675
922
|
```ts
|
|
676
923
|
const schema = z
|
|
677
924
|
.object({
|
|
678
925
|
name: z.string(),
|
|
679
926
|
})
|
|
680
|
-
.meta({
|
|
927
|
+
.meta({
|
|
928
|
+
id: 'MyObject',
|
|
929
|
+
outputId: 'MyObjectResponse', // Customize the output schema name
|
|
930
|
+
});
|
|
681
931
|
```
|
|
682
932
|
|
|
933
|
+
You can also set a global suffix for output schemas or use `z.looseObject()` and `z.strictObject()` to have explicit control over the schema behavior.
|
|
934
|
+
|
|
935
|
+
> **⚠️ Note:** If your registered schema contains dynamically created lazy components, they won't be reused between input and output schemas.
|
|
936
|
+
|
|
683
937
|
## Supported OpenAPI Versions
|
|
684
938
|
|
|
685
939
|
Currently the following versions of OpenAPI are supported
|
|
686
940
|
|
|
687
|
-
- `3.1.0`
|
|
941
|
+
- `3.1.0` (minimum version)
|
|
688
942
|
- `3.1.1`
|
|
689
943
|
|
|
690
944
|
Setting the `openapi` field will change how the some of the components are rendered.
|
|
@@ -726,9 +980,9 @@ See the library in use in the [examples](./examples/) folder.
|
|
|
726
980
|
|
|
727
981
|
- [eslint-plugin-zod-openapi](https://github.com/samchungy/eslint-plugin-zod-openapi) - Eslint rules for zod-openapi. This includes features which can autogenerate Typescript comments for your Zod types based on your `description`, `example` and `deprecated` fields.
|
|
728
982
|
|
|
729
|
-
##
|
|
983
|
+
## Version Information
|
|
730
984
|
|
|
731
|
-
|
|
985
|
+
For information about changes and migration from v4 to v5, see the [v5 migration guide](./docs/v5.md).
|
|
732
986
|
|
|
733
987
|
## Development
|
|
734
988
|
|
|
@@ -757,21 +1011,3 @@ pnpm format
|
|
|
757
1011
|
# Check for issues
|
|
758
1012
|
pnpm lint
|
|
759
1013
|
```
|
|
760
|
-
|
|
761
|
-
### Release
|
|
762
|
-
|
|
763
|
-
To release a new version
|
|
764
|
-
|
|
765
|
-
1. Create a [new GitHub Release](https://github.com/samchungy/zod-openapi/releases/new)
|
|
766
|
-
2. Select `🏷️ Choose a tag`, enter a version number. eg. `v1.2.0` and click `+ Create new tag: vX.X.X on publish`.
|
|
767
|
-
3. Click the `Generate release notes` button and adjust the description.
|
|
768
|
-
4. Tick the `Set as the latest release` box and click `Publish release`. This will trigger the `Release` workflow.
|
|
769
|
-
5. Check the `Pull Requests` tab for a PR labelled `Release vX.X.X`.
|
|
770
|
-
6. Click `Merge Pull Request` on that Pull Request to update master with the new package version.
|
|
771
|
-
|
|
772
|
-
To release a new beta version
|
|
773
|
-
|
|
774
|
-
1. Create a [new GitHub Release](https://github.com/samchungy/zod-openapi/releases/new)
|
|
775
|
-
2. Select `🏷️ Choose a tag`, enter a version number with a `-beta.X` suffix eg. `v1.2.0-beta.1` and click `+ Create new tag: vX.X.X-beta.X on publish`.
|
|
776
|
-
3. Click the `Generate release notes` button and adjust the description.
|
|
777
|
-
4. Tick the `Set as a pre-release` box and click `Publish release`. This will trigger the `Prerelease` workflow.
|
package/dist/api.d.mts
CHANGED
|
@@ -1,20 +1,11 @@
|
|
|
1
|
-
import { ComponentRegistry, Override,
|
|
1
|
+
import { ComponentRegistry, Override, ZodOpenApiBaseMetadata, ZodOpenApiMetadata, createComponents, createRegistry } from "./components-CCPXuBcU.mjs";
|
|
2
2
|
import { $ZodObject, $ZodType, $ZodTypes } from "zod/v4/core";
|
|
3
3
|
import { core } from "zod/v4";
|
|
4
4
|
|
|
5
|
-
//#region src/create/parameters.d.ts
|
|
6
|
-
declare const createParameter: (parameter: $ZodType, location: {
|
|
7
|
-
in: ParameterLocation;
|
|
8
|
-
name: string;
|
|
9
|
-
} | undefined, ctx: {
|
|
10
|
-
registry: ComponentRegistry;
|
|
11
|
-
io: "input" | "output";
|
|
12
|
-
}, path: string[]) => ParameterObject | ReferenceObject;
|
|
13
|
-
//#endregion
|
|
14
5
|
//#region src/create/object.d.ts
|
|
15
6
|
declare const unwrapZodObject: (zodType: $ZodTypes, io: "input" | "output", path: string[]) => $ZodObject;
|
|
16
7
|
//#endregion
|
|
17
8
|
//#region src/zod.d.ts
|
|
18
9
|
declare const isAnyZodType: (schema: unknown) => schema is core.$ZodTypes;
|
|
19
10
|
//#endregion
|
|
20
|
-
export { ComponentRegistry, Override,
|
|
11
|
+
export { ComponentRegistry, Override, ZodOpenApiBaseMetadata, ZodOpenApiMetadata, createComponents, createRegistry, isAnyZodType, unwrapZodObject };
|
package/dist/api.d.ts
CHANGED
|
@@ -1,20 +1,11 @@
|
|
|
1
|
-
import { ComponentRegistry, Override,
|
|
1
|
+
import { ComponentRegistry, Override, ZodOpenApiBaseMetadata, ZodOpenApiMetadata, createComponents, createRegistry } from "./components-D8Br4Oqx.js";
|
|
2
2
|
import { $ZodObject, $ZodType, $ZodTypes } from "zod/v4/core";
|
|
3
3
|
import { core } from "zod/v4";
|
|
4
4
|
|
|
5
|
-
//#region src/create/parameters.d.ts
|
|
6
|
-
declare const createParameter: (parameter: $ZodType, location: {
|
|
7
|
-
in: ParameterLocation;
|
|
8
|
-
name: string;
|
|
9
|
-
} | undefined, ctx: {
|
|
10
|
-
registry: ComponentRegistry;
|
|
11
|
-
io: "input" | "output";
|
|
12
|
-
}, path: string[]) => ParameterObject | ReferenceObject;
|
|
13
|
-
//#endregion
|
|
14
5
|
//#region src/create/object.d.ts
|
|
15
6
|
declare const unwrapZodObject: (zodType: $ZodTypes, io: "input" | "output", path: string[]) => $ZodObject;
|
|
16
7
|
//#endregion
|
|
17
8
|
//#region src/zod.d.ts
|
|
18
9
|
declare const isAnyZodType: (schema: unknown) => schema is core.$ZodTypes;
|
|
19
10
|
//#endregion
|
|
20
|
-
export { ComponentRegistry, Override,
|
|
11
|
+
export { ComponentRegistry, Override, ZodOpenApiBaseMetadata, ZodOpenApiMetadata, createComponents, createRegistry, isAnyZodType, unwrapZodObject };
|
package/dist/api.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
const require_components = require('./components-
|
|
1
|
+
const require_components = require('./components-DpACv95L.js');
|
|
2
2
|
|
|
3
3
|
exports.createComponents = require_components.createComponents;
|
|
4
|
-
exports.createParameter = require_components.createParameter;
|
|
5
4
|
exports.createRegistry = require_components.createRegistry;
|
|
6
5
|
exports.isAnyZodType = require_components.isAnyZodType;
|
|
7
6
|
exports.unwrapZodObject = require_components.unwrapZodObject;
|
package/dist/api.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { createComponents,
|
|
1
|
+
import { createComponents, createRegistry, isAnyZodType, unwrapZodObject } from "./components-CUMu4QeI.mjs";
|
|
2
2
|
|
|
3
|
-
export { createComponents,
|
|
3
|
+
export { createComponents, createRegistry, isAnyZodType, unwrapZodObject };
|