UNPKG

openapi-zod-typed-express

Version:

Simple express runtime parser and documentation swagger generator with 100% support of Typescript static types

123 lines (122 loc) 4.88 kB
"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getZodValidator = exports.materialize = exports.zDual = exports.normalizeZodError = void 0; var zod_1 = require("zod"); var normalizeZodError = function (obj) { var _a; if (obj == null) return undefined; if (obj instanceof zod_1.z.ZodError) { return obj.issues.map(function (iss) { return ({ path: iss.path.join('.'), errors: [iss.message], }); }); } var message = (_a = obj === null || obj === void 0 ? void 0 : obj.message) !== null && _a !== void 0 ? _a : 'Unknown error'; return [{ path: '', errors: [message] }]; }; exports.normalizeZodError = normalizeZodError; var zDual = function (a) { var _a, _b; var self = { __dual: true, parse: a.parse, serialize: a.serialize, nullable: function () { return (0, exports.zDual)({ parse: a.parse, serialize: a.serialize, isOptional: a.isOptional, isNullable: true, }); }, optional: function () { return (0, exports.zDual)({ parse: a.parse, serialize: a.serialize, isOptional: true, isNullable: a.isNullable, }); }, _nullable: ((_a = a.isNullable) !== null && _a !== void 0 ? _a : false), _optional: ((_b = a.isOptional) !== null && _b !== void 0 ? _b : false), }; return self; }; exports.zDual = zDual; var isDual = function (s) { return !!s && typeof s === 'object' && s.__dual === true; }; var materialize = function (s, mode) { var _a, _b; if (isDual(s)) { var zOut = (0, exports.materialize)(mode === 'parse' ? s.parse : s.serialize, mode); if (s._optional) { zOut = zOut.optional(); } if (s._nullable) { zOut = zOut.nullable(); } return zOut; } if (s instanceof zod_1.z.ZodObject) { var shape = Object.fromEntries(Object.entries(s.shape).map(function (_a) { var _b = __read(_a, 2), k = _b[0], v = _b[1]; return [k, (0, exports.materialize)(v, mode)]; })); return zod_1.z.object(shape); } if (s instanceof zod_1.z.ZodArray) return zod_1.z.array((0, exports.materialize)(s.element, mode)); if (s instanceof zod_1.z.ZodRecord) { var keySchema = (0, exports.materialize)((_a = s._def.keyType) !== null && _a !== void 0 ? _a : zod_1.z.string(), mode); var valSchema = (0, exports.materialize)(s._def.valueType, mode); return zod_1.z.record(keySchema, valSchema); } if (s instanceof zod_1.z.ZodUnion) return zod_1.z.union(s._def.options.map(function (o) { return (0, exports.materialize)(o, mode); })); if (((_b = s === null || s === void 0 ? void 0 : s._def) === null || _b === void 0 ? void 0 : _b.typeName) === 'ZodDiscriminatedUnion') { var du = s; return zod_1.z.discriminatedUnion(du._def.discriminator, du._def.options.map(function (o) { return (0, exports.materialize)(o, mode); })); } if (s instanceof zod_1.z.ZodOptional) return (0, exports.materialize)(s._def.innerType, mode).optional(); if (s instanceof zod_1.z.ZodNullable) return (0, exports.materialize)(s._def.innerType, mode).nullable(); if (s instanceof zod_1.z.ZodLazy) return zod_1.z.lazy(function () { return (0, exports.materialize)(s._def.getter(), mode); }); return s; }; exports.materialize = materialize; var getZodValidator = function (schema, extra) { var _a; if (!schema) { var validate_1 = (function (value) { return ({ success: true, data: value }); }); var isValid_1 = (function () { return true; }); return { validate: validate_1, isValid: isValid_1 }; } var mode = ((_a = extra === null || extra === void 0 ? void 0 : extra.transformTypeMode) !== null && _a !== void 0 ? _a : 'parse'); var materializedSchema = (0, exports.materialize)(schema, mode); var validate = (function (value) { return materializedSchema.safeParse(value); }); var isValid = function (value) { return validate(value).success; }; return { validate: validate, isValid: isValid }; }; exports.getZodValidator = getZodValidator;