zod-openapi 3.3.0 → 4.0.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.
@@ -1,36 +1,91 @@
1
1
  "use strict";
2
+ const mergeOpenApi = (openapi, {
3
+ ref: _ref,
4
+ refType: _refType,
5
+ param: _param,
6
+ header: _header,
7
+ ...rest
8
+ } = {}) => ({
9
+ ...rest,
10
+ ...openapi
11
+ });
2
12
  function extendZodWithOpenApi(zod) {
3
13
  if (typeof zod.ZodType.prototype.openapi !== "undefined") {
4
14
  return;
5
15
  }
6
16
  zod.ZodType.prototype.openapi = function(openapi) {
17
+ const { zodOpenApi, ...rest } = this._def;
7
18
  const result = new this.constructor({
8
- ...this._def,
9
- openapi
19
+ ...rest,
20
+ zodOpenApi: {
21
+ openapi: mergeOpenApi(
22
+ openapi,
23
+ zodOpenApi == null ? void 0 : zodOpenApi.openapi
24
+ )
25
+ }
10
26
  });
27
+ result._def.zodOpenApi.current = result;
28
+ if (zodOpenApi) {
29
+ result._def.zodOpenApi.previous = this;
30
+ }
31
+ return result;
32
+ };
33
+ const zodDescribe = zod.ZodType.prototype.describe;
34
+ zod.ZodType.prototype.describe = function(...args) {
35
+ const result = zodDescribe.apply(this, args);
36
+ const def = result._def;
37
+ if (def.zodOpenApi) {
38
+ const cloned = { ...def.zodOpenApi };
39
+ cloned.openapi = mergeOpenApi({ description: args[0] }, cloned.openapi);
40
+ cloned.previous = this;
41
+ cloned.current = result;
42
+ def.zodOpenApi = cloned;
43
+ } else {
44
+ def.zodOpenApi = {
45
+ openapi: { description: args[0] },
46
+ current: result
47
+ };
48
+ }
11
49
  return result;
12
50
  };
13
51
  const zodObjectExtend = zod.ZodObject.prototype.extend;
14
52
  zod.ZodObject.prototype.extend = function(...args) {
15
53
  const extendResult = zodObjectExtend.apply(this, args);
16
- extendResult._def.extendMetadata = {
17
- extends: this
18
- };
19
- delete extendResult._def.openapi;
54
+ const zodOpenApi = extendResult._def.zodOpenApi;
55
+ if (zodOpenApi) {
56
+ const cloned = { ...zodOpenApi };
57
+ cloned.openapi = mergeOpenApi({}, cloned.openapi);
58
+ cloned.previous = this;
59
+ extendResult._def.zodOpenApi = cloned;
60
+ } else {
61
+ extendResult._def.zodOpenApi = {
62
+ previous: this
63
+ };
64
+ }
20
65
  return extendResult;
21
66
  };
22
67
  const zodObjectOmit = zod.ZodObject.prototype.omit;
23
68
  zod.ZodObject.prototype.omit = function(...args) {
24
69
  const omitResult = zodObjectOmit.apply(this, args);
25
- delete omitResult._def.extendMetadata;
26
- delete omitResult._def.openapi;
70
+ const zodOpenApi = omitResult._def.zodOpenApi;
71
+ if (zodOpenApi) {
72
+ const cloned = { ...zodOpenApi };
73
+ cloned.openapi = mergeOpenApi({}, cloned.openapi);
74
+ delete cloned.previous;
75
+ omitResult._def.zodOpenApi = cloned;
76
+ }
27
77
  return omitResult;
28
78
  };
29
79
  const zodObjectPick = zod.ZodObject.prototype.pick;
30
80
  zod.ZodObject.prototype.pick = function(...args) {
31
81
  const pickResult = zodObjectPick.apply(this, args);
32
- delete pickResult._def.extendMetadata;
33
- delete pickResult._def.openapi;
82
+ const zodOpenApi = pickResult._def.zodOpenApi;
83
+ if (zodOpenApi) {
84
+ const cloned = { ...zodOpenApi };
85
+ cloned.openapi = mergeOpenApi({}, cloned.openapi);
86
+ delete cloned.previous;
87
+ pickResult._def.zodOpenApi = cloned;
88
+ }
34
89
  return pickResult;
35
90
  };
36
91
  }
