openapi-zod-typed-express
Version:
Simple express runtime parser and documentation swagger generator with 100% support of Typescript static types
28 lines (27 loc) • 1.19 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getZodValidator = 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 getZodValidator = function (_schema, extra) {
var schema = _schema !== null && _schema !== void 0 ? _schema : zod_1.z.any();
var validate = function (value) {
return (extra === null || extra === void 0 ? void 0 : extra.transformTypeMode) === 'parse' ? schema.safeDecode(value) : schema.safeEncode(value);
};
var isValid = function (value) { return validate(value).success; };
return { validate: validate, isValid: isValid };
};
exports.getZodValidator = getZodValidator;
;