zod-openapi 3.0.1 → 3.1.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 CHANGED
@@ -8,7 +8,7 @@ A Typescript library to use <a href="https://github.com/colinhacks/zod">Zod</a>
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>
10
10
  <a href="https://www.npmjs.com/package/zod-openapi"><img src="https://img.shields.io/npm/dm/zod-openapi"/><a>
11
- <a href="https://nodejs.org/en/"><img src="https://img.shields.io/badge/node-%3E%3D%2016.11-brightgreen"/><a>
11
+ <a href="https://nodejs.org/en/"><img src="https://img.shields.io/badge/node-%3E%3D%2018-brightgreen"/><a>
12
12
  <a href="https://github.com/samchungy/zod-openapi/actions/workflows/test.yml"><img src="https://github.com/samchungy/zod-openapi/actions/workflows/test.yml/badge.svg"/><a>
13
13
  <a href="https://github.com/samchungy/zod-openapi/actions/workflows/release.yml"><img src="https://github.com/samchungy/zod-openapi/actions/workflows/release.yml/badge.svg"/><a>
14
14
  <a href="https://github.com/seek-oss/skuba"><img src="https://img.shields.io/badge/🤿%20skuba-powered-009DC4"/><a>
@@ -59,16 +59,16 @@ z.string().openapi({ description: 'hello world!', example: 'hello world' });
59
59
 
60
60
  Use the `.openapi()` method to add metadata to a specific Zod type. The `.openapi()` method takes an object with the following options:
61
61
 
62
- | Option | Description |
63
- | :-------------: | :-------------------------------------------------------------------------------------------------------------------------: |
64
- | OpenAPI Options | This will take any option you would put on a [SchemaObject](https://swagger.io/docs/specification/data-models/data-types/). |
65
- | `effectType` | Use to override the creation type for a [Zod Effect](#zod-effects) |
66
- | `header` | Use to provide metadata for [response headers](#response-headers) |
67
- | `param` | Use to provide metadata for [request parameters](#parameters) |
68
- | `ref` | Use this to [auto register a schema as a re-usable component](#creating-components) |
69
- | `refType` | Use this to set the creation type for a component which is not referenced in the document. |
70
- | `type` | Use this to override the generated type. If this is provided no metadata will be generated. |
71
- | `unionOneOf` | Set to `true` to force a ZodUnion to output `oneOf` instead of `allOf` |
62
+ | Option | Description |
63
+ | :-------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------: |
64
+ | OpenAPI Options | This will take any option you would put on a [SchemaObject](https://swagger.io/docs/specification/data-models/data-types/). |
65
+ | `effectType` | Use to override the creation type for a [Zod Effect](#zod-effects) |
66
+ | `header` | Use to provide metadata for [response headers](#response-headers) |
67
+ | `param` | Use to provide metadata for [request parameters](#parameters) |
68
+ | `ref` | Use this to [auto register a schema as a re-usable component](#creating-components) |
69
+ | `refType` | Use this to set the creation type for a component which is not referenced in the document. |
70
+ | `type` | Use this to override the generated type. If this is provided no metadata will be generated. |
71
+ | `unionOneOf` | Set to `true` to force a single ZodUnion to output `oneOf` instead of `allOf`. See [CreateDocumentOptions](#CreateDocumentOptions) for a global option |
72
72
 
73
73
  ### `createDocument`
74
74
 
@@ -198,6 +198,87 @@ const document = createDocument({
198
198
  ```
199
199
  </details>
200
200
 
201
+ #### CreateDocumentOptions
202
+
203
+ `createDocument` takes an optional `CreateDocumentOptions` argument which can be used to modify how the document is created.
204
+
205
+ ```typescript
206
+ const document = createDocument(details, {
207
+ defaultDateSchema: { type: 'string', format: 'date-time' }, // defaults to { type: 'string' }
208
+ unionOneOf: true, // defaults to false. Forces all ZodUnions to output oneOf instead of allOf. An `.openapi()` `unionOneOf` value takes precedence over this one.
209
+ });
210
+ ```
211
+
212
+ ### `createSchema`
213
+
214
+ Creates an OpenAPI Schema Object along with any registered components. OpenAPI 3.1.0 Schema Objects are fully compatible with JSON Schema.
215
+
216
+ ```typescript
217
+ import 'zod-openapi/extend';
218
+ import { z } from 'zod';
219
+ import { createSchema } from 'zod-openapi';
220
+
221
+ const jobId = z.string().openapi({
222
+ description: 'A unique identifier for a job',
223
+ example: '12345',
224
+ ref: 'jobId',
225
+ });
226
+
227
+ const title = z.string().openapi({
228
+ description: 'Job title',
229
+ example: 'My job',
230
+ });
231
+
232
+ const job = z.object({
233
+ jobId,
234
+ title,
235
+ });
236
+
237
+ const { schema, components } = createSchema(job);
238
+ ```
239
+
240
+ <details>
241
+ <summary>Creates the following object:</summary>
242
+
243
+ ```json
244
+ {
245
+ "schema": {
246
+ "type": "object",
247
+ "properties": {
248
+ "jobId": {
249
+ "$ref": "#/components/schemas/jobId"
250
+ },
251
+ "title": {
252
+ "type": "string",
253
+ "description": "Job title",
254
+ "example": "My job"
255
+ }
256
+ },
257
+ "required": ["jobId", "title"]
258
+ },
259
+ "components": {
260
+ "jobId": {
261
+ "type": "string",
262
+ "description": "A unique identifier for a job",
263
+ "example": "12345"
264
+ }
265
+ }
266
+ }
267
+ ```
268
+ </details>
269
+
270
+ #### CreateSchemaOptions
271
+
272
+ `createSchema` takes an optional `CreateSchemaOptions` parameter which can also take the same options as [CreateDocumentOptions](#createdocumentoptions) along with the following options:
273
+
274
+ ```typescript
275
+ const { schema, components } = createSchema(job, {
276
+ schemaType: 'input'; // This controls whether this should be rendered as a request (`input`) or response (`output`). Defaults to `output`
277
+ openapi: '3.0.0'; // OpenAPI version to use, defaults to `'3.1.0'`
278
+ components: { jobId: z.string() } // Additional components to use and create while rendering the schema
279
+ })
280
+ ```
281
+
201
282
  ### Request Parameters
202
283
 
203
284
  Query, Path, Header & Cookie parameters can be created using the `requestParams` key under the `method` key as follows: