zod-openapi 2.12.0 → 2.14.0
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 +13 -11
- package/lib-commonjs/index.js +708 -292
- package/lib-esm/{index.js → index.mjs} +708 -292
- package/lib-types/create/components.d.ts +25 -2
- package/lib-types/create/document.d.ts +3 -2
- package/lib-types/create/parameters.d.ts +3 -2
- package/lib-types/create/schema/index.d.ts +19 -12
- package/lib-types/create/schema/metadata.d.ts +2 -1
- package/lib-types/create/schema/parsers/array.d.ts +2 -3
- package/lib-types/create/schema/parsers/boolean.d.ts +2 -2
- package/lib-types/create/schema/parsers/brand.d.ts +2 -3
- package/lib-types/create/schema/parsers/catch.d.ts +2 -3
- package/lib-types/create/schema/parsers/date.d.ts +2 -2
- package/lib-types/create/schema/parsers/default.d.ts +2 -3
- package/lib-types/create/schema/parsers/discriminatedUnion.d.ts +2 -2
- package/lib-types/create/schema/parsers/enum.d.ts +2 -2
- package/lib-types/create/schema/parsers/index.d.ts +2 -3
- package/lib-types/create/schema/parsers/intersection.d.ts +2 -3
- package/lib-types/create/schema/parsers/lazy.d.ts +2 -3
- package/lib-types/create/schema/parsers/literal.d.ts +2 -2
- package/lib-types/create/schema/parsers/manual.d.ts +2 -3
- package/lib-types/create/schema/parsers/nativeEnum.d.ts +2 -3
- package/lib-types/create/schema/parsers/null.d.ts +2 -2
- package/lib-types/create/schema/parsers/nullable.d.ts +2 -3
- package/lib-types/create/schema/parsers/number.d.ts +2 -2
- package/lib-types/create/schema/parsers/object.d.ts +14 -6
- package/lib-types/create/schema/parsers/optional.d.ts +9 -4
- package/lib-types/create/schema/parsers/pipeline.d.ts +2 -3
- package/lib-types/create/schema/parsers/preprocess.d.ts +2 -3
- package/lib-types/create/schema/parsers/readonly.d.ts +2 -3
- package/lib-types/create/schema/parsers/record.d.ts +2 -3
- package/lib-types/create/schema/parsers/refine.d.ts +2 -3
- package/lib-types/create/schema/parsers/set.d.ts +2 -3
- package/lib-types/create/schema/parsers/string.d.ts +2 -2
- package/lib-types/create/schema/parsers/transform.d.ts +8 -4
- package/lib-types/create/schema/parsers/tuple.d.ts +2 -3
- package/lib-types/create/schema/parsers/union.d.ts +2 -3
- package/lib-types/create/schema/parsers/unknown.d.ts +2 -2
- package/lib-types/extendZod.d.ts +26 -7
- package/lib-types/openapi3-ts/dist/model/openapi31.d.ts +1 -1
- package/package.json +17 -11
package/README.md
CHANGED
|
@@ -17,19 +17,21 @@ A Typescript library to use <a href="https://github.com/colinhacks/zod">Zod</a>
|
|
|
17
17
|
|
|
18
18
|
## Install
|
|
19
19
|
|
|
20
|
-
Install via `npm` or `
|
|
20
|
+
Install via `npm`, `yarn` or `pnpm`:
|
|
21
21
|
|
|
22
22
|
```bash
|
|
23
23
|
npm install zod zod-openapi
|
|
24
24
|
## or
|
|
25
25
|
yarn add zod zod-openapi
|
|
26
|
+
## or
|
|
27
|
+
pnpm install zod zod-openapi
|
|
26
28
|
```
|
|
27
29
|
|
|
28
30
|
## Usage
|
|
29
31
|
|
|
30
32
|
### `extendZodWithOpenApi`
|
|
31
33
|
|
|
32
|
-
This mutates Zod to add an extra `.openapi()` method.
|
|
34
|
+
This mutates Zod to add an extra `.openapi()` method. Call this at the top of your entry point(s).
|
|
33
35
|
|
|
34
36
|
```typescript
|
|
35
37
|
import { z } from 'zod';
|
|
@@ -48,8 +50,8 @@ Use the `.openapi()` method to add metadata to a specific Zod type. The `.openap
|
|
|
48
50
|
| :-------------: | :-------------------------------------------------------------------------------------------------------------------------: |
|
|
49
51
|
| OpenAPI Options | This will take any option you would put on a [SchemaObject](https://swagger.io/docs/specification/data-models/data-types/). |
|
|
50
52
|
| `effectType` | Use to override the creation type for a [Zod Effect](#zod-effects) |
|
|
51
|
-
| `param` | Use to provide metadata for [request parameters](#parameters) |
|
|
52
53
|
| `header` | Use to provide metadata for [response headers](#response-headers) |
|
|
54
|
+
| `param` | Use to provide metadata for [request parameters](#parameters) |
|
|
53
55
|
| `ref` | Use this to [auto register a schema](#creating-components) |
|
|
54
56
|
| `refType` | Use this to set the creation type for a component which is not referenced in the document. |
|
|
55
57
|
| `type` | Use this to override the generated type. If this is provided no metadata will be generated. |
|
|
@@ -328,13 +330,13 @@ createDocument({
|
|
|
328
330
|
|
|
329
331
|
##### Zod Effects
|
|
330
332
|
|
|
331
|
-
`.transform()` and `.pipe()` are complicated because they technically comprise of two types (input & output). This means that we need to understand which type you are creating. In particular with transform it is very difficult to infer the output type. This library will automatically select which _type_ to use by checking how the schema is used based on the following rules:
|
|
333
|
+
`.transform()`, `.default()` and `.pipe()` are complicated because they technically comprise of two types (input & output). This means that we need to understand which type you are creating. In particular with transform it is very difficult to infer the output type. This library will automatically select which _type_ to use by checking how the schema is used based on the following rules:
|
|
332
334
|
|
|
333
335
|
_Input_: Request Bodies, Request Parameters, Headers
|
|
334
336
|
|
|
335
337
|
_Output_: Responses, Response Headers
|
|
336
338
|
|
|
337
|
-
If a registered schema with a transform or pipeline is used in both a request and response schema you will receive an error because the created schema for each will be different. To override the creation type for a specific ZodEffect, add an `.openapi()` field on it and set the `effectType` field to `input` or `
|
|
339
|
+
If a registered schema with a transform or pipeline is used in both a request and response schema you will receive an error because the created schema for each will be different. To override the creation type for a specific ZodEffect, add an `.openapi()` field on it and set the `effectType` field to `input`, `output` or `same`. This will force this library to always generate the input/output type even if we are creating a response (output) or request (input) type. You typically want to set this when you know the type has not changed in the transform. `same` is the recommended choice as it will generate a TypeScript compiler error if the input and output types in the transform drift.
|
|
338
340
|
|
|
339
341
|
`.preprocess()` will always return the `output` type even if we are creating an input schema. If a different input type is required you can achieve this with a `.transform()` combined with a `.pipe()` or simply declare a manual `type` in `.openapi()`.
|
|
340
342
|
|
|
@@ -578,27 +580,27 @@ See the library in use in the [examples](./examples/) folder.
|
|
|
578
580
|
### Prerequisites
|
|
579
581
|
|
|
580
582
|
- Node.js LTS
|
|
581
|
-
-
|
|
583
|
+
- pnpm
|
|
582
584
|
|
|
583
585
|
```shell
|
|
584
|
-
|
|
585
|
-
|
|
586
|
+
pnpm
|
|
587
|
+
pnpm build
|
|
586
588
|
```
|
|
587
589
|
|
|
588
590
|
### Test
|
|
589
591
|
|
|
590
592
|
```shell
|
|
591
|
-
|
|
593
|
+
pnpm test
|
|
592
594
|
```
|
|
593
595
|
|
|
594
596
|
### Lint
|
|
595
597
|
|
|
596
598
|
```shell
|
|
597
599
|
# Fix issues
|
|
598
|
-
|
|
600
|
+
pnpm format
|
|
599
601
|
|
|
600
602
|
# Check for issues
|
|
601
|
-
|
|
603
|
+
pnpm lint
|
|
602
604
|
```
|
|
603
605
|
|
|
604
606
|
### Release
|