zod-openapi 2.16.0 → 2.17.0-beta.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 CHANGED
@@ -29,12 +29,25 @@ 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
+ #### Automatic
37
+
38
+ ```ts
39
+ import 'zod-openapi/extend';
37
40
  import { z } from 'zod';
41
+
42
+ z.string().openapi({ description: 'hello world!', example: 'hello world' });
43
+ ```
44
+
45
+ #### Manual
46
+
47
+ This is useful if you have a different instance of Zod that you would like to extend.
48
+
49
+ ```typescript
50
+ import { z } from 'another-lib';
38
51
  import { extendZodWithOpenApi } from 'zod-openapi';
39
52
 
40
53
  extendZodWithOpenApi(z);
@@ -65,11 +78,10 @@ Creates an OpenAPI documentation object
65
78
  import { z } from 'zod';
66
79
  import { createDocument, extendZodWithOpenApi } from 'zod-openapi';
67
80
 
68
- extendZodWithOpenApi(z);
69
-
70
81
  const jobId = z.string().openapi({
71
- description: 'Job ID',
82
+ description: 'A unique identifier for a job',
72
83
  example: '12345',
84
+ ref: 'jobId',
73
85
  });
74
86
 
75
87
  const title = z.string().openapi({
@@ -122,10 +134,9 @@ Generates the following object:
122
134
  {
123
135
  "in": "path",
124
136
  "name": "jobId",
137
+ "description": "A unique identifier for a job",
125
138
  "schema": {
126
- "type": "string",
127
- "description": "Job ID",
128
- "example": "12345"
139
+ "$ref": "#/components/schemas/jobId"
129
140
  }
130
141
  }
131
142
  ],
@@ -155,9 +166,7 @@ Generates the following object:
155
166
  "type": "object",
156
167
  "properties": {
157
168
  "jobId": {
158
- "type": "string",
159
- "description": "Job ID",
160
- "example": "12345"
169
+ "$ref": "#/components/schemas/jobId"
161
170
  },
162
171
  "title": {
163
172
  "type": "string",
@@ -173,6 +182,15 @@ Generates the following object:
173
182
  }
174
183
  }
175
184
  }
185
+ },
186
+ "components": {
187
+ "schemas": {
188
+ "jobId": {
189
+ "type": "string",
190
+ "description": "A unique identifier for a job",
191
+ "example": "12345"
192
+ }
193
+ }
176
194
  }
177
195
  }
178
196
  ```
@@ -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 type * from './extendZod';
@@ -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.1",
4
4
  "description": "Convert Zod Schemas to OpenAPI v3.x documentation",
5
5
  "keywords": [
6
6
  "typescript",
@@ -20,12 +20,21 @@
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",
@@ -52,9 +61,9 @@
52
61
  "@types/node": "^20.3.0",
53
62
  "eslint-plugin-zod-openapi": "^0.1.0",
54
63
  "openapi3-ts": "4.3.1",
55
- "skuba": "8.0.0",
64
+ "skuba": "8.0.1",
56
65
  "yaml": "2.4.1",
57
- "zod": "3.23.0-beta.2"
66
+ "zod": "3.23.3"
58
67
  },
59
68
  "peerDependencies": {
60
69
  "zod": "^3.21.4"