zod-openapi 2.11.0 → 2.12.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 CHANGED
@@ -182,7 +182,7 @@ Query, Path, Header & Cookie parameters can be created using the `requestParams`
182
182
  ```typescript
183
183
  createDocument({
184
184
  paths: {
185
- '/jobs/:a': {
185
+ '/jobs/{a}': {
186
186
  put: {
187
187
  requestParams: {
188
188
  path: z.object({ a: z.string() }),
@@ -201,7 +201,7 @@ If you would like to declare parameters in a more traditional way you may also d
201
201
  ```ts
202
202
  createDocument({
203
203
  paths: {
204
- '/jobs/:a': {
204
+ '/jobs/{a}': {
205
205
  put: {
206
206
  parameters: [
207
207
  z.string().openapi({
@@ -521,6 +521,7 @@ For example in `z.string().nullable()` will be rendered differently
521
521
  - ZodLiteral
522
522
  - ZodNativeEnum
523
523
  - supporting `string`, `number` and combined enums.
524
+ - ZodNever
524
525
  - ZodNull
525
526
  - ZodNullable
526
527
  - ZodNumber
@@ -543,6 +544,7 @@ For example in `z.string().nullable()` will be rendered differently
543
544
  - ZodTuple
544
545
  - `items` mapping for `.rest()`
545
546
  - `prefixItems` mapping for OpenAPI 3.1.0+
547
+ - ZodUndefined
546
548
  - ZodUnion
547
549
  - By default it outputs an `allOf` schema. Use `unionOneOf` to change this to output `oneOf` instead.
548
550
  - ZodUnknown
@@ -182,17 +182,11 @@ var createLiteralSchema = (zodLiteral) => ({
182
182
  // src/create/schema/parsers/manual.ts
183
183
  var createManualTypeSchema = (zodSchema, state) => {
184
184
  if (!zodSchema._def.openapi?.type) {
185
- const zodType = zodSchema.constructor.name;
186
- if (isZodType(zodSchema, "ZodEffects")) {
187
- const schemaName = `${zodType} - ${zodSchema._def.effect.type}`;
188
- throw new Error(
189
- `Unknown schema ${schemaName} at ${state.path.join(
190
- " > "
191
- )}. Please assign it a manual 'type', wrap it in a ZodPipeline or change the 'effectType'.`
192
- );
193
- }
185
+ const schemaName = zodSchema.constructor.name;
194
186
  throw new Error(
195
- `Unknown schema ${zodType}. Please assign it a manual 'type'.`
187
+ `Unknown schema ${schemaName} at ${state.path.join(
188
+ " > "
189
+ )}. Please assign it a manual 'type'.`
196
190
  );
197
191
  }
198
192
  return {
@@ -357,7 +351,7 @@ var createOptionalSchema = (zodOptional, state) => (
357
351
  createSchemaObject(zodOptional.unwrap(), state, ["optional"])
358
352
  );
359
353
  var isOptionalSchema = (zodSchema, state) => {
360
- if (isZodType(zodSchema, "ZodOptional")) {
354
+ if (isZodType(zodSchema, "ZodOptional") || isZodType(zodSchema, "ZodNever") || isZodType(zodSchema, "ZodUndefined")) {
361
355
  return true;
362
356
  }
363
357
  if (isZodType(zodSchema, "ZodDefault")) {
@@ -497,6 +491,9 @@ var mapRequired = (shape, state) => {
497
491
  };
498
492
  var mapProperties = (shape, state) => Object.entries(shape).reduce(
499
493
  (acc, [key, zodSchema]) => {
494
+ if (isZodType(zodSchema, "ZodNever") || isZodType(zodSchema, "ZodUndefined")) {
495
+ return acc;
496
+ }
500
497
  acc[key] = createSchemaObject(zodSchema, state, [`property: ${key}`]);
501
498
  return acc;
502
499
  },
@@ -554,7 +551,6 @@ var createRecordSchema = (zodRecord, state) => {
554
551
  if (satisfiesVersion(state.components.openapi, "3.1.0") && "type" in renderedKeySchema && renderedKeySchema.type === "string" && Object.keys(renderedKeySchema).length > 1) {
555
552
  return {
556
553
  type: "object",
557
- // @ts-expect-error FIXME: https://github.com/metadevpro/openapi3-ts/pull/120
558
554
  propertyNames: keySchema,
559
555
  additionalProperties
560
556
  };
@@ -682,7 +678,7 @@ var mapStringFormat = (zodStringChecks) => {
682
678
  // src/create/schema/parsers/transform.ts
683
679
  var createTransformSchema = (zodTransform, state) => {
684
680
  if (zodTransform._def.openapi?.effectType === "output") {
685
- return createManualTypeSchema(zodTransform, state);
681
+ return createManualOutputTransformSchema(zodTransform, state);
686
682
  }
687
683
  if (zodTransform._def.openapi?.effectType === "input") {
688
684
  return createSchemaObject(zodTransform._def.schema, state, [
@@ -690,13 +686,27 @@ var createTransformSchema = (zodTransform, state) => {
690
686
  ]);
691
687
  }
692
688
  if (state.type === "output") {
693
- return createManualTypeSchema(zodTransform, state);
689
+ return createManualOutputTransformSchema(zodTransform, state);
694
690
  }
695
691
  state.effectType = "input";
696
692
  return createSchemaObject(zodTransform._def.schema, state, [
697
693
  "transform input"
698
694
  ]);
699
695
  };
696
+ var createManualOutputTransformSchema = (zodTransform, state) => {
697
+ if (!zodTransform._def.openapi?.type) {
698
+ const zodType = zodTransform.constructor.name;
699
+ const schemaName = `${zodType} - ${zodTransform._def.effect.type}`;
700
+ throw new Error(
701
+ `Failed to determine a type for ${schemaName} at ${state.path.join(
702
+ " > "
703
+ )}. Please change the 'effectType' to 'input', wrap it in a ZodPipeline or assign it a manual 'type'.`
704
+ );
705
+ }
706
+ return {
707
+ type: zodTransform._def.openapi.type
708
+ };
709
+ };
700
710
  var throwTransformError = (zodType, state) => {
701
711
  throw new Error(
702
712
  `${JSON.stringify(zodType)} at ${state.path.join(
@@ -988,7 +998,9 @@ var createBaseParameter = (schema, components, subpath) => {
988
998
  "schema"
989
999
  ]);
990
1000
  const required = !isOptionalSchema(schema, state);
1001
+ const description = schema._def.openapi?.description ?? schema._def.description;
991
1002
  return {
1003
+ ...description && { description },
992
1004
  ...rest,
993
1005
  ...schema && { schema: schemaObject },
994
1006
  ...required && { required }
package/lib-esm/index.js CHANGED
@@ -158,17 +158,11 @@ var createLiteralSchema = (zodLiteral) => ({
158
158
  // src/create/schema/parsers/manual.ts
159
159
  var createManualTypeSchema = (zodSchema, state) => {
160
160
  if (!zodSchema._def.openapi?.type) {
161
- const zodType = zodSchema.constructor.name;
162
- if (isZodType(zodSchema, "ZodEffects")) {
163
- const schemaName = `${zodType} - ${zodSchema._def.effect.type}`;
164
- throw new Error(
165
- `Unknown schema ${schemaName} at ${state.path.join(
166
- " > "
167
- )}. Please assign it a manual 'type', wrap it in a ZodPipeline or change the 'effectType'.`
168
- );
169
- }
161
+ const schemaName = zodSchema.constructor.name;
170
162
  throw new Error(
171
- `Unknown schema ${zodType}. Please assign it a manual 'type'.`
163
+ `Unknown schema ${schemaName} at ${state.path.join(
164
+ " > "
165
+ )}. Please assign it a manual 'type'.`
172
166
  );
173
167
  }
174
168
  return {
@@ -333,7 +327,7 @@ var createOptionalSchema = (zodOptional, state) => (
333
327
  createSchemaObject(zodOptional.unwrap(), state, ["optional"])
334
328
  );
335
329
  var isOptionalSchema = (zodSchema, state) => {
336
- if (isZodType(zodSchema, "ZodOptional")) {
330
+ if (isZodType(zodSchema, "ZodOptional") || isZodType(zodSchema, "ZodNever") || isZodType(zodSchema, "ZodUndefined")) {
337
331
  return true;
338
332
  }
339
333
  if (isZodType(zodSchema, "ZodDefault")) {
@@ -473,6 +467,9 @@ var mapRequired = (shape, state) => {
473
467
  };
474
468
  var mapProperties = (shape, state) => Object.entries(shape).reduce(
475
469
  (acc, [key, zodSchema]) => {
470
+ if (isZodType(zodSchema, "ZodNever") || isZodType(zodSchema, "ZodUndefined")) {
471
+ return acc;
472
+ }
476
473
  acc[key] = createSchemaObject(zodSchema, state, [`property: ${key}`]);
477
474
  return acc;
478
475
  },
@@ -530,7 +527,6 @@ var createRecordSchema = (zodRecord, state) => {
530
527
  if (satisfiesVersion(state.components.openapi, "3.1.0") && "type" in renderedKeySchema && renderedKeySchema.type === "string" && Object.keys(renderedKeySchema).length > 1) {
531
528
  return {
532
529
  type: "object",
533
- // @ts-expect-error FIXME: https://github.com/metadevpro/openapi3-ts/pull/120
534
530
  propertyNames: keySchema,
535
531
  additionalProperties
536
532
  };
@@ -658,7 +654,7 @@ var mapStringFormat = (zodStringChecks) => {
658
654
  // src/create/schema/parsers/transform.ts
659
655
  var createTransformSchema = (zodTransform, state) => {
660
656
  if (zodTransform._def.openapi?.effectType === "output") {
661
- return createManualTypeSchema(zodTransform, state);
657
+ return createManualOutputTransformSchema(zodTransform, state);
662
658
  }
663
659
  if (zodTransform._def.openapi?.effectType === "input") {
664
660
  return createSchemaObject(zodTransform._def.schema, state, [
@@ -666,13 +662,27 @@ var createTransformSchema = (zodTransform, state) => {
666
662
  ]);
667
663
  }
668
664
  if (state.type === "output") {
669
- return createManualTypeSchema(zodTransform, state);
665
+ return createManualOutputTransformSchema(zodTransform, state);
670
666
  }
671
667
  state.effectType = "input";
672
668
  return createSchemaObject(zodTransform._def.schema, state, [
673
669
  "transform input"
674
670
  ]);
675
671
  };
672
+ var createManualOutputTransformSchema = (zodTransform, state) => {
673
+ if (!zodTransform._def.openapi?.type) {
674
+ const zodType = zodTransform.constructor.name;
675
+ const schemaName = `${zodType} - ${zodTransform._def.effect.type}`;
676
+ throw new Error(
677
+ `Failed to determine a type for ${schemaName} at ${state.path.join(
678
+ " > "
679
+ )}. Please change the 'effectType' to 'input', wrap it in a ZodPipeline or assign it a manual 'type'.`
680
+ );
681
+ }
682
+ return {
683
+ type: zodTransform._def.openapi.type
684
+ };
685
+ };
676
686
  var throwTransformError = (zodType, state) => {
677
687
  throw new Error(
678
688
  `${JSON.stringify(zodType)} at ${state.path.join(
@@ -964,7 +974,9 @@ var createBaseParameter = (schema, components, subpath) => {
964
974
  "schema"
965
975
  ]);
966
976
  const required = !isOptionalSchema(schema, state);
977
+ const description = schema._def.openapi?.description ?? schema._def.description;
967
978
  return {
979
+ ...description && { description },
968
980
  ...rest,
969
981
  ...schema && { schema: schemaObject },
970
982
  ...required && { required }
@@ -2,4 +2,5 @@ import type { ZodEffects, ZodType, ZodTypeAny, input, output } from 'zod';
2
2
  import type { oas31 } from '../../../openapi3-ts/dist';
3
3
  import { type SchemaState } from '../../schema';
4
4
  export declare const createTransformSchema: <T extends ZodTypeAny, Output = output<T>, Input = input<T>>(zodTransform: ZodEffects<T, Output, Input>, state: SchemaState) => oas31.SchemaObject | oas31.ReferenceObject;
5
+ export declare const createManualOutputTransformSchema: <T extends ZodTypeAny, Output = output<T>, Input = input<T>>(zodTransform: ZodEffects<T, Output, Input>, state: SchemaState) => oas31.SchemaObject;
5
6
  export declare const throwTransformError: (zodType: ZodType, state: SchemaState) => never;
@@ -28,6 +28,7 @@ export interface ContactObject extends ISpecificationExtension {
28
28
  }
29
29
  export interface LicenseObject extends ISpecificationExtension {
30
30
  name: string;
31
+ identifier?: string;
31
32
  url?: string;
32
33
  }
33
34
  export interface ComponentsObject extends ISpecificationExtension {
@@ -225,6 +226,7 @@ export interface SchemaObject extends ISpecificationExtension {
225
226
  [propertyName: string]: SchemaObject | ReferenceObject;
226
227
  };
227
228
  additionalProperties?: SchemaObject | ReferenceObject | boolean;
229
+ propertyNames?: SchemaObject | ReferenceObject;
228
230
  description?: string;
229
231
  default?: any;
230
232
  title?: string;
@@ -244,6 +246,8 @@ export interface SchemaObject extends ISpecificationExtension {
244
246
  required?: string[];
245
247
  enum?: any[];
246
248
  prefixItems?: (SchemaObject | ReferenceObject)[];
249
+ contentMediaType?: string;
250
+ contentEncoding?: string;
247
251
  }
248
252
  export declare function isSchemaObject(schema: SchemaObject | ReferenceObject): schema is SchemaObject;
249
253
  export interface SchemasObject {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod-openapi",
3
- "version": "2.11.0",
3
+ "version": "2.12.0",
4
4
  "description": "Convert Zod Schemas to OpenAPI v3.x documentation",
5
5
  "keywords": [
6
6
  "typescript",
@@ -43,12 +43,12 @@
43
43
  },
44
44
  "dependencies": {},
45
45
  "devDependencies": {
46
- "@redocly/cli": "1.4.0",
46
+ "@redocly/cli": "1.6.0",
47
47
  "@types/node": "^20.3.0",
48
48
  "eslint-plugin-zod-openapi": "^0.1.0",
49
- "openapi3-ts": "4.1.2",
50
- "skuba": "7.2.0",
51
- "yaml": "2.3.3",
49
+ "openapi3-ts": "4.2.1",
50
+ "skuba": "7.3.1",
51
+ "yaml": "2.3.4",
52
52
  "zod": "3.22.4"
53
53
  },
54
54
  "peerDependencies": {