@@ -1,35 +1,90 @@
1
+ const mergeOpenApi = (openapi, {
2
+ ref: _ref,
3
+ refType: _refType,
4
+ param: _param,
5
+ header: _header,
6
+ ...rest
7
+ } = {}) => ({
8
+ ...rest,
9
+ ...openapi
10
+ });
1
11
  function extendZodWithOpenApi(zod) {
2
12
  if (typeof zod.ZodType.prototype.openapi !== "undefined") {
3
13
  return;
4
14
  }
5
15
  zod.ZodType.prototype.openapi = function(openapi) {
16
+ const { zodOpenApi, ...rest } = this._def;
6
17
  const result = new this.constructor({
7
- ...this._def,
8
- openapi
18
+ ...rest,
19
+ zodOpenApi: {
20
+ openapi: mergeOpenApi(
21
+ openapi,
22
+ zodOpenApi == null ? void 0 : zodOpenApi.openapi
23
+ )
24
+ }
9
25
  });
26
+ result._def.zodOpenApi.current = result;
27
+ if (zodOpenApi) {
28
+ result._def.zodOpenApi.previous = this;
29
+ }
30
+ return result;
31
+ };
32
+ const zodDescribe = zod.ZodType.prototype.describe;
33
+ zod.ZodType.prototype.describe = function(...args) {
34
+ const result = zodDescribe.apply(this, args);
35
+ const def = result._def;
36
+ if (def.zodOpenApi) {
37
+ const cloned = { ...def.zodOpenApi };
38
+ cloned.openapi = mergeOpenApi({ description: args[0] }, cloned.openapi);
39
+ cloned.previous = this;
40
+ cloned.current = result;
41
+ def.zodOpenApi = cloned;
42
+ } else {
43
+ def.zodOpenApi = {
44
+ openapi: { description: args[0] },
45
+ current: result
46
+ };
47
+ }
10
48
  return result;
11
49
  };
12
50
  const zodObjectExtend = zod.ZodObject.prototype.extend;
13
51
  zod.ZodObject.prototype.extend = function(...args) {
14
52
  const extendResult = zodObjectExtend.apply(this, args);
15
- extendResult._def.extendMetadata = {
16
- extends: this
17
- };
18
- delete extendResult._def.openapi;
53
+ const zodOpenApi = extendResult._def.zodOpenApi;
54
+ if (zodOpenApi) {
55
+ const cloned = { ...zodOpenApi };
56
+ cloned.openapi = mergeOpenApi({}, cloned.openapi);
57
+ cloned.previous = this;
58
+ extendResult._def.zodOpenApi = cloned;
59
+ } else {
60
+ extendResult._def.zodOpenApi = {
61
+ previous: this
62
+ };
63
+ }
19
64
  return extendResult;
20
65
  };
21
66
  const zodObjectOmit = zod.ZodObject.prototype.omit;
22
67
  zod.ZodObject.prototype.omit = function(...args) {
23
68
  const omitResult = zodObjectOmit.apply(this, args);
24
- delete omitResult._def.extendMetadata;
25
- delete omitResult._def.openapi;
69
+ const zodOpenApi = omitResult._def.zodOpenApi;
70
+ if (zodOpenApi) {
71
+ const cloned = { ...zodOpenApi };
72
+ cloned.openapi = mergeOpenApi({}, cloned.openapi);
73
+ delete cloned.previous;
74
+ omitResult._def.zodOpenApi = cloned;
75
+ }
26
76
  return omitResult;
27
77
  };
28
78
  const zodObjectPick = zod.ZodObject.prototype.pick;
29
79
  zod.ZodObject.prototype.pick = function(...args) {
30
80
  const pickResult = zodObjectPick.apply(this, args);
31
- delete pickResult._def.extendMetadata;
32
- delete pickResult._def.openapi;
81
+ const zodOpenApi = pickResult._def.zodOpenApi;
82
+ if (zodOpenApi) {
83
+ const cloned = { ...zodOpenApi };
84
+ cloned.openapi = mergeOpenApi({}, cloned.openapi);
85
+ delete cloned.previous;
86
+ pickResult._def.zodOpenApi = cloned;
87
+ }
33
88
  return pickResult;
34
89
  };
35
90
  }
@@ -25,6 +25,7 @@ interface ZodOpenApiMetadata<T extends ZodTypeAny, TInferred = z.input<T> | z.ou
25
25
  refType?: CreationType;
26
26
  /**
27
27
  * Used to set the created type of an effect.
28
+ * If this was previously set to `same` and this is throwing an error, your effect is no longer returning the same type.
28
29
  */
29
30
  effectType?: CreationType | (z.input<T> extends z.output<T> ? z.output<T> extends z.input<T> ? 'same' : never : never);
30
31
  /**
@@ -54,6 +55,21 @@ interface ZodOpenApiMetadata<T extends ZodTypeAny, TInferred = z.input<T> | z.ou
54
55
  */
55
56
  type?: SchemaObject['type'];
56
57
  }
58
+ interface ZodOpenApiMetadataDef {
59
+ /**
60
+ * Up to date OpenAPI metadata
61
+ */
62
+ openapi?: ZodOpenApiMetadata<ZodTypeAny>;
63
+ /**
64
+ * Used to keep track of the Zod Schema had `.openapi` called on it
65
+ */
66
+ current?: ZodTypeAny;
67
+ /**
68
+ * Used to keep track of the previous Zod Schema that had `.openapi` called on it if another `.openapi` is called.
69
+ * This can also be present when .extend is called on an object.
70
+ */
71
+ previous?: ZodTypeAny;
72
+ }
57
73
  interface ZodOpenApiExtendMetadata {
58
74
  extends: ZodObject<any, any, any, any, any>;
59
75
  }
@@ -65,10 +81,7 @@ declare module 'zod' {
65
81
  openapi<T extends ZodTypeAny>(this: T, metadata: ZodOpenApiMetadata<T>): T;
66
82
  }
67
83
  interface ZodTypeDef {
68
- /**
69
- * OpenAPI metadata
70
- */
71
- openapi?: ZodOpenApiMetadata<ZodTypeAny>;
84
+ zodOpenApi?: ZodOpenApiMetadataDef;
72
85
  }
73
86
  interface ZodObjectDef {
74
87
  extendMetadata?: ZodOpenApiExtendMetadata;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod-openapi",
3
- "version": "3.3.0",
3
+ "version": "4.0.0",
4
4
  "description": "Convert Zod Schemas to OpenAPI v3.x documentation",
5
5
  "keywords": [
6
6
  "typescript",