"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { ZodSchemaAdapter: () => ZodSchemaAdapter, z: () => z9 }); module.exports = __toCommonJS(src_exports); var import_schemas9 = require("@palmares/schemas"); var z9 = __toESM(require("zod"), 1); // src/fields/index.ts var import_schemas = require("@palmares/schemas"); var z = __toESM(require("zod"), 1); var defaultFieldAdapter = (0, import_schemas.fieldAdapter)({ translate: /* @__PURE__ */ __name((_, args, baseResult) => { if (args.optional.allow) baseResult = baseResult.optional(); if (args.nullable.allow) baseResult = baseResult.nullable(); return baseResult; }, "translate"), // eslint-disable-next-line ts/require-await parse: /* @__PURE__ */ __name(async (_adapter, _fieldAdapter, result, value, _args) => { try { const parsed = result.safeParse(value); let errors = void 0; if (parsed.success !== true) { const issues = []; for (const issue of parsed.error.issues) { if (issue.code === "invalid_union") { for (const errorOfSchemas of issue.unionErrors) { issues.push(...errorOfSchemas.issues); } } else issues.push(issue); } errors = issues; } return { errors, parsed: parsed.success ? parsed.data : void 0 }; } catch (error) { if (error instanceof z.ZodError) { return { errors: error.errors, parsed: void 0 }; } else throw error; } }, "parse"), // eslint-disable-next-line ts/require-await formatError: /* @__PURE__ */ __name(async (_adapter, _fieldAdapter, _schema, error, _metadata) => { switch (error.code) { case "invalid_type": if (error.received === "null") { return { code: "null", path: error.path, message: error.message }; } if (error.received === "undefined") return { code: "required", path: error.path, message: error.message }; return { code: error.expected, path: error.path, message: error.message }; } return error; }, "formatError"), // eslint-disable-next-line ts/require-await toString: /* @__PURE__ */ __name(async (_adapter, _fieldAdapter, args, baseResult) => { if (args.optional.allow) baseResult += `.optional()`; if (args.nullable.allow) baseResult += `.nullable()`; return baseResult; }, "toString") }); // src/fields/array.ts var import_schemas2 = require("@palmares/schemas"); var z2 = __toESM(require("zod"), 1); // src/utils.ts async function transformErrorsOnComplexTypes(adapter, fieldAdapter2, schema, error, metadata) { let errors = error; if (metadata.$alreadyValidatedOnComplex !== true) { const stringFormattedErrors = await adapter.string?.formatError(adapter, fieldAdapter2, schema, error, metadata); const numberFormattedErrors = await adapter.number?.formatError(adapter, fieldAdapter2, schema, stringFormattedErrors, metadata); const datetimeFormattedErrors = await adapter.datetime?.formatError(adapter, fieldAdapter2, schema, numberFormattedErrors, metadata); errors = await adapter.boolean?.formatError(adapter, fieldAdapter2, schema, datetimeFormattedErrors, metadata); metadata.$alreadyValidatedOnComplex = true; if (metadata.$type.has("union") === false && adapter.union) errors = await adapter.union.formatError(adapter, fieldAdapter2, schema, errors, metadata); if (metadata.$type.has("object") === false && adapter.object) errors = await adapter.object.formatError(adapter, fieldAdapter2, schema, errors, metadata); if (metadata.$type.has("array") === false && adapter.array) errors = await adapter.array.formatError(adapter, fieldAdapter2, schema, errors, metadata); return fieldAdapter2.formatError(adapter, fieldAdapter2, schema, errors, metadata); } return error; } __name(transformErrorsOnComplexTypes, "transformErrorsOnComplexTypes"); // src/fields/array.ts var arrayAdapter = (0, import_schemas2.arrayFieldAdapter)({ translate: /* @__PURE__ */ __name((fieldAdapter2, args) => { let result = z2[args.isTuple ? "tuple" : "array"]( // eslint-disable-next-line ts/no-unnecessary-type-assertion args.isTuple ? args.schemas : args.schemas[0], { errorMap: /* @__PURE__ */ __name((issue) => { const isOptional = issue.code === "invalid_type" && issue.expected === "array" && issue.received === "undefined"; const isNullable = issue.code === "invalid_type" && issue.received === "null"; if (isOptional) return { message: args.optional.message }; else if (isNullable) return { message: args.nullable.message }; else if (issue.code === "invalid_type") return { message: args.type.message }; return { message: issue.message || "" }; }, "errorMap") } ); if (args.maxLength && args.maxLength.inclusive) result = result.max(args.maxLength.value, args.maxLength.message); if (args.maxLength && args.maxLength.inclusive === false) result = result.max(args.maxLength.value - 1, args.maxLength.message); if (args.minLength && args.minLength.inclusive) result = result.min(args.minLength.value, args.minLength.message); if (args.minLength && args.minLength.inclusive === false) result = result.min(args.minLength.value + 1, args.minLength.message); result = fieldAdapter2.translate(fieldAdapter2, args, result); return result; }, "translate"), // eslint-disable-next-line ts/require-await toString: /* @__PURE__ */ __name(async (_adapter, _fieldAdapter, _args) => { return ""; }, "toString"), formatError: /* @__PURE__ */ __name((adapter, fieldAdapter2, schema, error, metadata) => { if (metadata === void 0) metadata = {}; if (metadata.$type instanceof Set === false) metadata.$type = /* @__PURE__ */ new Set(); if (metadata.$type instanceof Set) metadata.$type.add("array"); switch (error.code) { case "too_big": if (error.type === "array") return { code: "maxLength", path: error.path, message: error.message }; break; case "too_small": if (error.type === "array") return { code: "minLength", path: error.path, message: error.message }; break; } return transformErrorsOnComplexTypes(adapter, fieldAdapter2, schema, error, metadata); }, "formatError"), // eslint-disable-next-line ts/require-await parse: /* @__PURE__ */ __name(async (_adapter, fieldAdapter2, result, value, _args) => { return fieldAdapter2.parse(_adapter, fieldAdapter2, result, value, _args); }, "parse") }); // src/fields/boolean.ts var import_schemas3 = require("@palmares/schemas"); var z3 = __toESM(require("zod"), 1); var booleanAdapter = (0, import_schemas3.booleanFieldAdapter)({ translate: /* @__PURE__ */ __name((fieldAdapter2, args) => { let result = z3.boolean({ errorMap: /* @__PURE__ */ __name((issue) => { const isOptional = issue.code === "invalid_type" && issue.expected === "boolean" && issue.received === "undefined"; const isNullable = issue.code === "invalid_type" && issue.received === "null"; if (isOptional) return { message: args.optional.message }; else if (isNullable) return { message: args.nullable.message }; else if (issue.code === "invalid_type") return { message: args.type.message }; return { message: issue.message || "" }; }, "errorMap") }); if (args.is) result = result.superRefine((value, ctx) => { const isValid = value === args.is.value; if (!isValid) ctx.addIssue({ code: "is", expected: args.is.value, message: args.is.message }); }); if (args.parsers.trueValues) { result = z3.preprocess((value) => { const isValueATrueValue = args.parsers.trueValues?.includes(value); if (isValueATrueValue) return true; return value; }, result); } if (args.parsers.falseValues) { result = z3.preprocess((value) => { const isValueAFalseValue = args.parsers.falseValues?.includes(value); if (isValueAFalseValue) return false; return value; }, result); } if (args.parsers.allowString) { result = z3.preprocess((value) => { const isValueAString = typeof value === "string"; if (isValueAString) return Boolean(value); return value; }, result); } if (args.parsers.allowNumber) { result = z3.preprocess((value) => { const isValueANumber = typeof value === "number"; if (isValueANumber) return Boolean(value); return value; }, result); } result = fieldAdapter2.translate(fieldAdapter2, args, result); return result; }, "translate"), // eslint-disable-next-line ts/require-await toString: /* @__PURE__ */ __name(async (_adapter) => { return ""; }, "toString"), // eslint-disable-next-line ts/require-await formatError: /* @__PURE__ */ __name(async (_adapter, fieldAdapter2, _schema, error, _metadata) => { switch (error.code) { case "is": return { code: "is", message: error.message, path: error.path }; } return fieldAdapter2.formatError(_adapter, fieldAdapter2, _schema, error, _metadata); }, "formatError"), // eslint-disable-next-line ts/require-await parse: /* @__PURE__ */ __name(async (_adapter, fieldAdapter2, result, value, _args) => { return fieldAdapter2.parse(_adapter, fieldAdapter2, result, value, _args); }, "parse") }); // src/fields/datetime.ts var import_schemas4 = require("@palmares/schemas"); var z4 = __toESM(require("zod"), 1); var datetimeAdapter = (0, import_schemas4.datetimeFieldAdapter)({ translate: /* @__PURE__ */ __name((fieldAdapter2, args) => { let result = z4.date({ errorMap: /* @__PURE__ */ __name((issue) => { const isOptional = issue.code === "invalid_type" && issue.expected === "date" && issue.received === "undefined"; const isNullable = issue.code === "invalid_type" && issue.received === "null"; if (isOptional) return { message: args.optional.message }; else if (isNullable) return { message: args.nullable.message }; else if (issue.code === "invalid_type") return { message: args.type.message }; return { message: issue.message || "" }; }, "errorMap") }); if (args.below) { result = result.superRefine((value, ctx) => { const isValid = args.below ? args.below.inclusive ? value <= args.below.value : value < args.below.value : true; if (!isValid) ctx.addIssue({ code: "below", expected: args.below?.value, message: args.below?.message }); }); } if (args.above) { result = result.superRefine((value, ctx) => { const isValid = args.above ? args.above.inclusive ? value >= args.above.value : value > args.above.value : true; if (!isValid) ctx.addIssue({ code: "above", expected: args.above?.value, message: args.above?.message }); }); } if (args.allowString) { result = z4.preprocess((value) => { if (typeof value === "string") { return new Date(value); } return value; }, result); } result = fieldAdapter2.translate(fieldAdapter2, args, result); return result; }, "translate"), // eslint-disable-next-line ts/require-await toString: /* @__PURE__ */ __name(async (_adapter) => { return ""; }, "toString"), // eslint-disable-next-line ts/require-await formatError: /* @__PURE__ */ __name(async (_adapter, fieldAdapter2, _schema, error, _metadata) => { switch (error.code) { case "below": return { code: "below", path: error.path, message: error.message }; case "above": return { code: "above", path: error.path, message: error.message }; } return fieldAdapter2.formatError(_adapter, fieldAdapter2, _schema, error, _metadata); }, "formatError"), // eslint-disable-next-line ts/require-await parse: /* @__PURE__ */ __name(async (_adapter, fieldAdapter2, result, value, _args) => { return fieldAdapter2.parse(_adapter, fieldAdapter2, result, value, _args); }, "parse") }); // src/fields/number.ts var import_schemas5 = require("@palmares/schemas"); var z5 = __toESM(require("zod"), 1); var numberAdapter = (0, import_schemas5.numberFieldAdapter)({ translate: /* @__PURE__ */ __name((fieldAdapter2, args) => { let result = z5.number({ errorMap: /* @__PURE__ */ __name((issue) => { const isOptional = issue.code === "invalid_type" && issue.expected === "number" && issue.received === "undefined"; const isNullable = issue.code === "invalid_type" && issue.received === "null"; if (isOptional) return { message: args.optional.message }; else if (isNullable) return { message: args.nullable.message }; else if (issue.code === "invalid_type") return { message: args.type.message }; return { message: issue.message || "" }; }, "errorMap") }); if (args.integer) result = result.int(args.integer.message); if (args.max) { if (args.max.inclusive) result = result.lte(args.max.value, args.max.message); else result = result.lt(args.max.value, args.max.message); } if (args.min) { if (args.min.inclusive) result = result.gte(args.min.value, args.min.message); else result = result.gt(args.min.value, args.min.message); } if (args.maxDigits) result = result.superRefine((value, ctx) => { const isValid = value.toString().replaceAll(".", "").length <= args.maxDigits?.value; if (!isValid) ctx.addIssue({ code: "max_digits", expected: args.maxDigits?.value, message: args.maxDigits?.message }); }); if (args.decimalPlaces) result = result.superRefine((value, ctx) => { const isValid = (value.toString().split(".")[1] || "").length <= args.decimalPlaces?.value; if (!isValid) ctx.addIssue({ code: "decimal_places", expected: args.decimalPlaces?.value, message: args.decimalPlaces?.message }); }); if (args.is) { const isAsSet = new Set(args.is.value); result = result.superRefine((value, ctx) => { const isValid = isAsSet.has(value); if (!isValid) ctx.addIssue({ code: "is", expected: args.is?.value, message: args.is?.message }); }); } if (args.parsers.allowString) { result = z5.preprocess((value) => { if (typeof value === "string") return Number(value); return value; }, result); } result = fieldAdapter2.translate(fieldAdapter2, args, result); return result; }, "translate"), // eslint-disable-next-line ts/require-await parse: /* @__PURE__ */ __name(async (_adapter, fieldAdapter2, result, value, _args) => { return fieldAdapter2.parse(_adapter, fieldAdapter2, result, value, _args); }, "parse"), toString: /* @__PURE__ */ __name(async (adapter, fieldAdapter2, args) => { let result = `z.number()`; if (args.max) { if (args.max.inclusive) result += `.lte(${args.max.value}, ${args.max.message})`; else result += `.lt(${args.max.value}, ${args.max.message})`; } if (args.min) { if (args.min.inclusive) result += `.gte(${args.min.value}, ${args.min.message})`; else result += `.gt(${args.min.value, args.min.message})`; } result = await fieldAdapter2.toString(adapter, fieldAdapter2, args, result); return result; }, "toString"), // eslint-disable-next-line ts/require-await formatError: /* @__PURE__ */ __name(async (_adapter, fieldAdapter2, _schema, error, _metadata) => { switch (error.code) { case "invalid_type": { if (error?.expected === "integer") return { code: "integer", path: error.path, message: error.message }; return fieldAdapter2.formatError(_adapter, fieldAdapter2, _schema, error, _metadata); } case "max_digits": return { code: "maxDigits", path: error.path, message: error.message }; case "decimal_places": return { code: "decimalPlaces", path: error.path, message: error.message }; case "is": return { code: "is", path: error.path, message: error.message }; case "too_big": if (error.type === "number") return { code: "max", path: error.path, message: error.message }; break; case "too_small": if (error.type === "number") return { code: "min", path: error.path, message: error.message }; break; } return fieldAdapter2.formatError(_adapter, fieldAdapter2, _schema, error, _metadata); }, "formatError") }); // src/fields/object.ts var import_schemas6 = require("@palmares/schemas"); var z6 = __toESM(require("zod"), 1); var objectAdapter = (0, import_schemas6.objectFieldAdapter)({ translate: /* @__PURE__ */ __name((fieldAdapter2, args) => { let result = z6.object(args.data, { errorMap: /* @__PURE__ */ __name((issue) => { const isOptional = issue.code === "invalid_type" && issue.expected === "object" && issue.received === "undefined"; const isNullable = issue.code === "invalid_type" && issue.received === "null"; if (isOptional) return { message: args.optional.message }; else if (isNullable) return { message: args.nullable.message }; else if (issue.code === "invalid_type") return { message: args.type.message }; return { message: issue.message || "" }; }, "errorMap") }); result = fieldAdapter2.translate(fieldAdapter2, args, result); return result; }, "translate"), // eslint-disable-next-line ts/require-await parse: /* @__PURE__ */ __name(async (_adapter, fieldAdapter2, result, value, _args) => { return fieldAdapter2.parse(_adapter, fieldAdapter2, result, value, _args); }, "parse"), formatError: /* @__PURE__ */ __name(async (adapter, fieldAdapter2, schema, error, metadata) => { if (metadata === void 0) metadata = {}; if (metadata.$type instanceof Set === false) metadata.$type = /* @__PURE__ */ new Set(); if (metadata.$type instanceof Set) metadata.$type.add("object"); return transformErrorsOnComplexTypes(adapter, fieldAdapter2, schema, error, metadata); }, "formatError"), // eslint-disable-next-line ts/require-await toString: /* @__PURE__ */ __name(async (_adapter, _fieldAdapter, args) => { let objectData = `{ `; for (const [key, value] of Object.entries(args.data)) { objectData = objectData + ` ${key}: ${value}, `; } return `z.object(${objectData.slice(0, -2)} )`; }, "toString") }); // src/fields/string.ts var import_schemas7 = require("@palmares/schemas"); var z7 = __toESM(require("zod"), 1); var stringAdapter = (0, import_schemas7.stringFieldAdapter)({ translate: /* @__PURE__ */ __name((fieldAdapter2, args) => { let result = z7.string({ errorMap: /* @__PURE__ */ __name((issue) => { const isOptional = issue.code === "invalid_type" && issue.expected === "string" && issue.received === "undefined"; const isNullable = issue.code === "invalid_type" && issue.received === "null"; if (isOptional) return { message: args.optional.message }; else if (isNullable) return { message: args.nullable.message }; else if (issue.code === "invalid_type") return { message: args.type.message }; return { message: issue.message || "" }; }, "errorMap") }); if (args.endsWith) result = result.endsWith(args.endsWith.value, args.endsWith.message); if (args.startsWith) result = result.startsWith(args.startsWith.value, args.startsWith.message); if (args.includes) result = result.includes(args.includes.value, { message: args.includes.message }); if (args.maxLength) result = result.max(args.maxLength.value > 0 ? args.maxLength.value : 0, args.maxLength.message); if (args.minLength) result = result.min(args.minLength.value > 0 ? args.minLength.value : 0, args.minLength.message); if (args.regex) result = result.regex(args.regex.value, args.regex.message); if (args.uuid) result = result.uuid(args.uuid.message); if (args.email) result = result.email(args.email.message); if (args.is) { if (Array.isArray(args.is.value)) result = z7.enum(args.is.value, { errorMap: /* @__PURE__ */ __name((issue) => { if (issue.code === "invalid_enum_value") return { message: args.is?.message || "" }; return { message: issue.message || "" }; }, "errorMap") }); else result = z7.literal(args.is.value, { errorMap: /* @__PURE__ */ __name((issue) => { if (issue.code === "invalid_literal") return { message: args.is?.message || "" }; return { message: issue.message || "" }; }, "errorMap") }); } result = fieldAdapter2.translate(fieldAdapter2, args, result); return result; }, "translate"), // eslint-disable-next-line ts/require-await parse: /* @__PURE__ */ __name(async (_adapter, _fieldAdapter, result, value, _args) => { try { const parsed = result.safeParse(value); return { errors: parsed.success ? void 0 : parsed.error.issues, parsed: parsed.success ? parsed.data : void 0 }; } catch (error) { if (error instanceof z7.ZodError) return { errors: error.errors, parsed: void 0 }; else throw error; } }, "parse"), // eslint-disable-next-line ts/require-await formatError: /* @__PURE__ */ __name(async (_adapter, fieldAdapter2, _schema, error, _metadata) => { switch (error.code) { case "invalid_enum_value": return { code: "is", path: error.path, message: error.message }; case "invalid_string": { const validationAsObject = ( // eslint-disable-next-line ts/no-unnecessary-condition typeof error.validation === "object" && error.validation !== null ? error.validation : {} ); const validationAsString = typeof error.validation === "string" ? error.validation : ""; if ("includes" in validationAsObject && "position" in validationAsObject) { return { code: "includes", path: error.path, message: error.message }; } else if ("startsWith" in validationAsObject) { return { code: "startsWith", path: error.path, message: error.message }; } else if ("endsWith" in validationAsObject) { return { code: "endsWith", path: error.path, message: error.message }; } else if (validationAsString === "regex") { return { code: "regex", message: error.message, path: error.path }; } else if (validationAsString === "uuid") { return { code: "uuid", message: error.message, path: error.path }; } else if (validationAsString === "email") { return { code: "email", message: error.message, path: error.path }; } return error; } case "too_big": if (error.type === "string") return { code: "maxLength", path: error.path, message: error.message }; break; case "too_small": if (error.type === "string") return { code: "minLength", path: error.path, message: error.message }; break; } return fieldAdapter2.formatError(_adapter, fieldAdapter2, _schema, error, _metadata); }, "formatError"), // eslint-disable-next-line ts/require-await toString: /* @__PURE__ */ __name(async () => { return ""; }, "toString") }); // src/fields/union.ts var import_schemas8 = require("@palmares/schemas"); var z8 = __toESM(require("zod"), 1); var unionAdapter = (0, import_schemas8.unionFieldAdapter)({ translate: /* @__PURE__ */ __name((fieldAdapter2, args) => { let result = z8.union(args.schemas, { errorMap: /* @__PURE__ */ __name((issue) => { if (issue.code === "invalid_union") { for (const unionError of issue.unionErrors) { for (const issue2 of unionError.issues) { if (issue2.code === "invalid_type" && issue2.received === "undefined") issue2.message = args.optional.message; if (issue2.code === "invalid_type" && issue2.received === "null") issue2.message = args.nullable.message; else if (issue2.code === "invalid_type") return { message: args.type.message }; } } } return { message: issue.message || "" }; }, "errorMap") }); result = fieldAdapter2.translate(fieldAdapter2, args, result); return result; }, "translate"), // eslint-disable-next-line ts/require-await toString: /* @__PURE__ */ __name(async (_adapter, _fieldAdapter, _args) => { return ""; }, "toString"), formatError: /* @__PURE__ */ __name((adapter, fieldAdapter2, schema, error, metadata) => { if (metadata === void 0) metadata = {}; if (metadata.$type instanceof Set === false) metadata.$type = /* @__PURE__ */ new Set(); if (metadata.$type instanceof Set) metadata.$type.add("union"); return transformErrorsOnComplexTypes(adapter, fieldAdapter2, schema, error, metadata); }, "formatError"), // eslint-disable-next-line ts/require-await parse: /* @__PURE__ */ __name(async (_adapter, fieldAdapter2, result, value, _args) => { return fieldAdapter2.parse(_adapter, fieldAdapter2, result, value, _args); }, "parse") }); // src/index.ts var ZodSchemaAdapter = class extends import_schemas9.SchemaAdapter { static { __name(this, "ZodSchemaAdapter"); } field = new defaultFieldAdapter(); number = new numberAdapter(); object = new objectAdapter(); union = new unionAdapter(); boolean = new booleanAdapter(); string = new stringAdapter(); datetime = new datetimeAdapter(); array = new arrayAdapter(); // eslint-disable-next-line ts/require-await async formatError(error) { return { message: error.message, path: error.path.map((path) => `${path}`), code: error.code }; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ZodSchemaAdapter, z });