avancement planning

This commit is contained in:
2026-05-26 11:58:39 +02:00
parent 619a2b240a
commit 150b97cd2e
4892 changed files with 99214 additions and 429382 deletions
+437 -200
View File
@@ -23,8 +23,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.$ZodSet = exports.$ZodMap = exports.$ZodRecord = exports.$ZodTuple = exports.$ZodIntersection = exports.$ZodDiscriminatedUnion = exports.$ZodUnion = exports.$ZodObject = exports.$ZodArray = exports.$ZodDate = exports.$ZodVoid = exports.$ZodNever = exports.$ZodUnknown = exports.$ZodAny = exports.$ZodNull = exports.$ZodUndefined = exports.$ZodSymbol = exports.$ZodBigIntFormat = exports.$ZodBigInt = exports.$ZodBoolean = exports.$ZodNumberFormat = exports.$ZodNumber = exports.$ZodCustomStringFormat = exports.$ZodJWT = exports.$ZodE164 = exports.$ZodBase64URL = exports.$ZodBase64 = exports.$ZodCIDRv6 = exports.$ZodCIDRv4 = exports.$ZodIPv6 = exports.$ZodIPv4 = exports.$ZodISODuration = exports.$ZodISOTime = exports.$ZodISODate = exports.$ZodISODateTime = exports.$ZodKSUID = exports.$ZodXID = exports.$ZodULID = exports.$ZodCUID2 = exports.$ZodCUID = exports.$ZodNanoID = exports.$ZodEmoji = exports.$ZodURL = exports.$ZodEmail = exports.$ZodUUID = exports.$ZodGUID = exports.$ZodStringFormat = exports.$ZodString = exports.clone = exports.$ZodType = void 0;
exports.$ZodCustom = exports.$ZodLazy = exports.$ZodPromise = exports.$ZodTemplateLiteral = exports.$ZodReadonly = exports.$ZodPipe = exports.$ZodNaN = exports.$ZodCatch = exports.$ZodSuccess = exports.$ZodNonOptional = exports.$ZodPrefault = exports.$ZodDefault = exports.$ZodNullable = exports.$ZodOptional = exports.$ZodTransform = exports.$ZodFile = exports.$ZodLiteral = exports.$ZodEnum = void 0;
exports.$ZodRecord = exports.$ZodTuple = exports.$ZodIntersection = exports.$ZodDiscriminatedUnion = exports.$ZodUnion = exports.$ZodObjectJIT = exports.$ZodObject = exports.$ZodArray = exports.$ZodDate = exports.$ZodVoid = exports.$ZodNever = exports.$ZodUnknown = exports.$ZodAny = exports.$ZodNull = exports.$ZodUndefined = exports.$ZodSymbol = exports.$ZodBigIntFormat = exports.$ZodBigInt = exports.$ZodBoolean = exports.$ZodNumberFormat = exports.$ZodNumber = exports.$ZodCustomStringFormat = exports.$ZodJWT = exports.$ZodE164 = exports.$ZodBase64URL = exports.$ZodBase64 = exports.$ZodCIDRv6 = exports.$ZodCIDRv4 = exports.$ZodMAC = exports.$ZodIPv6 = exports.$ZodIPv4 = exports.$ZodISODuration = exports.$ZodISOTime = exports.$ZodISODate = exports.$ZodISODateTime = exports.$ZodKSUID = exports.$ZodXID = exports.$ZodULID = exports.$ZodCUID2 = exports.$ZodCUID = exports.$ZodNanoID = exports.$ZodEmoji = exports.$ZodURL = exports.$ZodEmail = exports.$ZodUUID = exports.$ZodGUID = exports.$ZodStringFormat = exports.$ZodString = exports.clone = exports.$ZodType = void 0;
exports.$ZodCustom = exports.$ZodLazy = exports.$ZodPromise = exports.$ZodFunction = exports.$ZodTemplateLiteral = exports.$ZodReadonly = exports.$ZodCodec = exports.$ZodPipe = exports.$ZodNaN = exports.$ZodCatch = exports.$ZodSuccess = exports.$ZodNonOptional = exports.$ZodPrefault = exports.$ZodDefault = exports.$ZodNullable = exports.$ZodOptional = exports.$ZodTransform = exports.$ZodFile = exports.$ZodLiteral = exports.$ZodEnum = exports.$ZodSet = exports.$ZodMap = void 0;
exports.isValidBase64 = isValidBase64;
exports.isValidBase64URL = isValidBase64URL;
exports.isValidJWT = isValidJWT;
@@ -46,7 +46,6 @@ exports.$ZodType = core.$constructor("$ZodType", (inst, def) => {
if (inst._zod.traits.has("$ZodCheck")) {
checks.unshift(inst);
}
//
for (const ch of checks) {
for (const fn of ch._zod.onattach) {
fn(inst);
@@ -103,7 +102,47 @@ exports.$ZodType = core.$constructor("$ZodType", (inst, def) => {
}
return payload;
};
// const handleChecksResult = (
// checkResult: ParsePayload,
// originalResult: ParsePayload,
// ctx: ParseContextInternal
// ): util.MaybeAsync<ParsePayload> => {
// // if the checks mutated the value && there are no issues, re-parse the result
// if (checkResult.value !== originalResult.value && !checkResult.issues.length)
// return inst._zod.parse(checkResult, ctx);
// return originalResult;
// };
const handleCanaryResult = (canary, payload, ctx) => {
// abort if the canary is aborted
if (util.aborted(canary)) {
canary.aborted = true;
return canary;
}
// run checks first, then
const checkResult = runChecks(payload, checks, ctx);
if (checkResult instanceof Promise) {
if (ctx.async === false)
throw new core.$ZodAsyncError();
return checkResult.then((checkResult) => inst._zod.parse(checkResult, ctx));
}
return inst._zod.parse(checkResult, ctx);
};
inst._zod.run = (payload, ctx) => {
if (ctx.skipChecks) {
return inst._zod.parse(payload, ctx);
}
if (ctx.direction === "backward") {
// run canary
// initial pass (no checks)
const canary = inst._zod.parse({ value: payload.value, issues: [] }, { ...ctx, skipChecks: true });
if (canary instanceof Promise) {
return canary.then((canary) => {
return handleCanaryResult(canary, payload, ctx);
});
}
return handleCanaryResult(canary, payload, ctx);
}
// forward
const result = inst._zod.parse(payload, ctx);
if (result instanceof Promise) {
if (ctx.async === false)
@@ -187,9 +226,10 @@ exports.$ZodURL = core.$constructor("$ZodURL", (inst, def) => {
exports.$ZodStringFormat.init(inst, def);
inst._zod.check = (payload) => {
try {
const orig = payload.value;
const url = new URL(orig);
const href = url.href;
// Trim whitespace from input
const trimmed = payload.value.trim();
// @ts-ignore
const url = new URL(trimmed);
if (def.hostname) {
def.hostname.lastIndex = 0;
if (!def.hostname.test(url.hostname)) {
@@ -197,7 +237,7 @@ exports.$ZodURL = core.$constructor("$ZodURL", (inst, def) => {
code: "invalid_format",
format: "url",
note: "Invalid hostname",
pattern: regexes.hostname.source,
pattern: def.hostname.source,
input: payload.value,
inst,
continue: !def.abort,
@@ -218,12 +258,14 @@ exports.$ZodURL = core.$constructor("$ZodURL", (inst, def) => {
});
}
}
// payload.value = url.href;
if (!orig.endsWith("/") && href.endsWith("/")) {
payload.value = href.slice(0, -1);
// Set the output value based on normalize flag
if (def.normalize) {
// Use normalized URL
payload.value = url.href;
}
else {
payload.value = href;
// Preserve the original input (trimmed)
payload.value = trimmed;
}
return;
}
@@ -285,20 +327,15 @@ exports.$ZodISODuration = core.$constructor("$ZodISODuration", (inst, def) => {
exports.$ZodIPv4 = core.$constructor("$ZodIPv4", (inst, def) => {
def.pattern ?? (def.pattern = regexes.ipv4);
exports.$ZodStringFormat.init(inst, def);
inst._zod.onattach.push((inst) => {
const bag = inst._zod.bag;
bag.format = `ipv4`;
});
inst._zod.bag.format = `ipv4`;
});
exports.$ZodIPv6 = core.$constructor("$ZodIPv6", (inst, def) => {
def.pattern ?? (def.pattern = regexes.ipv6);
exports.$ZodStringFormat.init(inst, def);
inst._zod.onattach.push((inst) => {
const bag = inst._zod.bag;
bag.format = `ipv6`;
});
inst._zod.bag.format = `ipv6`;
inst._zod.check = (payload) => {
try {
// @ts-ignore
new URL(`http://[${payload.value}]`);
// return;
}
@@ -313,6 +350,11 @@ exports.$ZodIPv6 = core.$constructor("$ZodIPv6", (inst, def) => {
}
};
});
exports.$ZodMAC = core.$constructor("$ZodMAC", (inst, def) => {
def.pattern ?? (def.pattern = regexes.mac(def.delimiter));
exports.$ZodStringFormat.init(inst, def);
inst._zod.bag.format = `mac`;
});
exports.$ZodCIDRv4 = core.$constructor("$ZodCIDRv4", (inst, def) => {
def.pattern ?? (def.pattern = regexes.cidrv4);
exports.$ZodStringFormat.init(inst, def);
@@ -321,8 +363,11 @@ exports.$ZodCIDRv6 = core.$constructor("$ZodCIDRv6", (inst, def) => {
def.pattern ?? (def.pattern = regexes.cidrv6); // not used for validation
exports.$ZodStringFormat.init(inst, def);
inst._zod.check = (payload) => {
const [address, prefix] = payload.value.split("/");
const parts = payload.value.split("/");
try {
if (parts.length !== 2)
throw new Error();
const [address, prefix] = parts;
if (!prefix)
throw new Error();
const prefixNum = Number(prefix);
@@ -330,6 +375,7 @@ exports.$ZodCIDRv6 = core.$constructor("$ZodCIDRv6", (inst, def) => {
throw new Error();
if (prefixNum < 0 || prefixNum > 128)
throw new Error();
// @ts-ignore
new URL(`http://[${address}]`);
}
catch {
@@ -350,6 +396,7 @@ function isValidBase64(data) {
if (data.length % 4 !== 0)
return false;
try {
// @ts-ignore
atob(data);
return true;
}
@@ -360,9 +407,7 @@ function isValidBase64(data) {
exports.$ZodBase64 = core.$constructor("$ZodBase64", (inst, def) => {
def.pattern ?? (def.pattern = regexes.base64);
exports.$ZodStringFormat.init(inst, def);
inst._zod.onattach.push((inst) => {
inst._zod.bag.contentEncoding = "base64";
});
inst._zod.bag.contentEncoding = "base64";
inst._zod.check = (payload) => {
if (isValidBase64(payload.value))
return;
@@ -386,9 +431,7 @@ function isValidBase64URL(data) {
exports.$ZodBase64URL = core.$constructor("$ZodBase64URL", (inst, def) => {
def.pattern ?? (def.pattern = regexes.base64url);
exports.$ZodStringFormat.init(inst, def);
inst._zod.onattach.push((inst) => {
inst._zod.bag.contentEncoding = "base64url";
});
inst._zod.bag.contentEncoding = "base64url";
inst._zod.check = (payload) => {
if (isValidBase64URL(payload.value))
return;
@@ -414,6 +457,7 @@ function isValidJWT(token, algorithm = null) {
const [header] = tokensParts;
if (!header)
return false;
// @ts-ignore
const parsedHeader = JSON.parse(atob(header));
if ("typ" in parsedHeader && parsedHeader?.typ !== "JWT")
return false;
@@ -485,9 +529,9 @@ exports.$ZodNumber = core.$constructor("$ZodNumber", (inst, def) => {
return payload;
};
});
exports.$ZodNumberFormat = core.$constructor("$ZodNumber", (inst, def) => {
exports.$ZodNumberFormat = core.$constructor("$ZodNumberFormat", (inst, def) => {
checks.$ZodCheckNumberFormat.init(inst, def);
exports.$ZodNumber.init(inst, def); // no format checksp
exports.$ZodNumber.init(inst, def); // no format checks
});
exports.$ZodBoolean = core.$constructor("$ZodBoolean", (inst, def) => {
exports.$ZodType.init(inst, def);
@@ -530,7 +574,7 @@ exports.$ZodBigInt = core.$constructor("$ZodBigInt", (inst, def) => {
return payload;
};
});
exports.$ZodBigIntFormat = core.$constructor("$ZodBigInt", (inst, def) => {
exports.$ZodBigIntFormat = core.$constructor("$ZodBigIntFormat", (inst, def) => {
checks.$ZodCheckBigIntFormat.init(inst, def);
exports.$ZodBigInt.init(inst, def); // no format checks
});
@@ -684,58 +728,88 @@ exports.$ZodArray = core.$constructor("$ZodArray", (inst, def) => {
return payload; //handleArrayResultsAsync(parseResults, final);
};
});
function handleObjectResult(result, final, key) {
// if(isOptional)
function handlePropertyResult(result, final, key, input) {
if (result.issues.length) {
final.issues.push(...util.prefixIssues(key, result.issues));
}
final.value[key] = result.value;
}
function handleOptionalObjectResult(result, final, key, input) {
if (result.issues.length) {
// validation failed against value schema
if (input[key] === undefined) {
// if input was undefined, ignore the error
if (key in input) {
final.value[key] = undefined;
}
else {
final.value[key] = result.value;
}
}
else {
final.issues.push(...util.prefixIssues(key, result.issues));
}
}
else if (result.value === undefined) {
// validation returned `undefined`
if (key in input)
if (result.value === undefined) {
if (key in input) {
final.value[key] = undefined;
}
}
else {
// non-undefined value
final.value[key] = result.value;
}
}
function normalizeDef(def) {
const keys = Object.keys(def.shape);
for (const k of keys) {
if (!def.shape?.[k]?._zod?.traits?.has("$ZodType")) {
throw new Error(`Invalid element at key "${k}": expected a Zod schema`);
}
}
const okeys = util.optionalKeys(def.shape);
return {
...def,
keys,
keySet: new Set(keys),
numKeys: keys.length,
optionalKeys: new Set(okeys),
};
}
function handleCatchall(proms, input, payload, ctx, def, inst) {
const unrecognized = [];
// iterate over input keys
const keySet = def.keySet;
const _catchall = def.catchall._zod;
const t = _catchall.def.type;
for (const key in input) {
if (keySet.has(key))
continue;
if (t === "never") {
unrecognized.push(key);
continue;
}
const r = _catchall.run({ value: input[key], issues: [] }, ctx);
if (r instanceof Promise) {
proms.push(r.then((r) => handlePropertyResult(r, payload, key, input)));
}
else {
handlePropertyResult(r, payload, key, input);
}
}
if (unrecognized.length) {
payload.issues.push({
code: "unrecognized_keys",
keys: unrecognized,
input,
inst,
});
}
if (!proms.length)
return payload;
return Promise.all(proms).then(() => {
return payload;
});
}
exports.$ZodObject = core.$constructor("$ZodObject", (inst, def) => {
// requires cast because technically $ZodObject doesn't extend
exports.$ZodType.init(inst, def);
const _normalized = util.cached(() => {
const keys = Object.keys(def.shape);
for (const k of keys) {
if (!(def.shape[k] instanceof exports.$ZodType)) {
throw new Error(`Invalid element at key "${k}": expected a Zod schema`);
}
}
const okeys = util.optionalKeys(def.shape);
return {
shape: def.shape,
keys,
keySet: new Set(keys),
numKeys: keys.length,
optionalKeys: new Set(okeys),
};
});
// const sh = def.shape;
const desc = Object.getOwnPropertyDescriptor(def, "shape");
if (!desc?.get) {
const sh = def.shape;
Object.defineProperty(def, "shape", {
get: () => {
const newSh = { ...sh };
Object.defineProperty(def, "shape", {
value: newSh,
});
return newSh;
},
});
}
const _normalized = util.cached(() => normalizeDef(def));
util.defineLazy(inst._zod, "propValues", () => {
const shape = def.shape;
const propValues = {};
@@ -749,6 +823,45 @@ exports.$ZodObject = core.$constructor("$ZodObject", (inst, def) => {
}
return propValues;
});
const isObject = util.isObject;
const catchall = def.catchall;
let value;
inst._zod.parse = (payload, ctx) => {
value ?? (value = _normalized.value);
const input = payload.value;
if (!isObject(input)) {
payload.issues.push({
expected: "object",
code: "invalid_type",
input,
inst,
});
return payload;
}
payload.value = {};
const proms = [];
const shape = value.shape;
for (const key of value.keys) {
const el = shape[key];
const r = el._zod.run({ value: input[key], issues: [] }, ctx);
if (r instanceof Promise) {
proms.push(r.then((r) => handlePropertyResult(r, payload, key, input)));
}
else {
handlePropertyResult(r, payload, key, input);
}
}
if (!catchall) {
return proms.length ? Promise.all(proms).then(() => payload) : payload;
}
return handleCatchall(proms, input, payload, ctx, _normalized.value, inst);
};
});
exports.$ZodObjectJIT = core.$constructor("$ZodObjectJIT", (inst, def) => {
// requires cast because technically $ZodObject doesn't extend
exports.$ZodObject.init(inst, def);
const superParse = inst._zod.parse;
const _normalized = util.cached(() => normalizeDef(def));
const generateFastpass = (shape) => {
const doc = new doc_js_1.Doc(["shape", "payload", "ctx"]);
const normalized = _normalized.value;
@@ -763,44 +876,29 @@ exports.$ZodObject = core.$constructor("$ZodObject", (inst, def) => {
ids[key] = `key_${counter++}`;
}
// A: preserve key order {
doc.write(`const newResult = {}`);
doc.write(`const newResult = {};`);
for (const key of normalized.keys) {
if (normalized.optionalKeys.has(key)) {
const id = ids[key];
doc.write(`const ${id} = ${parseStr(key)};`);
const k = util.esc(key);
doc.write(`
const id = ids[key];
const k = util.esc(key);
doc.write(`const ${id} = ${parseStr(key)};`);
doc.write(`
if (${id}.issues.length) {
if (input[${k}] === undefined) {
if (${k} in input) {
newResult[${k}] = undefined;
}
} else {
payload.issues = payload.issues.concat(
${id}.issues.map((iss) => ({
...iss,
path: iss.path ? [${k}, ...iss.path] : [${k}],
}))
);
payload.issues = payload.issues.concat(${id}.issues.map(iss => ({
...iss,
path: iss.path ? [${k}, ...iss.path] : [${k}]
})));
}
if (${id}.value === undefined) {
if (${k} in input) {
newResult[${k}] = undefined;
}
} else if (${id}.value === undefined) {
if (${k} in input) newResult[${k}] = undefined;
} else {
newResult[${k}] = ${id}.value;
}
`);
}
else {
const id = ids[key];
// const id = ids[key];
doc.write(`const ${id} = ${parseStr(key)};`);
doc.write(`
if (${id}.issues.length) payload.issues = payload.issues.concat(${id}.issues.map(iss => ({
...iss,
path: iss.path ? [${util.esc(key)}, ...iss.path] : [${util.esc(key)}]
})));`);
doc.write(`newResult[${util.esc(key)}] = ${id}.value`);
}
`);
}
doc.write(`payload.value = newResult;`);
doc.write(`return payload;`);
@@ -826,80 +924,16 @@ exports.$ZodObject = core.$constructor("$ZodObject", (inst, def) => {
});
return payload;
}
const proms = [];
if (jit && fastEnabled && ctx?.async === false && ctx.jitless !== true) {
// always synchronous
if (!fastpass)
fastpass = generateFastpass(def.shape);
payload = fastpass(payload, ctx);
if (!catchall)
return payload;
return handleCatchall([], input, payload, ctx, value, inst);
}
else {
payload.value = {};
const shape = value.shape;
for (const key of value.keys) {
const el = shape[key];
// do not add omitted optional keys
// if (!(key in input)) {
// if (optionalKeys.has(key)) continue;
// payload.issues.push({
// code: "invalid_type",
// path: [key],
// expected: "nonoptional",
// note: `Missing required key: "${key}"`,
// input,
// inst,
// });
// }
const r = el._zod.run({ value: input[key], issues: [] }, ctx);
const isOptional = el._zod.optin === "optional" && el._zod.optout === "optional";
if (r instanceof Promise) {
proms.push(r.then((r) => isOptional ? handleOptionalObjectResult(r, payload, key, input) : handleObjectResult(r, payload, key)));
}
else if (isOptional) {
handleOptionalObjectResult(r, payload, key, input);
}
else {
handleObjectResult(r, payload, key);
}
}
}
if (!catchall) {
// return payload;
return proms.length ? Promise.all(proms).then(() => payload) : payload;
}
const unrecognized = [];
// iterate over input keys
const keySet = value.keySet;
const _catchall = catchall._zod;
const t = _catchall.def.type;
for (const key of Object.keys(input)) {
if (keySet.has(key))
continue;
if (t === "never") {
unrecognized.push(key);
continue;
}
const r = _catchall.run({ value: input[key], issues: [] }, ctx);
if (r instanceof Promise) {
proms.push(r.then((r) => handleObjectResult(r, payload, key)));
}
else {
handleObjectResult(r, payload, key);
}
}
if (unrecognized.length) {
payload.issues.push({
code: "unrecognized_keys",
keys: unrecognized,
input,
inst,
});
}
if (!proms.length)
return payload;
return Promise.all(proms).then(() => {
return payload;
});
return superParse(payload, ctx);
};
});
function handleUnionResults(results, final, inst, ctx) {
@@ -909,6 +943,11 @@ function handleUnionResults(results, final, inst, ctx) {
return final;
}
}
const nonaborted = results.filter((r) => !util.aborted(r));
if (nonaborted.length === 1) {
final.value = nonaborted[0].value;
return nonaborted[0];
}
final.issues.push({
code: "invalid_union",
input: final.value,
@@ -934,7 +973,12 @@ exports.$ZodUnion = core.$constructor("$ZodUnion", (inst, def) => {
}
return undefined;
});
const single = def.options.length === 1;
const first = def.options[0]._zod.run;
inst._zod.parse = (payload, ctx) => {
if (single) {
return first(payload, ctx);
}
let async = false;
const results = [];
for (const option of def.options) {
@@ -984,7 +1028,7 @@ core.$constructor("$ZodDiscriminatedUnion", (inst, def) => {
const opts = def.options;
const map = new Map();
for (const o of opts) {
const values = o._zod.propValues[def.discriminator];
const values = o._zod.propValues?.[def.discriminator];
if (!values || values.size === 0)
throw new Error(`Invalid discriminated union option at index "${def.options.indexOf(o)}"`);
for (const v of values) {
@@ -1019,6 +1063,7 @@ core.$constructor("$ZodDiscriminatedUnion", (inst, def) => {
code: "invalid_union",
errors: [],
note: "No matching discriminator",
discriminator: def.discriminator,
input,
path: [def.discriminator],
inst,
@@ -1106,7 +1151,6 @@ function handleIntersectionResults(result, left, right) {
exports.$ZodTuple = core.$constructor("$ZodTuple", (inst, def) => {
exports.$ZodType.init(inst, def);
const items = def.items;
const optStart = items.length - [...items].reverse().findIndex((item) => item._zod.optin !== "optional");
inst._zod.parse = (payload, ctx) => {
const input = payload.value;
if (!Array.isArray(input)) {
@@ -1120,15 +1164,17 @@ exports.$ZodTuple = core.$constructor("$ZodTuple", (inst, def) => {
}
payload.value = [];
const proms = [];
const reversedIndex = [...items].reverse().findIndex((item) => item._zod.optin !== "optional");
const optStart = reversedIndex === -1 ? 0 : items.length - reversedIndex;
if (!def.rest) {
const tooBig = input.length > items.length;
const tooSmall = input.length < optStart - 1;
if (tooBig || tooSmall) {
payload.issues.push({
...(tooBig ? { code: "too_big", maximum: items.length } : { code: "too_small", minimum: items.length }),
input,
inst,
origin: "array",
...(tooBig ? { code: "too_big", maximum: items.length } : { code: "too_small", minimum: items.length }),
});
return payload;
}
@@ -1191,11 +1237,13 @@ exports.$ZodRecord = core.$constructor("$ZodRecord", (inst, def) => {
return payload;
}
const proms = [];
if (def.keyType._zod.values) {
const values = def.keyType._zod.values;
const values = def.keyType._zod.values;
if (values) {
payload.value = {};
const recordKeys = new Set();
for (const key of values) {
if (typeof key === "string" || typeof key === "number" || typeof key === "symbol") {
recordKeys.add(typeof key === "number" ? key.toString() : key);
const result = def.valueType._zod.run({ value: input[key], issues: [] }, ctx);
if (result instanceof Promise) {
proms.push(result.then((result) => {
@@ -1215,7 +1263,7 @@ exports.$ZodRecord = core.$constructor("$ZodRecord", (inst, def) => {
}
let unrecognized;
for (const key in input) {
if (!values.has(key)) {
if (!recordKeys.has(key)) {
unrecognized = unrecognized ?? [];
unrecognized.push(key);
}
@@ -1240,8 +1288,8 @@ exports.$ZodRecord = core.$constructor("$ZodRecord", (inst, def) => {
}
if (keyResult.issues.length) {
payload.issues.push({
origin: "record",
code: "invalid_key",
origin: "record",
issues: keyResult.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config())),
input: key,
path: [key],
@@ -1312,8 +1360,8 @@ function handleMapResult(keyResult, valueResult, final, key, input, inst, ctx) {
}
else {
final.issues.push({
origin: "map",
code: "invalid_key",
origin: "map",
input,
inst,
issues: keyResult.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config())),
@@ -1374,14 +1422,15 @@ function handleSetResult(result, final) {
exports.$ZodEnum = core.$constructor("$ZodEnum", (inst, def) => {
exports.$ZodType.init(inst, def);
const values = util.getEnumValues(def.entries);
inst._zod.values = new Set(values);
const valuesSet = new Set(values);
inst._zod.values = valuesSet;
inst._zod.pattern = new RegExp(`^(${values
.filter((k) => util.propertyKeyTypes.has(typeof k))
.map((o) => (typeof o === "string" ? util.escapeRegex(o) : o.toString()))
.join("|")})$`);
inst._zod.parse = (payload, _ctx) => {
const input = payload.value;
if (inst._zod.values.has(input)) {
if (valuesSet.has(input)) {
return payload;
}
payload.issues.push({
@@ -1395,13 +1444,17 @@ exports.$ZodEnum = core.$constructor("$ZodEnum", (inst, def) => {
});
exports.$ZodLiteral = core.$constructor("$ZodLiteral", (inst, def) => {
exports.$ZodType.init(inst, def);
inst._zod.values = new Set(def.values);
if (def.values.length === 0) {
throw new Error("Cannot create literal schema with no valid values");
}
const values = new Set(def.values);
inst._zod.values = values;
inst._zod.pattern = new RegExp(`^(${def.values
.map((o) => (typeof o === "string" ? util.escapeRegex(o) : o ? o.toString() : String(o)))
.map((o) => (typeof o === "string" ? util.escapeRegex(o) : o ? util.escapeRegex(o.toString()) : String(o)))
.join("|")})$`);
inst._zod.parse = (payload, _ctx) => {
const input = payload.value;
if (inst._zod.values.has(input)) {
if (values.has(input)) {
return payload;
}
payload.issues.push({
@@ -1417,6 +1470,7 @@ exports.$ZodFile = core.$constructor("$ZodFile", (inst, def) => {
exports.$ZodType.init(inst, def);
inst._zod.parse = (payload, _ctx) => {
const input = payload.value;
// @ts-ignore
if (input instanceof File)
return payload;
payload.issues.push({
@@ -1430,9 +1484,12 @@ exports.$ZodFile = core.$constructor("$ZodFile", (inst, def) => {
});
exports.$ZodTransform = core.$constructor("$ZodTransform", (inst, def) => {
exports.$ZodType.init(inst, def);
inst._zod.parse = (payload, _ctx) => {
inst._zod.parse = (payload, ctx) => {
if (ctx.direction === "backward") {
throw new core.$ZodEncodeError(inst.constructor.name);
}
const _out = def.transform(payload.value, payload);
if (_ctx.async) {
if (ctx.async) {
const output = _out instanceof Promise ? _out : Promise.resolve(_out);
return output.then((output) => {
payload.value = output;
@@ -1446,6 +1503,12 @@ exports.$ZodTransform = core.$constructor("$ZodTransform", (inst, def) => {
return payload;
};
});
function handleOptionalResult(result, input) {
if (result.issues.length && input === undefined) {
return { issues: [], value: undefined };
}
return result;
}
exports.$ZodOptional = core.$constructor("$ZodOptional", (inst, def) => {
exports.$ZodType.init(inst, def);
inst._zod.optin = "optional";
@@ -1459,7 +1522,10 @@ exports.$ZodOptional = core.$constructor("$ZodOptional", (inst, def) => {
});
inst._zod.parse = (payload, ctx) => {
if (def.innerType._zod.optin === "optional") {
return def.innerType._zod.run(payload, ctx);
const result = def.innerType._zod.run(payload, ctx);
if (result instanceof Promise)
return result.then((r) => handleOptionalResult(r, payload.value));
return handleOptionalResult(result, payload.value);
}
if (payload.value === undefined) {
return payload;
@@ -1479,6 +1545,7 @@ exports.$ZodNullable = core.$constructor("$ZodNullable", (inst, def) => {
return def.innerType._zod.values ? new Set([...def.innerType._zod.values, null]) : undefined;
});
inst._zod.parse = (payload, ctx) => {
// Forward direction (decode): allow null to pass through
if (payload.value === null)
return payload;
return def.innerType._zod.run(payload, ctx);
@@ -1490,13 +1557,18 @@ exports.$ZodDefault = core.$constructor("$ZodDefault", (inst, def) => {
inst._zod.optin = "optional";
util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
inst._zod.parse = (payload, ctx) => {
if (ctx.direction === "backward") {
return def.innerType._zod.run(payload, ctx);
}
// Forward direction (decode): apply defaults for undefined input
if (payload.value === undefined) {
payload.value = def.defaultValue;
/**
* $ZodDefault always returns the default value immediately.
* $ZodDefault returns the default value immediately in forward direction.
* It doesn't pass the default value into the validator ("prefault"). There's no reason to pass the default value through validation. The validity of the default is enforced by TypeScript statically. Otherwise, it's the responsibility of the user to ensure the default is valid. In the case of pipes with divergent in/out types, you can specify the default on the `in` schema of your ZodPipe to set a "prefault" for the pipe. */
return payload;
}
// Forward direction: continue with default handling
const result = def.innerType._zod.run(payload, ctx);
if (result instanceof Promise) {
return result.then((result) => handleDefaultResult(result, def));
@@ -1515,6 +1587,10 @@ exports.$ZodPrefault = core.$constructor("$ZodPrefault", (inst, def) => {
inst._zod.optin = "optional";
util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
inst._zod.parse = (payload, ctx) => {
if (ctx.direction === "backward") {
return def.innerType._zod.run(payload, ctx);
}
// Forward direction (decode): apply prefault for undefined input
if (payload.value === undefined) {
payload.value = def.defaultValue;
}
@@ -1549,6 +1625,9 @@ function handleNonOptionalResult(payload, inst) {
exports.$ZodSuccess = core.$constructor("$ZodSuccess", (inst, def) => {
exports.$ZodType.init(inst, def);
inst._zod.parse = (payload, ctx) => {
if (ctx.direction === "backward") {
throw new core.$ZodEncodeError("ZodSuccess");
}
const result = def.innerType._zod.run(payload, ctx);
if (result instanceof Promise) {
return result.then((result) => {
@@ -1562,10 +1641,14 @@ exports.$ZodSuccess = core.$constructor("$ZodSuccess", (inst, def) => {
});
exports.$ZodCatch = core.$constructor("$ZodCatch", (inst, def) => {
exports.$ZodType.init(inst, def);
inst._zod.optin = "optional";
util.defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
inst._zod.parse = (payload, ctx) => {
if (ctx.direction === "backward") {
return def.innerType._zod.run(payload, ctx);
}
// Forward direction (decode): apply catch logic
const result = def.innerType._zod.run(payload, ctx);
if (result instanceof Promise) {
return result.then((result) => {
@@ -1617,27 +1700,94 @@ exports.$ZodPipe = core.$constructor("$ZodPipe", (inst, def) => {
util.defineLazy(inst._zod, "values", () => def.in._zod.values);
util.defineLazy(inst._zod, "optin", () => def.in._zod.optin);
util.defineLazy(inst._zod, "optout", () => def.out._zod.optout);
util.defineLazy(inst._zod, "propValues", () => def.in._zod.propValues);
inst._zod.parse = (payload, ctx) => {
if (ctx.direction === "backward") {
const right = def.out._zod.run(payload, ctx);
if (right instanceof Promise) {
return right.then((right) => handlePipeResult(right, def.in, ctx));
}
return handlePipeResult(right, def.in, ctx);
}
const left = def.in._zod.run(payload, ctx);
if (left instanceof Promise) {
return left.then((left) => handlePipeResult(left, def, ctx));
return left.then((left) => handlePipeResult(left, def.out, ctx));
}
return handlePipeResult(left, def, ctx);
return handlePipeResult(left, def.out, ctx);
};
});
function handlePipeResult(left, def, ctx) {
if (util.aborted(left)) {
function handlePipeResult(left, next, ctx) {
if (left.issues.length) {
// prevent further checks
left.aborted = true;
return left;
}
return def.out._zod.run({ value: left.value, issues: left.issues }, ctx);
return next._zod.run({ value: left.value, issues: left.issues }, ctx);
}
exports.$ZodCodec = core.$constructor("$ZodCodec", (inst, def) => {
exports.$ZodType.init(inst, def);
util.defineLazy(inst._zod, "values", () => def.in._zod.values);
util.defineLazy(inst._zod, "optin", () => def.in._zod.optin);
util.defineLazy(inst._zod, "optout", () => def.out._zod.optout);
util.defineLazy(inst._zod, "propValues", () => def.in._zod.propValues);
inst._zod.parse = (payload, ctx) => {
const direction = ctx.direction || "forward";
if (direction === "forward") {
const left = def.in._zod.run(payload, ctx);
if (left instanceof Promise) {
return left.then((left) => handleCodecAResult(left, def, ctx));
}
return handleCodecAResult(left, def, ctx);
}
else {
const right = def.out._zod.run(payload, ctx);
if (right instanceof Promise) {
return right.then((right) => handleCodecAResult(right, def, ctx));
}
return handleCodecAResult(right, def, ctx);
}
};
});
function handleCodecAResult(result, def, ctx) {
if (result.issues.length) {
// prevent further checks
result.aborted = true;
return result;
}
const direction = ctx.direction || "forward";
if (direction === "forward") {
const transformed = def.transform(result.value, result);
if (transformed instanceof Promise) {
return transformed.then((value) => handleCodecTxResult(result, value, def.out, ctx));
}
return handleCodecTxResult(result, transformed, def.out, ctx);
}
else {
const transformed = def.reverseTransform(result.value, result);
if (transformed instanceof Promise) {
return transformed.then((value) => handleCodecTxResult(result, value, def.in, ctx));
}
return handleCodecTxResult(result, transformed, def.in, ctx);
}
}
function handleCodecTxResult(left, value, nextSchema, ctx) {
// Check if transform added any issues
if (left.issues.length) {
left.aborted = true;
return left;
}
return nextSchema._zod.run({ value, issues: left.issues }, ctx);
}
exports.$ZodReadonly = core.$constructor("$ZodReadonly", (inst, def) => {
exports.$ZodType.init(inst, def);
util.defineLazy(inst._zod, "propValues", () => def.innerType._zod.propValues);
util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
util.defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
util.defineLazy(inst._zod, "optin", () => def.innerType?._zod?.optin);
util.defineLazy(inst._zod, "optout", () => def.innerType?._zod?.optout);
inst._zod.parse = (payload, ctx) => {
if (ctx.direction === "backward") {
return def.innerType._zod.run(payload, ctx);
}
const result = def.innerType._zod.run(payload, ctx);
if (result instanceof Promise) {
return result.then(handleReadonlyResult);
@@ -1653,7 +1803,8 @@ exports.$ZodTemplateLiteral = core.$constructor("$ZodTemplateLiteral", (inst, de
exports.$ZodType.init(inst, def);
const regexParts = [];
for (const part of def.parts) {
if (part instanceof exports.$ZodType) {
if (typeof part === "object" && part !== null) {
// is Zod schema
if (!part._zod.pattern) {
// if (!source)
throw new Error(`Invalid template literal part, no pattern found: ${[...part._zod.traits].shift()}`);
@@ -1689,7 +1840,7 @@ exports.$ZodTemplateLiteral = core.$constructor("$ZodTemplateLiteral", (inst, de
input: payload.value,
inst,
code: "invalid_format",
format: "template_literal",
format: def.format ?? "template_literal",
pattern: inst._zod.pattern.source,
});
return payload;
@@ -1697,6 +1848,85 @@ exports.$ZodTemplateLiteral = core.$constructor("$ZodTemplateLiteral", (inst, de
return payload;
};
});
exports.$ZodFunction = core.$constructor("$ZodFunction", (inst, def) => {
exports.$ZodType.init(inst, def);
inst._def = def;
inst._zod.def = def;
inst.implement = (func) => {
if (typeof func !== "function") {
throw new Error("implement() must be called with a function");
}
return function (...args) {
const parsedArgs = inst._def.input ? (0, parse_js_1.parse)(inst._def.input, args) : args;
const result = Reflect.apply(func, this, parsedArgs);
if (inst._def.output) {
return (0, parse_js_1.parse)(inst._def.output, result);
}
return result;
};
};
inst.implementAsync = (func) => {
if (typeof func !== "function") {
throw new Error("implementAsync() must be called with a function");
}
return async function (...args) {
const parsedArgs = inst._def.input ? await (0, parse_js_1.parseAsync)(inst._def.input, args) : args;
const result = await Reflect.apply(func, this, parsedArgs);
if (inst._def.output) {
return await (0, parse_js_1.parseAsync)(inst._def.output, result);
}
return result;
};
};
inst._zod.parse = (payload, _ctx) => {
if (typeof payload.value !== "function") {
payload.issues.push({
code: "invalid_type",
expected: "function",
input: payload.value,
inst,
});
return payload;
}
// Check if output is a promise type to determine if we should use async implementation
const hasPromiseOutput = inst._def.output && inst._def.output._zod.def.type === "promise";
if (hasPromiseOutput) {
payload.value = inst.implementAsync(payload.value);
}
else {
payload.value = inst.implement(payload.value);
}
return payload;
};
inst.input = (...args) => {
const F = inst.constructor;
if (Array.isArray(args[0])) {
return new F({
type: "function",
input: new exports.$ZodTuple({
type: "tuple",
items: args[0],
rest: args[1],
}),
output: inst._def.output,
});
}
return new F({
type: "function",
input: args[0],
output: inst._def.output,
});
};
inst.output = (output) => {
const F = inst.constructor;
return new F({
type: "function",
input: inst._def.input,
output,
});
};
return inst;
});
exports.$ZodPromise = core.$constructor("$ZodPromise", (inst, def) => {
exports.$ZodType.init(inst, def);
inst._zod.parse = (payload, ctx) => {
@@ -1705,11 +1935,18 @@ exports.$ZodPromise = core.$constructor("$ZodPromise", (inst, def) => {
});
exports.$ZodLazy = core.$constructor("$ZodLazy", (inst, def) => {
exports.$ZodType.init(inst, def);
// let _innerType!: any;
// util.defineLazy(def, "getter", () => {
// if (!_innerType) {
// _innerType = def.getter();
// }
// return () => _innerType;
// });
util.defineLazy(inst._zod, "innerType", () => def.getter());
util.defineLazy(inst._zod, "pattern", () => inst._zod.innerType._zod.pattern);
util.defineLazy(inst._zod, "propValues", () => inst._zod.innerType._zod.propValues);
util.defineLazy(inst._zod, "optin", () => inst._zod.innerType._zod.optin);
util.defineLazy(inst._zod, "optout", () => inst._zod.innerType._zod.optout);
util.defineLazy(inst._zod, "pattern", () => inst._zod.innerType?._zod?.pattern);
util.defineLazy(inst._zod, "propValues", () => inst._zod.innerType?._zod?.propValues);
util.defineLazy(inst._zod, "optin", () => inst._zod.innerType?._zod?.optin ?? undefined);
util.defineLazy(inst._zod, "optout", () => inst._zod.innerType?._zod?.optout ?? undefined);
inst._zod.parse = (payload, ctx) => {
const inner = inst._zod.innerType;
return inner._zod.run(payload, ctx);