zod-openapi 4.0.0 → 4.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 +8 -5
- package/dist/components.chunk.cjs +16 -0
- package/dist/components.chunk.mjs +16 -0
- package/dist/create/document.d.ts +5 -1
- package/dist/extendZodTypes.d.ts +4 -3
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -68,7 +68,7 @@ Use the `.openapi()` method to add metadata to a specific Zod type. The `.openap
|
|
|
68
68
|
| `ref` | Use this to [auto register a schema as a re-usable component](#creating-components) |
|
|
69
69
|
| `refType` | Use this to set the creation type for a component which is not referenced in the document. |
|
|
70
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 `
|
|
71
|
+
| `unionOneOf` | Set to `true` to force a single ZodUnion to output `oneOf` instead of `anyOf`. See [CreateDocumentOptions](#CreateDocumentOptions) for a global option |
|
|
72
72
|
|
|
73
73
|
### `createDocument`
|
|
74
74
|
|
|
@@ -205,7 +205,8 @@ const document = createDocument({
|
|
|
205
205
|
```typescript
|
|
206
206
|
const document = createDocument(details, {
|
|
207
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
|
|
208
|
+
unionOneOf: true, // defaults to false. Forces all ZodUnions to output oneOf instead of anyOf. An `.openapi()` `unionOneOf` value takes precedence over this one.
|
|
209
|
+
enforceDiscriminatedUnionComponents: true, // defaults to false. Throws an error if a Discriminated Union member is not registered as a component.
|
|
209
210
|
});
|
|
210
211
|
```
|
|
211
212
|
|
|
@@ -449,7 +450,7 @@ Wherever `title` is used in schemas across the document, it will instead be crea
|
|
|
449
450
|
}
|
|
450
451
|
```
|
|
451
452
|
|
|
452
|
-
This
|
|
453
|
+
This is a great way to create less repetitive Open API documentation. There are some Open API features like [discriminator mapping](https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/) which require all schemas in the union to contain a ref.
|
|
453
454
|
|
|
454
455
|
##### Manually Registering Schema
|
|
455
456
|
|
|
@@ -467,7 +468,7 @@ createDocument({
|
|
|
467
468
|
});
|
|
468
469
|
```
|
|
469
470
|
|
|
470
|
-
Unfortunately, as a limitation of this library, you will need to attach an `.openapi()` field or `.describe()` to the schema that you are passing into the components or
|
|
471
|
+
Unfortunately, as a limitation of this library, you will need to attach an `.openapi()` field or `.describe()` to the schema that you are passing into the components or you will not reap the full benefits of component generation. As a result, I recommend utilising the auto registering components over manual registration.
|
|
471
472
|
|
|
472
473
|
#### Parameters
|
|
473
474
|
|
|
@@ -763,6 +764,8 @@ As an example `z.string().nullable()` will be rendered differently
|
|
|
763
764
|
- ZodAny
|
|
764
765
|
- ZodArray
|
|
765
766
|
- `minItems`/`maxItems` mapping for `.length()`, `.min()`, `.max()`
|
|
767
|
+
- ZodBigInt
|
|
768
|
+
- `integer` `type` and `int64` `format` mapping
|
|
766
769
|
- ZodBoolean
|
|
767
770
|
- ZodBranded
|
|
768
771
|
- ZodCatch
|
|
@@ -811,7 +814,7 @@ As an example `z.string().nullable()` will be rendered differently
|
|
|
811
814
|
- `prefixItems` mapping for OpenAPI 3.1.0+
|
|
812
815
|
- ZodUndefined
|
|
813
816
|
- ZodUnion
|
|
814
|
-
- By default it outputs an `
|
|
817
|
+
- By default it outputs an `anyOf` schema. Use `unionOneOf` to change this to output `oneOf` instead.
|
|
815
818
|
- ZodUnknown
|
|
816
819
|
|
|
817
820
|
If this library cannot determine a type for a Zod Schema, it will throw an error. To avoid this, declare a manual `type` in the `.openapi()` section of that schema.
|
|
@@ -154,6 +154,13 @@ const createArraySchema = (zodArray, state) => {
|
|
|
154
154
|
effects: items.effects
|
|
155
155
|
};
|
|
156
156
|
};
|
|
157
|
+
const createBigIntSchema = (_zodBigInt) => ({
|
|
158
|
+
type: "schema",
|
|
159
|
+
schema: {
|
|
160
|
+
type: "integer",
|
|
161
|
+
format: "int64"
|
|
162
|
+
}
|
|
163
|
+
});
|
|
157
164
|
const createBooleanSchema = (_zodBoolean) => ({
|
|
158
165
|
type: "schema",
|
|
159
166
|
schema: {
|
|
@@ -452,6 +459,7 @@ const unwrapLiterals = (zodType, state) => {
|
|
|
452
459
|
return void 0;
|
|
453
460
|
};
|
|
454
461
|
const mapDiscriminator = (schemas, zodObjects, discriminator, state) => {
|
|
462
|
+
var _a;
|
|
455
463
|
if (typeof discriminator !== "string") {
|
|
456
464
|
return void 0;
|
|
457
465
|
}
|
|
@@ -460,6 +468,11 @@ const mapDiscriminator = (schemas, zodObjects, discriminator, state) => {
|
|
|
460
468
|
const schema = schemas[index];
|
|
461
469
|
const componentSchemaRef = "$ref" in schema ? schema == null ? void 0 : schema.$ref : void 0;
|
|
462
470
|
if (!componentSchemaRef) {
|
|
471
|
+
if ((_a = state.documentOptions) == null ? void 0 : _a.enforceDiscriminatedUnionComponents) {
|
|
472
|
+
throw new Error(
|
|
473
|
+
`Discriminated Union member ${index} at ${state.path.join(" > ")} is not registered as a component`
|
|
474
|
+
);
|
|
475
|
+
}
|
|
463
476
|
return void 0;
|
|
464
477
|
}
|
|
465
478
|
const value = zodObject.shape[discriminator];
|
|
@@ -1379,6 +1392,9 @@ const createSchemaSwitch = (zodSchema, previous, state) => {
|
|
|
1379
1392
|
if (isZodType(zodSchema, "ZodSet")) {
|
|
1380
1393
|
return createSetSchema(zodSchema, state);
|
|
1381
1394
|
}
|
|
1395
|
+
if (isZodType(zodSchema, "ZodBigInt")) {
|
|
1396
|
+
return createBigIntSchema();
|
|
1397
|
+
}
|
|
1382
1398
|
return createManualTypeSchema(zodSchema, state);
|
|
1383
1399
|
};
|
|
1384
1400
|
const createNewSchema = ({
|
|
@@ -153,6 +153,13 @@ const createArraySchema = (zodArray, state) => {
|
|
|
153
153
|
effects: items.effects
|
|
154
154
|
};
|
|
155
155
|
};
|
|
156
|
+
const createBigIntSchema = (_zodBigInt) => ({
|
|
157
|
+
type: "schema",
|
|
158
|
+
schema: {
|
|
159
|
+
type: "integer",
|
|
160
|
+
format: "int64"
|
|
161
|
+
}
|
|
162
|
+
});
|
|
156
163
|
const createBooleanSchema = (_zodBoolean) => ({
|
|
157
164
|
type: "schema",
|
|
158
165
|
schema: {
|
|
@@ -451,6 +458,7 @@ const unwrapLiterals = (zodType, state) => {
|
|
|
451
458
|
return void 0;
|
|
452
459
|
};
|
|
453
460
|
const mapDiscriminator = (schemas, zodObjects, discriminator, state) => {
|
|
461
|
+
var _a;
|
|
454
462
|
if (typeof discriminator !== "string") {
|
|
455
463
|
return void 0;
|
|
456
464
|
}
|
|
@@ -459,6 +467,11 @@ const mapDiscriminator = (schemas, zodObjects, discriminator, state) => {
|
|
|
459
467
|
const schema = schemas[index];
|
|
460
468
|
const componentSchemaRef = "$ref" in schema ? schema == null ? void 0 : schema.$ref : void 0;
|
|
461
469
|
if (!componentSchemaRef) {
|
|
470
|
+
if ((_a = state.documentOptions) == null ? void 0 : _a.enforceDiscriminatedUnionComponents) {
|
|
471
|
+
throw new Error(
|
|
472
|
+
`Discriminated Union member ${index} at ${state.path.join(" > ")} is not registered as a component`
|
|
473
|
+
);
|
|
474
|
+
}
|
|
462
475
|
return void 0;
|
|
463
476
|
}
|
|
464
477
|
const value = zodObject.shape[discriminator];
|
|
@@ -1378,6 +1391,9 @@ const createSchemaSwitch = (zodSchema, previous, state) => {
|
|
|
1378
1391
|
if (isZodType(zodSchema, "ZodSet")) {
|
|
1379
1392
|
return createSetSchema(zodSchema, state);
|
|
1380
1393
|
}
|
|
1394
|
+
if (isZodType(zodSchema, "ZodBigInt")) {
|
|
1395
|
+
return createBigIntSchema();
|
|
1396
|
+
}
|
|
1381
1397
|
return createManualTypeSchema(zodSchema, state);
|
|
1382
1398
|
};
|
|
1383
1399
|
const createNewSchema = ({
|
|
@@ -74,12 +74,16 @@ interface ZodOpenApiObject extends Omit<OpenAPIObject, 'openapi' | 'paths' | 'we
|
|
|
74
74
|
}
|
|
75
75
|
type ZodObjectInputType<Output = unknown, Def extends ZodTypeDef = ZodTypeDef, Input = Record<string, unknown>> = ZodType<Output, Def, Input>;
|
|
76
76
|
interface CreateDocumentOptions {
|
|
77
|
+
/**
|
|
78
|
+
* Used to throw an error if a Discriminated Union member is not registered as a component
|
|
79
|
+
*/
|
|
80
|
+
enforceDiscriminatedUnionComponents?: boolean;
|
|
77
81
|
/**
|
|
78
82
|
* Used to change the default Zod Date schema
|
|
79
83
|
*/
|
|
80
84
|
defaultDateSchema?: Pick<SchemaObject, 'type' | 'format'>;
|
|
81
85
|
/**
|
|
82
|
-
* Used to set the output of a ZodUnion to be `oneOf` instead of `
|
|
86
|
+
* Used to set the output of a ZodUnion to be `oneOf` instead of `anyOf`
|
|
83
87
|
*/
|
|
84
88
|
unionOneOf?: boolean;
|
|
85
89
|
}
|
package/dist/extendZodTypes.d.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import { ZodTypeAny, z,
|
|
1
|
+
import { ZodTypeAny, z, ZodObject } from 'zod';
|
|
2
2
|
import { CreationType } from './create/components.js';
|
|
3
3
|
import { HeaderObject as HeaderObject$1, SchemaObject as SchemaObject$1 } from './openapi3-ts/dist/model/openapi30.js';
|
|
4
4
|
import { ParameterObject, ExampleObject, ReferenceObject, HeaderObject, SchemaObject as SchemaObject$2 } from './openapi3-ts/dist/model/openapi31.js';
|
|
5
5
|
|
|
6
6
|
type SchemaObject = SchemaObject$1 & SchemaObject$2;
|
|
7
|
+
type ReplaceDate<T> = T extends Date ? Date | string : T;
|
|
7
8
|
/**
|
|
8
9
|
* zod-openapi metadata
|
|
9
10
|
*/
|
|
10
|
-
interface ZodOpenApiMetadata<T extends ZodTypeAny, TInferred = z.input<T> | z.output<T>> extends SchemaObject {
|
|
11
|
+
interface ZodOpenApiMetadata<T extends ZodTypeAny, TInferred = Exclude<ReplaceDate<z.input<T> | z.output<T>>, undefined>> extends SchemaObject {
|
|
11
12
|
example?: TInferred;
|
|
12
13
|
examples?: [TInferred, ...TInferred[]];
|
|
13
|
-
default?:
|
|
14
|
+
default?: TInferred;
|
|
14
15
|
/**
|
|
15
16
|
* Used to set the output of a ZodUnion to be `oneOf` instead of `allOf`
|
|
16
17
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zod-openapi",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.1",
|
|
4
4
|
"description": "Convert Zod Schemas to OpenAPI v3.x documentation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -72,14 +72,14 @@
|
|
|
72
72
|
"test:watch": "skuba test --watch"
|
|
73
73
|
},
|
|
74
74
|
"devDependencies": {
|
|
75
|
-
"@arethetypeswrong/cli": "0.
|
|
75
|
+
"@arethetypeswrong/cli": "0.17.0",
|
|
76
76
|
"@crackle/cli": "0.15.5",
|
|
77
|
-
"@redocly/cli": "1.25.
|
|
77
|
+
"@redocly/cli": "1.25.13",
|
|
78
78
|
"@types/node": "^20.3.0",
|
|
79
79
|
"eslint-plugin-zod-openapi": "^1.0.0-beta.0",
|
|
80
80
|
"openapi3-ts": "4.4.0",
|
|
81
81
|
"skuba": "9.1.0",
|
|
82
|
-
"yaml": "2.6.
|
|
82
|
+
"yaml": "2.6.1",
|
|
83
83
|
"zod": "3.23.8"
|
|
84
84
|
},
|
|
85
85
|
"peerDependencies": {
|