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
Copy file name to clipboardExpand all lines: README.md
+72-2
Original file line number
Diff line number
Diff line change
@@ -469,16 +469,86 @@ createDocument({
469
469
470
470
##### Zod Effects
471
471
472
-
`.transform()`, `.catch()`, `.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:
472
+
`.transform()`, `.catch()`, `.default()` and `.pipe()` are complicated because they all comprise of two different types that we could generate (input & output).
473
+
474
+
We attempt to determine what type of schema to create based on the following contexts:
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.
480
+
As an example:
481
+
482
+
```ts
483
+
z.object({
484
+
a: z.string().default('a'),
485
+
});
486
+
```
487
+
488
+
In a request context, this would render the following OpenAPI schema:
489
+
490
+
```yaml
491
+
type: 'object'
492
+
properties:
493
+
- a:
494
+
type: 'string'
495
+
default: 'a'
496
+
```
497
+
498
+
or the following for a response:
499
+
500
+
```yaml
501
+
type: 'object'
502
+
properties:
503
+
- a:
504
+
type: 'string'
505
+
default: 'a'
506
+
required:
507
+
- a
508
+
```
509
+
510
+
Note how the response schema created an extra `required` field. This means, if you were to register a Zod schema with `.default()` as a component and use it in both a request or response, your schema would be invalid. Zod OpenAPI keeps track of this usage and will throw an error if this occurs.
511
+
512
+
##### EffectType
513
+
514
+
```ts
515
+
z.string().transform((str) => str.trim());
516
+
```
517
+
518
+
Whilst the TypeScript compiler can understand that the result is still a `string`, unfortunately we cannot introspect this as your transform function may be far more complicated than this example. To address this, you can set the `effectType` on the schema to `same`, `input` or `output`.
519
+
520
+
`same`- This informs Zod OpenAPI to pick either the input schema or output schema to generate with because they should be the same.
521
+
522
+
```ts
523
+
z.string()
524
+
.transform((str) => str.trim())
525
+
.openapi({ effectType: 'same' });
526
+
```
527
+
528
+
If the transform were to drift from this, you will receive a TypeScript error:
529
+
530
+
```ts
531
+
z.string()
532
+
.transform((str) => str.length)
533
+
.openapi({ effectType: 'same' });
534
+
// ~~~~~~~~~~
535
+
// Type 'same' is not assignable to type 'CreationType | undefined'.ts(2322)
536
+
```
537
+
538
+
`input`or `output` - This tells Zod OpenAPI to pick a specific schema to create whenever we run into this schema, regardless of it is a request or response schema.
539
+
540
+
```ts
541
+
z.string()
542
+
.transform((str) => str.length)
543
+
.openapi({ effectType: 'input' });
544
+
```
545
+
546
+
##### Preprocess
479
547
480
548
`.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()`.
481
549
550
+
##### Component Effects
551
+
482
552
If you are adding a ZodSchema directly to the `components` section which is not referenced anywhere in the document, additional context may be required to create either an input or output schema. You can do this by setting the `refType` field to `input` or `output` in `.openapi()`. This defaults to `output` by default.
0 commit comments