UNPKG

@asteasolutions/zod-to-openapi

Version:
97 lines (96 loc) 5 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.extendZodWithOpenApi = void 0; const zod_is_type_1 = require("./lib/zod-is-type"); function extendZodWithOpenApi(zod) { if (typeof zod.ZodType.prototype.openapi !== 'undefined') { // This zod instance is already extended with the required methods, // doing it again will just result in multiple wrapper methods for // `optional` and `nullable` return; } zod.ZodSchema.prototype.openapi = function (openapi) { var _a, _b, _c, _d, _e, _f; const _g = openapi !== null && openapi !== void 0 ? openapi : {}, { param } = _g, restOfOpenApi = __rest(_g, ["param"]); const result = new this.constructor(Object.assign(Object.assign({}, this._def), { openapi: { _internal: (_a = this._def.openapi) === null || _a === void 0 ? void 0 : _a._internal, metadata: Object.assign(Object.assign(Object.assign({}, (_b = this._def.openapi) === null || _b === void 0 ? void 0 : _b.metadata), restOfOpenApi), { param: ((_d = (_c = this._def.openapi) === null || _c === void 0 ? void 0 : _c.metadata) === null || _d === void 0 ? void 0 : _d.param) || param ? Object.assign(Object.assign({}, (_f = (_e = this._def.openapi) === null || _e === void 0 ? void 0 : _e.metadata) === null || _f === void 0 ? void 0 : _f.param), param) : undefined }), } })); /** * We want to preserve the behavior created by `internal_openapi` * for extended objects. Applying metadata does not change * the parent's `refId` to be used for `extendedFrom` */ if ((0, zod_is_type_1.isZodType)(this, 'ZodObject')) { result.extend = this.extend; } return result; }; zod.ZodSchema.prototype.internal_openapi = function (openapi) { var _a, _b; const result = new this.constructor(Object.assign(Object.assign({}, this._def), { openapi: { _internal: Object.assign(Object.assign({}, (_a = this._def.openapi) === null || _a === void 0 ? void 0 : _a._internal), openapi), metadata: (_b = this._def.openapi) === null || _b === void 0 ? void 0 : _b.metadata, } })); if ((0, zod_is_type_1.isZodType)(this, 'ZodObject')) { const originalExtend = this.extend; result.extend = function (...args) { var _a, _b, _c, _d, _e; const extendedResult = originalExtend.apply(this, args); extendedResult._def.openapi = { _internal: { extendedFrom: ((_b = (_a = this._def.openapi) === null || _a === void 0 ? void 0 : _a._internal) === null || _b === void 0 ? void 0 : _b.refId) ? { refId: (_d = (_c = this._def.openapi) === null || _c === void 0 ? void 0 : _c._internal) === null || _d === void 0 ? void 0 : _d.refId, schema: this } : (_e = this._def.openapi) === null || _e === void 0 ? void 0 : _e._internal.extendedFrom, }, metadata: {}, }; return extendedResult; }; } return result; }; const zodOptional = zod.ZodType.prototype.optional; zod.ZodType.prototype.optional = function (...args) { const result = zodOptional.apply(this, args); result._def.openapi = this._def.openapi; return result; }; const zodNullable = zod.ZodType.prototype.nullable; zod.ZodType.prototype.nullable = function (...args) { const result = zodNullable.apply(this, args); result._def.openapi = this._def.openapi; return result; }; const zodDefault = zod.ZodType.prototype.default; zod.ZodType.prototype.default = function (...args) { const result = zodDefault.apply(this, args); result._def.openapi = this._def.openapi; return result; }; const zodPick = zod.ZodObject.prototype.pick; zod.ZodObject.prototype.pick = function (...args) { const result = zodPick.apply(this, args); result._def.openapi = undefined; return result; }; const zodOmit = zod.ZodObject.prototype.omit; zod.ZodObject.prototype.omit = function (...args) { const result = zodOmit.apply(this, args); result._def.openapi = undefined; return result; }; } exports.extendZodWithOpenApi = extendZodWithOpenApi;