zod-openapi 2.16.0 → 2.17.0-beta.2

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
@@ -29,12 +29,23 @@ pnpm install zod zod-openapi
29
29
 
30
30
  ## Usage
31
31
 
32
- ### `extendZodWithOpenApi`
32
+ ### Extend Zod
33
33
 
34
34
  This mutates Zod to add an extra `.openapi()` method. Call this at the top of your entry point(s).
35
35
 
36
- ```typescript
36
+ ```ts
37
+ import 'zod-openapi/extend';
37
38
  import { z } from 'zod';
39
+
40
+ z.string().openapi({ description: 'hello world!', example: 'hello world' });
41
+ ```
42
+
43
+ #### Manual
44
+
45
+ This is useful if you have a different instance of Zod that you would like to extend.
46
+
47
+ ```typescript
48
+ import { z } from 'another-lib';
38
49
  import { extendZodWithOpenApi } from 'zod-openapi';
39
50
 
40
51
  extendZodWithOpenApi(z);
@@ -65,11 +76,10 @@ Creates an OpenAPI documentation object
65
76
  import { z } from 'zod';
66
77
  import { createDocument, extendZodWithOpenApi } from 'zod-openapi';
67
78
 
68
- extendZodWithOpenApi(z);
69
-
70
79
  const jobId = z.string().openapi({
71
- description: 'Job ID',
80
+ description: 'A unique identifier for a job',
72
81
  example: '12345',
82
+ ref: 'jobId',
73
83
  });
74
84
 
75
85
  const title = z.string().openapi({
@@ -122,10 +132,9 @@ Generates the following object:
122
132
  {
123
133
  "in": "path",
124
134
  "name": "jobId",
135
+ "description": "A unique identifier for a job",
125
136
  "schema": {
126
- "type": "string",
127
- "description": "Job ID",
128
- "example": "12345"
137
+ "$ref": "#/components/schemas/jobId"
129
138
  }
130
139
  }
131
140
  ],
@@ -155,9 +164,7 @@ Generates the following object:
155
164
  "type": "object",
156
165
  "properties": {
157
166
  "jobId": {
158
- "type": "string",
159
- "description": "Job ID",
160
- "example": "12345"
167
+ "$ref": "#/components/schemas/jobId"
161
168
  },
162
169
  "title": {
163
170
  "type": "string",
@@ -173,6 +180,15 @@ Generates the following object:
173
180
  }
174
181
  }
175
182
  }
183
+ },
184
+ "components": {
185
+ "schemas": {
186
+ "jobId": {
187
+ "type": "string",
188
+ "description": "A unique identifier for a job",
189
+ "example": "12345"
190
+ }
191
+ }
176
192
  }
177
193
  }
