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.
Files changed (41) hide show
  1. package/README.md +13 -11
  2. package/lib-commonjs/index.js +708 -292
  3. package/lib-esm/{index.js → index.mjs} +708 -292
  4. package/lib-types/create/components.d.ts +25 -2
  5. package/lib-types/create/document.d.ts +3 -2
  6. package/lib-types/create/parameters.d.ts +3 -2
  7. package/lib-types/create/schema/index.d.ts +19 -12
  8. package/lib-types/create/schema/metadata.d.ts +2 -1
  9. package/lib-types/create/schema/parsers/array.d.ts +2 -3
  10. package/lib-types/create/schema/parsers/boolean.d.ts +2 -2
  11. package/lib-types/create/schema/parsers/brand.d.ts +2 -3
  12. package/lib-types/create/schema/parsers/catch.d.ts +2 -3
  13. package/lib-types/create/schema/parsers/date.d.ts +2 -2
  14. package/lib-types/create/schema/parsers/default.d.ts +2 -3
  15. package/lib-types/create/schema/parsers/discriminatedUnion.d.ts +2 -2
  16. package/lib-types/create/schema/parsers/enum.d.ts +2 -2
  17. package/lib-types/create/schema/parsers/index.d.ts +2 -3
  18. package/lib-types/create/schema/parsers/intersection.d.ts +2 -3
  19. package/lib-types/create/schema/parsers/lazy.d.ts +2 -3
  20. package/lib-types/create/schema/parsers/literal.d.ts +2 -2
  21. package/lib-types/create/schema/parsers/manual.d.ts +2 -3
  22. package/lib-types/create/schema/parsers/nativeEnum.d.ts +2 -3
  23. package/lib-types/create/schema/parsers/null.d.ts +2 -2
  24. package/lib-types/create/schema/parsers/nullable.d.ts +2 -3
  25. package/lib-types/create/schema/parsers/number.d.ts +2 -2
  26. package/lib-types/create/schema/parsers/object.d.ts +14 -6
  27. package/lib-types/create/schema/parsers/optional.d.ts +9 -4
  28. package/lib-types/create/schema/parsers/pipeline.d.ts +2 -3
  29. package/lib-types/create/schema/parsers/preprocess.d.ts +2 -3
  30. package/lib-types/create/schema/parsers/readonly.d.ts +2 -3
  31. package/lib-types/create/schema/parsers/record.d.ts +2 -3
  32. package/lib-types/create/schema/parsers/refine.d.ts +2 -3
  33. package/lib-types/create/schema/parsers/set.d.ts +2 -3
  34. package/lib-types/create/schema/parsers/string.d.ts +2 -2
  35. package/lib-types/create/schema/parsers/transform.d.ts +8 -4
  36. package/lib-types/create/schema/parsers/tuple.d.ts +2 -3
  37. package/lib-types/create/schema/parsers/union.d.ts +2 -3
  38. package/lib-types/create/schema/parsers/unknown.d.ts +2 -2
  39. package/lib-types/extendZod.d.ts +26 -7
  40. package/lib-types/openapi3-ts/dist/model/openapi31.d.ts +1 -1
  41. 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 `yarn`:
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. Make a side-effectful import at the top of your entry point(s).
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 `output`. 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 use this when you know your transform has not changed the type.
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
- - Yarn 1.x
583
+ - pnpm
582
584
 
583
585
  ```shell
584
- yarn
585
- yarn build
586
+ pnpm
587
+ pnpm build
586
588
  ```
587
589
 
588
590
  ### Test
589
591
 
590
592
  ```shell
591
- yarn test
593
+ pnpm test
592
594
  ```
593
595
 
594
596
  ### Lint
595
597
 
596
598
  ```shell
597
599
  # Fix issues
598
- yarn format
600
+ pnpm format
599
601
 
600
602
  # Check for issues
601
- yarn lint
603
+ pnpm lint
602
604
  ```
603
605
 
604
606
  ### Release