zod-openapi 2.13.0 → 2.15.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 -10
- package/lib-commonjs/index.js +66 -30
- package/lib-esm/index.mjs +66 -30
- package/lib-types/create/document.d.ts +3 -2
- package/lib-types/create/parameters.d.ts +3 -2
- package/lib-types/create/schema/parsers/literal.d.ts +2 -2
- package/lib-types/create/schema/parsers/null.d.ts +1 -2
- package/lib-types/extendZod.d.ts +25 -6
- package/lib-types/openapi3-ts/dist/model/openapi31.d.ts +2 -1
- package/package.json +8 -9
package/README.md
CHANGED
|
@@ -17,12 +17,14 @@ 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
|
|
@@ -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`, `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
|
|
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
|
|
|
@@ -516,6 +518,7 @@ For example in `z.string().nullable()` will be rendered differently
|
|
|
516
518
|
- `pre-process` support. We assume that the input type is the same as the output type. Otherwise pipe and transform can be used instead.
|
|
517
519
|
- `refine` full support
|
|
518
520
|
- ZodEnum
|
|
521
|
+
- ZodIntersection
|
|
519
522
|
- ZodLazy
|
|
520
523
|
- The recursive schema within the ZodLazy or the ZodLazy _**must**_ be registered as a component. See [Creating Components](#creating-components) for more information.
|
|
521
524
|
- ZodLiteral
|
|
@@ -578,27 +581,27 @@ See the library in use in the [examples](./examples/) folder.
|
|
|
578
581
|
### Prerequisites
|
|
579
582
|
|
|
580
583
|
- Node.js LTS
|
|
581
|
-
-
|
|
584
|
+
- pnpm
|
|
582
585
|
|
|
583
586
|
```shell
|
|
584
|
-
|
|
585
|
-
|
|
587
|
+
pnpm
|
|
588
|
+
pnpm build
|
|
586
589
|
```
|
|
587
590
|
|
|
588
591
|
### Test
|
|
589
592
|
|
|
590
593
|
```shell
|
|
591
|
-
|
|
594
|
+
pnpm test
|
|
592
595
|
```
|
|
593
596
|
|
|
594
597
|
### Lint
|
|
595
598
|
|
|
596
599
|
```shell
|
|
597
600
|
# Fix issues
|
|
598
|
-
|
|
601
|
+
pnpm format
|
|
599
602
|
|
|
600
603
|
# Check for issues
|
|
601
|
-
|
|
604
|
+
pnpm lint
|
|
602
605
|
```
|
|
603
606
|
|
|
604
607
|
### Release
|
package/lib-commonjs/index.js
CHANGED
|
@@ -178,11 +178,11 @@ var throwTransformError = (effect) => {
|
|
|
178
178
|
|
|
179
179
|
This may cause the schema to render incorrectly and is most likely a mistake. You can resolve this by:
|
|
180
180
|
|
|
181
|
-
1. Setting an \`effectType\` on the
|
|
181
|
+
1. Setting an \`effectType\` on one of the transformations to \`same\` (Not applicable for ZodDefault), \`input\` or \`output\` eg. \`.openapi({type: 'same'})\`
|
|
182
182
|
2. Wrapping the transformation in a ZodPipeline
|
|
183
183
|
3. Assigning a manual type to the transformation eg. \`.openapi({type: 'string'})\`
|
|
184
184
|
4. Removing the transformation
|
|
185
|
-
5.
|
|
185
|
+
5. Deregister the component containing the transformation`
|
|
186
186
|
);
|
|
187
187
|
};
|
|
188
188
|
var resolveSingleEffect = (effect, state) => {
|
|
@@ -356,15 +356,47 @@ var createLazySchema = (zodLazy, state) => {
|
|
|
356
356
|
return createSchemaObject(innerSchema, state, ["lazy schema"]);
|
|
357
357
|
};
|
|
358
358
|
|
|
359
|
-
// src/
|
|
360
|
-
var
|
|
359
|
+
// src/openapi.ts
|
|
360
|
+
var openApiVersions = [
|
|
361
|
+
"3.0.0",
|
|
362
|
+
"3.0.1",
|
|
363
|
+
"3.0.2",
|
|
364
|
+
"3.0.3",
|
|
365
|
+
"3.1.0"
|
|
366
|
+
];
|
|
367
|
+
var satisfiesVersion = (test, against) => openApiVersions.indexOf(test) >= openApiVersions.indexOf(against);
|
|
368
|
+
|
|
369
|
+
// src/create/schema/parsers/null.ts
|
|
370
|
+
var createNullSchema = () => ({
|
|
361
371
|
type: "schema",
|
|
362
372
|
schema: {
|
|
363
|
-
type:
|
|
364
|
-
enum: [zodLiteral._def.value]
|
|
373
|
+
type: "null"
|
|
365
374
|
}
|
|
366
375
|
});
|
|
367
376
|
|
|
377
|
+
// src/create/schema/parsers/literal.ts
|
|
378
|
+
var createLiteralSchema = (zodLiteral, state) => {
|
|
379
|
+
if (zodLiteral.value === null) {
|
|
380
|
+
return createNullSchema();
|
|
381
|
+
}
|
|
382
|
+
if (satisfiesVersion(state.components.openapi, "3.1.0")) {
|
|
383
|
+
return {
|
|
384
|
+
type: "schema",
|
|
385
|
+
schema: {
|
|
386
|
+
type: typeof zodLiteral.value,
|
|
387
|
+
const: zodLiteral.value
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
return {
|
|
392
|
+
type: "schema",
|
|
393
|
+
schema: {
|
|
394
|
+
type: typeof zodLiteral.value,
|
|
395
|
+
enum: [zodLiteral.value]
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
};
|
|
399
|
+
|
|
368
400
|
// src/create/schema/parsers/manual.ts
|
|
369
401
|
var createManualTypeSchema = (zodSchema, state) => {
|
|
370
402
|
if (!zodSchema._def.openapi?.type) {
|
|
@@ -383,16 +415,6 @@ var createManualTypeSchema = (zodSchema, state) => {
|
|
|
383
415
|
};
|
|
384
416
|
};
|
|
385
417
|
|
|
386
|
-
// src/openapi.ts
|
|
387
|
-
var openApiVersions = [
|
|
388
|
-
"3.0.0",
|
|
389
|
-
"3.0.1",
|
|
390
|
-
"3.0.2",
|
|
391
|
-
"3.0.3",
|
|
392
|
-
"3.1.0"
|
|
393
|
-
];
|
|
394
|
-
var satisfiesVersion = (test, against) => openApiVersions.indexOf(test) >= openApiVersions.indexOf(against);
|
|
395
|
-
|
|
396
418
|
// src/create/schema/parsers/nativeEnum.ts
|
|
397
419
|
var createNativeEnumSchema = (zodEnum, state) => {
|
|
398
420
|
const enumValues = getValidEnumValues(zodEnum._def.values);
|
|
@@ -444,14 +466,6 @@ var sortStringsAndNumbers = (values) => ({
|
|
|
444
466
|
numbers: values.filter((value) => typeof value === "number")
|
|
445
467
|
});
|
|
446
468
|
|
|
447
|
-
// src/create/schema/parsers/null.ts
|
|
448
|
-
var createNullSchema = (_zodNull) => ({
|
|
449
|
-
type: "schema",
|
|
450
|
-
schema: {
|
|
451
|
-
type: "null"
|
|
452
|
-
}
|
|
453
|
-
});
|
|
454
|
-
|
|
455
469
|
// src/create/schema/parsers/nullable.ts
|
|
456
470
|
var createNullableSchema = (zodNullable, state) => {
|
|
457
471
|
const schemaObject = createSchemaObject(zodNullable.unwrap(), state, [
|
|
@@ -589,7 +603,7 @@ var mapNumberType = (zodNumberChecks) => zodNumberChecks.int ? "integer" : "numb
|
|
|
589
603
|
// src/create/schema/parsers/optional.ts
|
|
590
604
|
var createOptionalSchema = (zodOptional, state) => createSchemaObject(zodOptional.unwrap(), state, ["optional"]);
|
|
591
605
|
var isOptionalSchema = (zodSchema, state) => {
|
|
592
|
-
if (isZodType(zodSchema, "ZodOptional") || isZodType(zodSchema, "ZodNever") || isZodType(zodSchema, "ZodUndefined")) {
|
|
606
|
+
if (isZodType(zodSchema, "ZodOptional") || isZodType(zodSchema, "ZodNever") || isZodType(zodSchema, "ZodUndefined") || isZodType(zodSchema, "ZodLiteral") && zodSchema._def.value === void 0) {
|
|
593
607
|
return { optional: true };
|
|
594
608
|
}
|
|
595
609
|
if (isZodType(zodSchema, "ZodDefault")) {
|
|
@@ -1189,7 +1203,7 @@ var createSchemaSwitch = (zodSchema, state) => {
|
|
|
1189
1203
|
return createEnumSchema(zodSchema);
|
|
1190
1204
|
}
|
|
1191
1205
|
if (isZodType(zodSchema, "ZodLiteral")) {
|
|
1192
|
-
return createLiteralSchema(zodSchema);
|
|
1206
|
+
return createLiteralSchema(zodSchema, state);
|
|
1193
1207
|
}
|
|
1194
1208
|
if (isZodType(zodSchema, "ZodNativeEnum")) {
|
|
1195
1209
|
return createNativeEnumSchema(zodSchema, state);
|
|
@@ -1207,7 +1221,7 @@ var createSchemaSwitch = (zodSchema, state) => {
|
|
|
1207
1221
|
return createDiscriminatedUnionSchema(zodSchema, state);
|
|
1208
1222
|
}
|
|
1209
1223
|
if (isZodType(zodSchema, "ZodNull")) {
|
|
1210
|
-
return createNullSchema(
|
|
1224
|
+
return createNullSchema();
|
|
1211
1225
|
}
|
|
1212
1226
|
if (isZodType(zodSchema, "ZodNullable")) {
|
|
1213
1227
|
return createNullableSchema(zodSchema, state);
|
|
@@ -1442,11 +1456,12 @@ var createParamOrRef = (zodSchema, components, subpath, type, name) => {
|
|
|
1442
1456
|
}
|
|
1443
1457
|
return paramObject;
|
|
1444
1458
|
};
|
|
1445
|
-
var createParameters = (type,
|
|
1446
|
-
if (!
|
|
1459
|
+
var createParameters = (type, zodObjectType, components, subpath) => {
|
|
1460
|
+
if (!zodObjectType) {
|
|
1447
1461
|
return [];
|
|
1448
1462
|
}
|
|
1449
|
-
|
|
1463
|
+
const zodObject = getZodObject(zodObjectType, "input").shape;
|
|
1464
|
+
return Object.entries(zodObject).map(
|
|
1450
1465
|
([key, zodSchema]) => createParamOrRef(zodSchema, components, [...subpath, key], type, key)
|
|
1451
1466
|
);
|
|
1452
1467
|
};
|
|
@@ -1500,6 +1515,27 @@ var createParametersObject = (parameters, requestParams, components, subpath) =>
|
|
|
1500
1515
|
];
|
|
1501
1516
|
return combinedParameters.length ? combinedParameters : void 0;
|
|
1502
1517
|
};
|
|
1518
|
+
var getZodObject = (schema, type) => {
|
|
1519
|
+
if (isZodType(schema, "ZodObject")) {
|
|
1520
|
+
return schema;
|
|
1521
|
+
}
|
|
1522
|
+
if (isZodType(schema, "ZodLazy")) {
|
|
1523
|
+
return getZodObject(schema.schema, type);
|
|
1524
|
+
}
|
|
1525
|
+
if (isZodType(schema, "ZodEffects")) {
|
|
1526
|
+
return getZodObject(schema.innerType(), type);
|
|
1527
|
+
}
|
|
1528
|
+
if (isZodType(schema, "ZodBranded")) {
|
|
1529
|
+
return getZodObject(schema.unwrap(), type);
|
|
1530
|
+
}
|
|
1531
|
+
if (isZodType(schema, "ZodPipeline")) {
|
|
1532
|
+
if (type === "input") {
|
|
1533
|
+
return getZodObject(schema._def.in, type);
|
|
1534
|
+
}
|
|
1535
|
+
return getZodObject(schema._def.out, type);
|
|
1536
|
+
}
|
|
1537
|
+
throw new Error("failed to find ZodObject in schema");
|
|
1538
|
+
};
|
|
1503
1539
|
|
|
1504
1540
|
// src/create/content.ts
|
|
1505
1541
|
var createMediaTypeSchema = (schemaObject, components, type, subpath) => {
|
package/lib-esm/index.mjs
CHANGED
|
@@ -154,11 +154,11 @@ var throwTransformError = (effect) => {
|
|
|
154
154
|
|
|
155
155
|
This may cause the schema to render incorrectly and is most likely a mistake. You can resolve this by:
|
|
156
156
|
|
|
157
|
-
1. Setting an \`effectType\` on the
|
|
157
|
+
1. Setting an \`effectType\` on one of the transformations to \`same\` (Not applicable for ZodDefault), \`input\` or \`output\` eg. \`.openapi({type: 'same'})\`
|
|
158
158
|
2. Wrapping the transformation in a ZodPipeline
|
|
159
159
|
3. Assigning a manual type to the transformation eg. \`.openapi({type: 'string'})\`
|
|
160
160
|
4. Removing the transformation
|
|
161
|
-
5.
|
|
161
|
+
5. Deregister the component containing the transformation`
|
|
162
162
|
);
|
|
163
163
|
};
|
|
164
164
|
var resolveSingleEffect = (effect, state) => {
|
|
@@ -332,15 +332,47 @@ var createLazySchema = (zodLazy, state) => {
|
|
|
332
332
|
return createSchemaObject(innerSchema, state, ["lazy schema"]);
|
|
333
333
|
};
|
|
334
334
|
|
|
335
|
-
// src/
|
|
336
|
-
var
|
|
335
|
+
// src/openapi.ts
|
|
336
|
+
var openApiVersions = [
|
|
337
|
+
"3.0.0",
|
|
338
|
+
"3.0.1",
|
|
339
|
+
"3.0.2",
|
|
340
|
+
"3.0.3",
|
|
341
|
+
"3.1.0"
|
|
342
|
+
];
|
|
343
|
+
var satisfiesVersion = (test, against) => openApiVersions.indexOf(test) >= openApiVersions.indexOf(against);
|
|
344
|
+
|
|
345
|
+
// src/create/schema/parsers/null.ts
|
|
346
|
+
var createNullSchema = () => ({
|
|
337
347
|
type: "schema",
|
|
338
348
|
schema: {
|
|
339
|
-
type:
|
|
340
|
-
enum: [zodLiteral._def.value]
|
|
349
|
+
type: "null"
|
|
341
350
|
}
|
|
342
351
|
});
|
|
343
352
|
|
|
353
|
+
// src/create/schema/parsers/literal.ts
|
|
354
|
+
var createLiteralSchema = (zodLiteral, state) => {
|
|
355
|
+
if (zodLiteral.value === null) {
|
|
356
|
+
return createNullSchema();
|
|
357
|
+
}
|
|
358
|
+
if (satisfiesVersion(state.components.openapi, "3.1.0")) {
|
|
359
|
+
return {
|
|
360
|
+
type: "schema",
|
|
361
|
+
schema: {
|
|
362
|
+
type: typeof zodLiteral.value,
|
|
363
|
+
const: zodLiteral.value
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
return {
|
|
368
|
+
type: "schema",
|
|
369
|
+
schema: {
|
|
370
|
+
type: typeof zodLiteral.value,
|
|
371
|
+
enum: [zodLiteral.value]
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
};
|
|
375
|
+
|
|
344
376
|
// src/create/schema/parsers/manual.ts
|
|
345
377
|
var createManualTypeSchema = (zodSchema, state) => {
|
|
346
378
|
if (!zodSchema._def.openapi?.type) {
|
|
@@ -359,16 +391,6 @@ var createManualTypeSchema = (zodSchema, state) => {
|
|
|
359
391
|
};
|
|
360
392
|
};
|
|
361
393
|
|
|
362
|
-
// src/openapi.ts
|
|
363
|
-
var openApiVersions = [
|
|
364
|
-
"3.0.0",
|
|
365
|
-
"3.0.1",
|
|
366
|
-
"3.0.2",
|
|
367
|
-
"3.0.3",
|
|
368
|
-
"3.1.0"
|
|
369
|
-
];
|
|
370
|
-
var satisfiesVersion = (test, against) => openApiVersions.indexOf(test) >= openApiVersions.indexOf(against);
|
|
371
|
-
|
|
372
394
|
// src/create/schema/parsers/nativeEnum.ts
|
|
373
395
|
var createNativeEnumSchema = (zodEnum, state) => {
|
|
374
396
|
const enumValues = getValidEnumValues(zodEnum._def.values);
|
|
@@ -420,14 +442,6 @@ var sortStringsAndNumbers = (values) => ({
|
|
|
420
442
|
numbers: values.filter((value) => typeof value === "number")
|
|
421
443
|
});
|
|
422
444
|
|
|
423
|
-
// src/create/schema/parsers/null.ts
|
|
424
|
-
var createNullSchema = (_zodNull) => ({
|
|
425
|
-
type: "schema",
|
|
426
|
-
schema: {
|
|
427
|
-
type: "null"
|
|
428
|
-
}
|
|
429
|
-
});
|
|
430
|
-
|
|
431
445
|
// src/create/schema/parsers/nullable.ts
|
|
432
446
|
var createNullableSchema = (zodNullable, state) => {
|
|
433
447
|
const schemaObject = createSchemaObject(zodNullable.unwrap(), state, [
|
|
@@ -565,7 +579,7 @@ var mapNumberType = (zodNumberChecks) => zodNumberChecks.int ? "integer" : "numb
|
|
|
565
579
|
// src/create/schema/parsers/optional.ts
|
|
566
580
|
var createOptionalSchema = (zodOptional, state) => createSchemaObject(zodOptional.unwrap(), state, ["optional"]);
|
|
567
581
|
var isOptionalSchema = (zodSchema, state) => {
|
|
568
|
-
if (isZodType(zodSchema, "ZodOptional") || isZodType(zodSchema, "ZodNever") || isZodType(zodSchema, "ZodUndefined")) {
|
|
582
|
+
if (isZodType(zodSchema, "ZodOptional") || isZodType(zodSchema, "ZodNever") || isZodType(zodSchema, "ZodUndefined") || isZodType(zodSchema, "ZodLiteral") && zodSchema._def.value === void 0) {
|
|
569
583
|
return { optional: true };
|
|
570
584
|
}
|
|
571
585
|
if (isZodType(zodSchema, "ZodDefault")) {
|
|
@@ -1165,7 +1179,7 @@ var createSchemaSwitch = (zodSchema, state) => {
|
|
|
1165
1179
|
return createEnumSchema(zodSchema);
|
|
1166
1180
|
}
|
|
1167
1181
|
if (isZodType(zodSchema, "ZodLiteral")) {
|
|
1168
|
-
return createLiteralSchema(zodSchema);
|
|
1182
|
+
return createLiteralSchema(zodSchema, state);
|
|
1169
1183
|
}
|
|
1170
1184
|
if (isZodType(zodSchema, "ZodNativeEnum")) {
|
|
1171
1185
|
return createNativeEnumSchema(zodSchema, state);
|
|
@@ -1183,7 +1197,7 @@ var createSchemaSwitch = (zodSchema, state) => {
|
|
|
1183
1197
|
return createDiscriminatedUnionSchema(zodSchema, state);
|
|
1184
1198
|
}
|
|
1185
1199
|
if (isZodType(zodSchema, "ZodNull")) {
|
|
1186
|
-
return createNullSchema(
|
|
1200
|
+
return createNullSchema();
|
|
1187
1201
|
}
|
|
1188
1202
|
if (isZodType(zodSchema, "ZodNullable")) {
|
|
1189
1203
|
return createNullableSchema(zodSchema, state);
|
|
@@ -1418,11 +1432,12 @@ var createParamOrRef = (zodSchema, components, subpath, type, name) => {
|
|
|
1418
1432
|
}
|
|
1419
1433
|
return paramObject;
|
|
1420
1434
|
};
|
|
1421
|
-
var createParameters = (type,
|
|
1422
|
-
if (!
|
|
1435
|
+
var createParameters = (type, zodObjectType, components, subpath) => {
|
|
1436
|
+
if (!zodObjectType) {
|
|
1423
1437
|
return [];
|
|
1424
1438
|
}
|
|
1425
|
-
|
|
1439
|
+
const zodObject = getZodObject(zodObjectType, "input").shape;
|
|
1440
|
+
return Object.entries(zodObject).map(
|
|
1426
1441
|
([key, zodSchema]) => createParamOrRef(zodSchema, components, [...subpath, key], type, key)
|
|
1427
1442
|
);
|
|
1428
1443
|
};
|
|
@@ -1476,6 +1491,27 @@ var createParametersObject = (parameters, requestParams, components, subpath) =>
|
|
|
1476
1491
|
];
|
|
1477
1492
|
return combinedParameters.length ? combinedParameters : void 0;
|
|
1478
1493
|
};
|
|
1494
|
+
var getZodObject = (schema, type) => {
|
|
1495
|
+
if (isZodType(schema, "ZodObject")) {
|
|
1496
|
+
return schema;
|
|
1497
|
+
}
|
|
1498
|
+
if (isZodType(schema, "ZodLazy")) {
|
|
1499
|
+
return getZodObject(schema.schema, type);
|
|
1500
|
+
}
|
|
1501
|
+
if (isZodType(schema, "ZodEffects")) {
|
|
1502
|
+
return getZodObject(schema.innerType(), type);
|
|
1503
|
+
}
|
|
1504
|
+
if (isZodType(schema, "ZodBranded")) {
|
|
1505
|
+
return getZodObject(schema.unwrap(), type);
|
|
1506
|
+
}
|
|
1507
|
+
if (isZodType(schema, "ZodPipeline")) {
|
|
1508
|
+
if (type === "input") {
|
|
1509
|
+
return getZodObject(schema._def.in, type);
|
|
1510
|
+
}
|
|
1511
|
+
return getZodObject(schema._def.out, type);
|
|
1512
|
+
}
|
|
1513
|
+
throw new Error("failed to find ZodObject in schema");
|
|
1514
|
+
};
|
|
1479
1515
|
|
|
1480
1516
|
// src/create/content.ts
|
|
1481
1517
|
var createMediaTypeSchema = (schemaObject, components, type, subpath) => {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AnyZodObject, ZodType } from 'zod';
|
|
1
|
+
import type { AnyZodObject, ZodType, ZodTypeDef } from 'zod';
|
|
2
2
|
import type { OpenApiVersion } from '../openapi';
|
|
3
3
|
import type { oas30, oas31 } from '../openapi3-ts/dist';
|
|
4
4
|
export interface ZodOpenApiMediaTypeObject extends Omit<oas31.MediaTypeObject & oas30.MediaTypeObject, 'schema'> {
|
|
@@ -24,7 +24,7 @@ export interface ZodOpenApiResponsesObject extends oas31.ISpecificationExtension
|
|
|
24
24
|
[statuscode: `${1 | 2 | 3 | 4 | 5}${string}`]: ZodOpenApiResponseObject | oas31.ReferenceObject;
|
|
25
25
|
}
|
|
26
26
|
export type ZodOpenApiParameters = {
|
|
27
|
-
[type in oas31.ParameterLocation & oas30.ParameterLocation]?:
|
|
27
|
+
[type in oas31.ParameterLocation & oas30.ParameterLocation]?: ZodObjectInputType;
|
|
28
28
|
};
|
|
29
29
|
export interface ZodOpenApiOperationObject extends Omit<oas31.OperationObject & oas30.OperationObject, 'requestBody' | 'responses' | 'parameters'> {
|
|
30
30
|
parameters?: Array<ZodType | oas31.ParameterObject | oas30.ParameterObject | oas31.ReferenceObject | oas30.ReferenceObject>;
|
|
@@ -59,4 +59,5 @@ export interface ZodOpenApiObject extends Omit<oas31.OpenAPIObject, 'openapi' |
|
|
|
59
59
|
webhooks?: ZodOpenApiPathsObject;
|
|
60
60
|
components?: ZodOpenApiComponentsObject;
|
|
61
61
|
}
|
|
62
|
+
export type ZodObjectInputType<Output = unknown, Def extends ZodTypeDef = ZodTypeDef, Input = Record<string, unknown>> = ZodType<Output, Def, Input>;
|
|
62
63
|
export declare const createDocument: (zodOpenApiObject: ZodOpenApiObject) => oas31.OpenAPIObject;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import type { ZodType } from 'zod';
|
|
1
|
+
import type { AnyZodObject, ZodType } from 'zod';
|
|
2
2
|
import type { oas30, oas31 } from '../openapi3-ts/dist';
|
|
3
3
|
import type { ComponentsObject } from './components';
|
|
4
|
-
import type { ZodOpenApiParameters } from './document';
|
|
4
|
+
import type { ZodObjectInputType, ZodOpenApiParameters } from './document';
|
|
5
5
|
export declare const createComponentParamRef: (ref: string) => string;
|
|
6
6
|
export declare const createBaseParameter: (schema: ZodType, components: ComponentsObject, subpath: string[]) => oas31.BaseParameterObject;
|
|
7
7
|
export declare const createParamOrRef: (zodSchema: ZodType, components: ComponentsObject, subpath: string[], type?: keyof ZodOpenApiParameters, name?: string) => oas31.ParameterObject | oas31.ReferenceObject;
|
|
8
8
|
export declare const createManualParameters: (parameters: Array<oas31.ParameterObject | oas31.ReferenceObject | oas30.ParameterObject | oas30.ReferenceObject | ZodType> | undefined, components: ComponentsObject, subpath: string[]) => Array<oas31.ParameterObject | oas31.ReferenceObject>;
|
|
9
9
|
export declare const createParametersObject: (parameters: Array<oas31.ParameterObject | oas31.ReferenceObject | oas30.ParameterObject | oas30.ReferenceObject | ZodType> | undefined, requestParams: ZodOpenApiParameters | undefined, components: ComponentsObject, subpath: string[]) => Array<oas31.ParameterObject | oas31.ReferenceObject> | undefined;
|
|
10
|
+
export declare const getZodObject: (schema: ZodObjectInputType, type: 'input' | 'output') => AnyZodObject;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { ZodLiteral } from 'zod';
|
|
2
|
-
import type { Schema } from '..';
|
|
3
|
-
export declare const createLiteralSchema: (zodLiteral: ZodLiteral<unknown
|
|
2
|
+
import type { Schema, SchemaState } from '..';
|
|
3
|
+
export declare const createLiteralSchema: (zodLiteral: ZodLiteral<unknown>, state: SchemaState) => Schema;
|
package/lib-types/extendZod.d.ts
CHANGED
|
@@ -2,51 +2,70 @@ import type { UnknownKeysParam, ZodDate, ZodObject, ZodRawShape, ZodTypeAny, z }
|
|
|
2
2
|
import type { CreationType } from './create/components';
|
|
3
3
|
import type { oas30, oas31 } from './openapi3-ts/dist';
|
|
4
4
|
type SchemaObject = oas30.SchemaObject & oas31.SchemaObject;
|
|
5
|
+
/**
|
|
6
|
+
* zod-openapi metadata
|
|
7
|
+
*/
|
|
5
8
|
interface ZodOpenApiMetadata<T extends ZodTypeAny, TInferred = z.input<T> | z.output<T>> extends SchemaObject {
|
|
6
9
|
example?: TInferred;
|
|
7
10
|
examples?: [TInferred, ...TInferred[]];
|
|
8
11
|
default?: T extends ZodDate ? string : TInferred;
|
|
9
12
|
/**
|
|
10
|
-
*
|
|
13
|
+
* Used to set the output of a ZodUnion to be `oneOf` instead of `allOf`
|
|
11
14
|
*/
|
|
12
15
|
unionOneOf?: boolean;
|
|
13
16
|
/**
|
|
14
|
-
*
|
|
17
|
+
* Used to output this Zod Schema in the components schemas section. Any usage of this Zod Schema will then be transformed into a $ref.
|
|
15
18
|
*/
|
|
16
19
|
ref?: string;
|
|
17
20
|
/**
|
|
18
|
-
*
|
|
21
|
+
* Used when you are manually adding a Zod Schema to the components section. This controls whether this should be rendered as request (`input`) or response (`output`). Defaults to `output`
|
|
19
22
|
*/
|
|
20
23
|
refType?: CreationType;
|
|
21
24
|
/**
|
|
22
|
-
*
|
|
25
|
+
* Used to set the created type of an effect.
|
|
23
26
|
*/
|
|
24
27
|
effectType?: CreationType | (z.input<T> extends z.output<T> ? z.output<T> extends z.input<T> ? 'same' : never : never);
|
|
28
|
+
/**
|
|
29
|
+
* Used to set metadata for a parameter, request header or cookie
|
|
30
|
+
*/
|
|
25
31
|
param?: Partial<oas31.ParameterObject> & {
|
|
26
32
|
example?: TInferred;
|
|
27
33
|
examples?: Record<string, (oas31.ExampleObject & {
|
|
28
34
|
value: TInferred;
|
|
29
35
|
}) | oas31.ReferenceObject>;
|
|
30
36
|
/**
|
|
31
|
-
*
|
|
37
|
+
* Used to output this Zod Schema in the components parameters section. Any usage of this Zod Schema will then be transformed into a $ref.
|
|
32
38
|
*/
|
|
33
39
|
ref?: string;
|
|
34
40
|
};
|
|
41
|
+
/**
|
|
42
|
+
* Used to set data for a response header
|
|
43
|
+
*/
|
|
35
44
|
header?: Partial<oas31.HeaderObject & oas30.HeaderObject> & {
|
|
36
45
|
/**
|
|
37
|
-
*
|
|
46
|
+
* Used to output this Zod Schema in the components headers section. Any usage of this Zod Schema will then be transformed into a $ref.
|
|
38
47
|
*/
|
|
39
48
|
ref?: string;
|
|
40
49
|
};
|
|
50
|
+
/**
|
|
51
|
+
* Used to override the generated type. If this is provided no metadata will be generated.
|
|
52
|
+
*/
|
|
53
|
+
type?: SchemaObject['type'];
|
|
41
54
|
}
|
|
42
55
|
interface ZodOpenApiExtendMetadata {
|
|
43
56
|
extends: ZodObject<any, any, any, any, any>;
|
|
44
57
|
}
|
|
45
58
|
declare module 'zod' {
|
|
46
59
|
interface ZodType<Output, Def extends ZodTypeDef, Input = Output> {
|
|
60
|
+
/**
|
|
61
|
+
* Add OpenAPI metadata to a Zod Type
|
|
62
|
+
*/
|
|
47
63
|
openapi<T extends ZodTypeAny>(this: T, metadata: ZodOpenApiMetadata<T>): T;
|
|
48
64
|
}
|
|
49
65
|
interface ZodTypeDef {
|
|
66
|
+
/**
|
|
67
|
+
* OpenAPI metadata
|
|
68
|
+
*/
|
|
50
69
|
openapi?: ZodOpenApiMetadata<ZodTypeAny>;
|
|
51
70
|
}
|
|
52
71
|
interface ZodObjectDef<T extends ZodRawShape = ZodRawShape, UnknownKeys extends UnknownKeysParam = UnknownKeysParam, Catchall extends ZodTypeAny = ZodTypeAny> extends ZodTypeDef {
|
|
@@ -88,7 +88,7 @@ export interface OperationObject extends ISpecificationExtension {
|
|
|
88
88
|
operationId?: string;
|
|
89
89
|
parameters?: (ParameterObject | ReferenceObject)[];
|
|
90
90
|
requestBody?: RequestBodyObject | ReferenceObject;
|
|
91
|
-
responses
|
|
91
|
+
responses?: ResponsesObject;
|
|
92
92
|
callbacks?: CallbacksObject;
|
|
93
93
|
deprecated?: boolean;
|
|
94
94
|
security?: SecurityRequirementObject[];
|
|
@@ -232,6 +232,7 @@ export interface SchemaObject extends ISpecificationExtension {
|
|
|
232
232
|
title?: string;
|
|
233
233
|
multipleOf?: number;
|
|
234
234
|
maximum?: number;
|
|
235
|
+
const?: any;
|
|
235
236
|
exclusiveMaximum?: number;
|
|
236
237
|
minimum?: number;
|
|
237
238
|
exclusiveMinimum?: number;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zod-openapi",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.15.0",
|
|
4
4
|
"description": "Convert Zod Schemas to OpenAPI v3.x documentation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -37,24 +37,23 @@
|
|
|
37
37
|
"lib*/**/*.json"
|
|
38
38
|
],
|
|
39
39
|
"scripts": {
|
|
40
|
-
"build": "
|
|
40
|
+
"build": "pnpm copy:types && node esbuild.mjs && node esbuild.esm.mjs && tsc --allowJS false --declaration --emitDeclarationOnly --outDir lib-types --project tsconfig.build.json",
|
|
41
41
|
"copy:types": "skuba node scripts/copyTypes.ts",
|
|
42
42
|
"create:docs": " skuba node examples/simple/createSchema.ts && redocly build-docs examples/simple/openapi.yml --output=examples/simple/redoc-static.html",
|
|
43
43
|
"format": "skuba format",
|
|
44
44
|
"lint": "skuba lint",
|
|
45
|
-
"prepare": "
|
|
45
|
+
"prepare": "pnpm build",
|
|
46
46
|
"test": "skuba test",
|
|
47
47
|
"test:ci": "skuba test --coverage",
|
|
48
48
|
"test:watch": "skuba test --watch"
|
|
49
49
|
},
|
|
50
|
-
"dependencies": {},
|
|
51
50
|
"devDependencies": {
|
|
52
|
-
"@redocly/cli": "1.
|
|
51
|
+
"@redocly/cli": "1.10.5",
|
|
53
52
|
"@types/node": "^20.3.0",
|
|
54
53
|
"eslint-plugin-zod-openapi": "^0.1.0",
|
|
55
|
-
"openapi3-ts": "4.
|
|
56
|
-
"skuba": "7.
|
|
57
|
-
"yaml": "2.
|
|
54
|
+
"openapi3-ts": "4.3.1",
|
|
55
|
+
"skuba": "7.5.1",
|
|
56
|
+
"yaml": "2.4.1",
|
|
58
57
|
"zod": "3.22.4"
|
|
59
58
|
},
|
|
60
59
|
"peerDependencies": {
|
|
@@ -71,6 +70,6 @@
|
|
|
71
70
|
"entryPoint": "src/index.ts",
|
|
72
71
|
"template": "oss-npm-package",
|
|
73
72
|
"type": "package",
|
|
74
|
-
"version": "
|
|
73
|
+
"version": "7.4.1"
|
|
75
74
|
}
|
|
76
75
|
}
|