178
194
  ```
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+
16
+ // src/extend.ts
17
+ var extend_exports = {};
18
+ module.exports = __toCommonJS(extend_exports);
19
+ var import_zod = require("zod");
20
+
21
+ // src/extendZod.ts
22
+ function extendZodWithOpenApi(zod) {
23
+ if (typeof zod.ZodType.prototype.openapi !== "undefined") {
24
+ return;
25
+ }
26
+ zod.ZodType.prototype.openapi = function(openapi) {
27
+ const result = new this.constructor({
28
+ ...this._def,
29
+ openapi
30
+ });
31
+ return result;
32
+ };
33
+ const zodObjectExtend = zod.ZodObject.prototype.extend;
34
+ zod.ZodObject.prototype.extend = function(...args) {
35
+ const extendResult = zodObjectExtend.apply(this, args);
36
+ extendResult._def.extendMetadata = {
37
+ extends: this
38
+ };
39
+ delete extendResult._def.openapi;
40
+ return extendResult;
41
+ };
42
+ const zodObjectOmit = zod.ZodObject.prototype.omit;
43
+ zod.ZodObject.prototype.omit = function(...args) {
44
+ const omitResult = zodObjectOmit.apply(this, args);
45
+ delete omitResult._def.extendMetadata;
46
+ delete omitResult._def.openapi;
47
+ return omitResult;
48
+ };
49
+ const zodObjectPick = zod.ZodObject.prototype.pick;
50
+ zod.ZodObject.prototype.pick = function(...args) {
51
+ const pickResult = zodObjectPick.apply(this, args);
52
+ delete pickResult._def.extendMetadata;
53
+ delete pickResult._def.openapi;
54
+ return pickResult;
55
+ };
56
+ }
57
+
58
+ // src/extend.ts
59
+ extendZodWithOpenApi(import_zod.z);
@@ -0,0 +1,42 @@
1
+ // src/extend.ts
2
+ import { z } from "zod";
3
+
4
+ // src/extendZod.ts
5
+ function extendZodWithOpenApi(zod) {
6
+ if (typeof zod.ZodType.prototype.openapi !== "undefined") {
7
+ return;
8
+ }
9
+ zod.ZodType.prototype.openapi = function(openapi) {
10
+ const result = new this.constructor({
11
+ ...this._def,
12
+ openapi
13
+ });
14
+ return result;
15
+ };
16
+ const zodObjectExtend = zod.ZodObject.prototype.extend;
17
+ zod.ZodObject.prototype.extend = function(...args) {
18
+ const extendResult = zodObjectExtend.apply(this, args);
19
+ extendResult._def.extendMetadata = {
20
+ extends: this
21
+ };
22
+ delete extendResult._def.openapi;
23
+ return extendResult;
24
+ };
25
+ const zodObjectOmit = zod.ZodObject.prototype.omit;
26
+ zod.ZodObject.prototype.omit = function(...args) {
27
+ const omitResult = zodObjectOmit.apply(this, args);
28
+ delete omitResult._def.extendMetadata;
29
+ delete omitResult._def.openapi;
30
+ return omitResult;
31
+ };
32
+ const zodObjectPick = zod.ZodObject.prototype.pick;
33
+ zod.ZodObject.prototype.pick = function(...args) {
34
+ const pickResult = zodObjectPick.apply(this, args);
35
+ delete pickResult._def.extendMetadata;
36
+ delete pickResult._def.openapi;
37
+ return pickResult;
38
+ };
39
+ }
40
+
41
+ // src/extend.ts
42
+ extendZodWithOpenApi(z);
@@ -0,0 +1 @@
1
+ export * from './extendZodTypes';
@@ -1,76 +1,3 @@
1
- import type { ZodDate, ZodObject, ZodTypeAny, z } from 'zod';
2
- import type { CreationType } from './create/components';
3
- import type { oas30, oas31 } from './openapi3-ts/dist';
4
- type SchemaObject = oas30.SchemaObject & oas31.SchemaObject;
5
- /**
6
- * zod-openapi metadata
7
- */
8
- interface ZodOpenApiMetadata<T extends ZodTypeAny, TInferred = z.input<T> | z.output<T>> extends SchemaObject {
9
- example?: TInferred;
10
- examples?: [TInferred, ...TInferred[]];
11
- default?: T extends ZodDate ? string : TInferred;
12
- /**
13
- * Used to set the output of a ZodUnion to be `oneOf` instead of `allOf`
14
- */
15
- unionOneOf?: boolean;
16
- /**
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.
18
- */
19
- ref?: string;
20
- /**
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`
22
- */
23
- refType?: CreationType;
24
- /**
25
- * Used to set the created type of an effect.
26
- */
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
- */
31
- param?: Partial<oas31.ParameterObject> & {
32
- example?: TInferred;
33
- examples?: Record<string, (oas31.ExampleObject & {
34
- value: TInferred;
35
- }) | oas31.ReferenceObject>;
36
- /**
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.
38
- */
39
- ref?: string;
40
- };
41
- /**
42
- * Used to set data for a response header
43
- */
44
- header?: Partial<oas31.HeaderObject & oas30.HeaderObject> & {
45
- /**
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.
47
- */
48
- ref?: string;
49
- };
50
- /**
51
- * Used to override the generated type. If this is provided no metadata will be generated.
52
- */
53
- type?: SchemaObject['type'];
54
- }
55
- interface ZodOpenApiExtendMetadata {
56
- extends: ZodObject<any, any, any, any, any>;
57
- }
58
- declare module 'zod' {
59
- interface ZodType {
60
- /**
61
- * Add OpenAPI metadata to a Zod Type
62
- */
63
- openapi<T extends ZodTypeAny>(this: T, metadata: ZodOpenApiMetadata<T>): T;
64
- }
65
- interface ZodTypeDef {
66
- /**
67
- * OpenAPI metadata
68
- */
69
- openapi?: ZodOpenApiMetadata<ZodTypeAny>;
70
- }
71
- interface ZodObjectDef {
72
- extendMetadata?: ZodOpenApiExtendMetadata;
73
- }
74
- }
1
+ import type { z } from 'zod';
2
+ import './extendZodTypes';
75
3
  export declare function extendZodWithOpenApi(zod: typeof z): void;
76
- export {};
@@ -0,0 +1,75 @@
1
+ import type { ZodDate, ZodObject, ZodTypeAny, z } from 'zod';
2
+ import type { CreationType } from './create/components';
3
+ import type { oas30, oas31 } from './openapi3-ts/dist';
4
+ type SchemaObject = oas30.SchemaObject & oas31.SchemaObject;
5
+ /**
6
+ * zod-openapi metadata
7
+ */
8
+ interface ZodOpenApiMetadata<T extends ZodTypeAny, TInferred = z.input<T> | z.output<T>> extends SchemaObject {
9
+ example?: TInferred;
10
+ examples?: [TInferred, ...TInferred[]];
11
+ default?: T extends ZodDate ? string : TInferred;
12
+ /**
13
+ * Used to set the output of a ZodUnion to be `oneOf` instead of `allOf`
14
+ */
15
+ unionOneOf?: boolean;
16
+ /**
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.
18
+ */
19
+ ref?: string;
20
+ /**
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`
22
+ */
23
+ refType?: CreationType;
24
+ /**
25
+ * Used to set the created type of an effect.
26
+ */
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
+ */
31
+ param?: Partial<oas31.ParameterObject> & {
32
+ example?: TInferred;
33
+ examples?: Record<string, (oas31.ExampleObject & {
34
+ value: TInferred;
35
+ }) | oas31.ReferenceObject>;
36
+ /**
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.
38
+ */
39
+ ref?: string;
40
+ };
41
+ /**
42
+ * Used to set data for a response header
43
+ */
44
+ header?: Partial<oas31.HeaderObject & oas30.HeaderObject> & {
45
+ /**
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.
47
+ */
48
+ ref?: string;
49
+ };
50
+ /**
51
+ * Used to override the generated type. If this is provided no metadata will be generated.
52
+ */
53
+ type?: SchemaObject['type'];
54
+ }
55
+ interface ZodOpenApiExtendMetadata {
56
+ extends: ZodObject<any, any, any, any, any>;
57
+ }
58
+ declare module 'zod' {
59
+ interface ZodType {
60
+ /**
61
+ * Add OpenAPI metadata to a Zod Type
62
+ */
63
+ openapi<T extends ZodTypeAny>(this: T, metadata: ZodOpenApiMetadata<T>): T;
64
+ }
65
+ interface ZodTypeDef {
66
+ /**
67
+ * OpenAPI metadata
68
+ */
69
+ openapi?: ZodOpenApiMetadata<ZodTypeAny>;
70
+ }
71
+ interface ZodObjectDef {
72
+ extendMetadata?: ZodOpenApiExtendMetadata;
73
+ }
74
+ }
75
+ export {};
@@ -38,5 +38,5 @@ type ZodTypeMap = {
38
38
  ZodVoid: ZodVoid;
39
39
  };
40
40
  export declare const isZodType: <K extends keyof ZodTypeMap>(zodType: unknown, typeName: K) => zodType is ZodTypeMap[K];
41
- export declare const isAnyZodType: (zodType: unknown) => zodType is ZodType<unknown, ZodTypeDef, unknown>;
41
+ export declare const isAnyZodType: (zodType: unknown) => zodType is ZodType<any, ZodTypeDef, any>;
42
42
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod-openapi",
3
- "version": "2.16.0",
3
+ "version": "2.17.0-beta.2",
4
4
  "description": "Convert Zod Schemas to OpenAPI v3.x documentation",
5
5
  "keywords": [
6
6
  "typescript",
@@ -20,17 +20,34 @@
20
20
  "url": "git+ssh://git@github.com/samchungy/zod-openapi.git"
21
21
  },
22
22
  "license": "MIT",
23
- "sideEffects": false,
23
+ "sideEffects": [
24
+ "./src/extend.ts",
25
+ "./lib-esm/extend.mjs",
26
+ "./lib-commonjs/extend.js"
27
+ ],
24
28
  "exports": {
25
29
  ".": {
26
30
  "import": "./lib-esm/index.mjs",
27
31
  "require": "./lib-commonjs/index.js",
28
32
  "types": "./lib-types/index.d.ts"
33
+ },
34
+ "./extend": {
35
+ "import": "./lib-esm/extend.mjs",
36
+ "require": "./lib-commonjs/extend.js",
37
+ "types": "./lib-types/extend.d.ts"
29
38
  }
30
39
  },
31
40
  "main": "./lib-commonjs/index.js",
32
41
  "module": "./lib-esm/index.mjs",
33
42
  "types": "./lib-types/index.d.ts",
43
+ "typesVersions": {
44
+ "*": {
45
+ "*": [
46
+ "lib-types/index.d.ts",
47
+ "lib-types/extend.d.ts"
48
+ ]
49
+ }
50
+ },
34
51
  "files": [
35
52
  "lib*/**/*.d.ts",
36
53
  "lib*/**/*.{js,mjs}{,.map}",
@@ -52,9 +69,9 @@
52
69
  "@types/node": "^20.3.0",
53
70
  "eslint-plugin-zod-openapi": "^0.1.0",
54
71
  "openapi3-ts": "4.3.1",
55
- "skuba": "8.0.0",
72
+ "skuba": "8.0.1",
56
73
  "yaml": "2.4.1",
57
- "zod": "3.23.0-beta.2"
74
+ "zod": "3.23.3"
58
75
  },
59
76
  "peerDependencies": {
60
77
  "zod": "^3.21.4"