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
+436 -199
View File
@@ -1,7 +1,7 @@
import * as checks from "./checks.js";
import * as core from "./core.js";
import { Doc } from "./doc.js";
import { safeParse, safeParseAsync } from "./parse.js";
import { parse, parseAsync, safeParse, safeParseAsync } from "./parse.js";
import * as regexes from "./regexes.js";
import * as util from "./util.js";
import { version } from "./versions.js";
@@ -16,7 +16,6 @@ export const $ZodType = /*@__PURE__*/ 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);
@@ -73,7 +72,47 @@ export const $ZodType = /*@__PURE__*/ 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)
@@ -156,9 +195,10 @@ export const $ZodURL = /*@__PURE__*/ core.$constructor("$ZodURL", (inst, def) =>
$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)) {
@@ -166,7 +206,7 @@ export const $ZodURL = /*@__PURE__*/ 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,
@@ -187,12 +227,14 @@ export const $ZodURL = /*@__PURE__*/ 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;
}
@@ -254,20 +296,15 @@ export const $ZodISODuration = /*@__PURE__*/ core.$constructor("$ZodISODuration"
export const $ZodIPv4 = /*@__PURE__*/ core.$constructor("$ZodIPv4", (inst, def) => {
def.pattern ?? (def.pattern = regexes.ipv4);
$ZodStringFormat.init(inst, def);
inst._zod.onattach.push((inst) => {
const bag = inst._zod.bag;
bag.format = `ipv4`;
});
inst._zod.bag.format = `ipv4`;
});
export const $ZodIPv6 = /*@__PURE__*/ core.$constructor("$ZodIPv6", (inst, def) => {
def.pattern ?? (def.pattern = regexes.ipv6);
$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;
}
@@ -282,6 +319,11 @@ export const $ZodIPv6 = /*@__PURE__*/ core.$constructor("$ZodIPv6", (inst, def)
}
};
});
export const $ZodMAC = /*@__PURE__*/ core.$constructor("$ZodMAC", (inst, def) => {
def.pattern ?? (def.pattern = regexes.mac(def.delimiter));
$ZodStringFormat.init(inst, def);
inst._zod.bag.format = `mac`;
});
export const $ZodCIDRv4 = /*@__PURE__*/ core.$constructor("$ZodCIDRv4", (inst, def) => {
def.pattern ?? (def.pattern = regexes.cidrv4);
$ZodStringFormat.init(inst, def);
@@ -290,8 +332,11 @@ export const $ZodCIDRv6 = /*@__PURE__*/ core.$constructor("$ZodCIDRv6", (inst, d
def.pattern ?? (def.pattern = regexes.cidrv6); // not used for validation
$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);
@@ -299,6 +344,7 @@ export const $ZodCIDRv6 = /*@__PURE__*/ core.$constructor("$ZodCIDRv6", (inst, d
throw new Error();
if (prefixNum < 0 || prefixNum > 128)
throw new Error();
// @ts-ignore
new URL(`http://[${address}]`);
}
catch {
@@ -319,6 +365,7 @@ export function isValidBase64(data) {
if (data.length % 4 !== 0)
return false;
try {
// @ts-ignore
atob(data);
return true;
}
@@ -329,9 +376,7 @@ export function isValidBase64(data) {
export const $ZodBase64 = /*@__PURE__*/ core.$constructor("$ZodBase64", (inst, def) => {
def.pattern ?? (def.pattern = regexes.base64);
$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;
@@ -355,9 +400,7 @@ export function isValidBase64URL(data) {
export const $ZodBase64URL = /*@__PURE__*/ core.$constructor("$ZodBase64URL", (inst, def) => {
def.pattern ?? (def.pattern = regexes.base64url);
$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;
@@ -383,6 +426,7 @@ export 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;
@@ -454,9 +498,9 @@ export const $ZodNumber = /*@__PURE__*/ core.$constructor("$ZodNumber", (inst, d
return payload;
};
});
export const $ZodNumberFormat = /*@__PURE__*/ core.$constructor("$ZodNumber", (inst, def) => {
export const $ZodNumberFormat = /*@__PURE__*/ core.$constructor("$ZodNumberFormat", (inst, def) => {
checks.$ZodCheckNumberFormat.init(inst, def);
$ZodNumber.init(inst, def); // no format checksp
$ZodNumber.init(inst, def); // no format checks
});
export const $ZodBoolean = /*@__PURE__*/ core.$constructor("$ZodBoolean", (inst, def) => {
$ZodType.init(inst, def);
@@ -499,7 +543,7 @@ export const $ZodBigInt = /*@__PURE__*/ core.$constructor("$ZodBigInt", (inst, d
return payload;
};
});
export const $ZodBigIntFormat = /*@__PURE__*/ core.$constructor("$ZodBigInt", (inst, def) => {
export const $ZodBigIntFormat = /*@__PURE__*/ core.$constructor("$ZodBigIntFormat", (inst, def) => {
checks.$ZodCheckBigIntFormat.init(inst, def);
$ZodBigInt.init(inst, def); // no format checks
});
@@ -653,58 +697,88 @@ export const $ZodArray = /*@__PURE__*/ 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;
});
}
export const $ZodObject = /*@__PURE__*/ core.$constructor("$ZodObject", (inst, def) => {
// requires cast because technically $ZodObject doesn't extend
$ZodType.init(inst, def);
const _normalized = util.cached(() => {
const keys = Object.keys(def.shape);
for (const k of keys) {
if (!(def.shape[k] instanceof $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 = {};
@@ -718,6 +792,45 @@ export const $ZodObject = /*@__PURE__*/ core.$constructor("$ZodObject", (inst, d
}
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);
};
});
export const $ZodObjectJIT = /*@__PURE__*/ core.$constructor("$ZodObjectJIT", (inst, def) => {
// requires cast because technically $ZodObject doesn't extend
$ZodObject.init(inst, def);
const superParse = inst._zod.parse;
const _normalized = util.cached(() => normalizeDef(def));
const generateFastpass = (shape) => {
const doc = new Doc(["shape", "payload", "ctx"]);
const normalized = _normalized.value;
@@ -732,44 +845,29 @@ export const $ZodObject = /*@__PURE__*/ core.$constructor("$ZodObject", (inst, d
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;`);
@@ -795,80 +893,16 @@ export const $ZodObject = /*@__PURE__*/ core.$constructor("$ZodObject", (inst, d
});
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) {
@@ -878,6 +912,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,
@@ -903,7 +942,12 @@ export const $ZodUnion = /*@__PURE__*/ 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) {
@@ -953,7 +997,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) {
@@ -988,6 +1032,7 @@ core.$constructor("$ZodDiscriminatedUnion", (inst, def) => {
code: "invalid_union",
errors: [],
note: "No matching discriminator",
discriminator: def.discriminator,
input,
path: [def.discriminator],
inst,
@@ -1075,7 +1120,6 @@ function handleIntersectionResults(result, left, right) {
export const $ZodTuple = /*@__PURE__*/ core.$constructor("$ZodTuple", (inst, def) => {
$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)) {
@@ -1089,15 +1133,17 @@ export const $ZodTuple = /*@__PURE__*/ 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;
}
@@ -1160,11 +1206,13 @@ export const $ZodRecord = /*@__PURE__*/ core.$constructor("$ZodRecord", (inst, d
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) => {
@@ -1184,7 +1232,7 @@ export const $ZodRecord = /*@__PURE__*/ core.$constructor("$ZodRecord", (inst, d
}
let unrecognized;
for (const key in input) {
if (!values.has(key)) {
if (!recordKeys.has(key)) {
unrecognized = unrecognized ?? [];
unrecognized.push(key);
}
@@ -1209,8 +1257,8 @@ export const $ZodRecord = /*@__PURE__*/ core.$constructor("$ZodRecord", (inst, d
}
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],
@@ -1281,8 +1329,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())),
@@ -1343,14 +1391,15 @@ function handleSetResult(result, final) {
export const $ZodEnum = /*@__PURE__*/ core.$constructor("$ZodEnum", (inst, def) => {
$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({
@@ -1364,13 +1413,17 @@ export const $ZodEnum = /*@__PURE__*/ core.$constructor("$ZodEnum", (inst, def)
});
export const $ZodLiteral = /*@__PURE__*/ core.$constructor("$ZodLiteral", (inst, def) => {
$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({
@@ -1386,6 +1439,7 @@ export const $ZodFile = /*@__PURE__*/ core.$constructor("$ZodFile", (inst, def)
$ZodType.init(inst, def);
inst._zod.parse = (payload, _ctx) => {
const input = payload.value;
// @ts-ignore
if (input instanceof File)
return payload;
payload.issues.push({
@@ -1399,9 +1453,12 @@ export const $ZodFile = /*@__PURE__*/ core.$constructor("$ZodFile", (inst, def)
});
export const $ZodTransform = /*@__PURE__*/ core.$constructor("$ZodTransform", (inst, def) => {
$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;
@@ -1415,6 +1472,12 @@ export const $ZodTransform = /*@__PURE__*/ core.$constructor("$ZodTransform", (i
return payload;
};
});
function handleOptionalResult(result, input) {
if (result.issues.length && input === undefined) {
return { issues: [], value: undefined };
}
return result;
}
export const $ZodOptional = /*@__PURE__*/ core.$constructor("$ZodOptional", (inst, def) => {
$ZodType.init(inst, def);
inst._zod.optin = "optional";
@@ -1428,7 +1491,10 @@ export const $ZodOptional = /*@__PURE__*/ core.$constructor("$ZodOptional", (ins
});
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;
@@ -1448,6 +1514,7 @@ export const $ZodNullable = /*@__PURE__*/ core.$constructor("$ZodNullable", (ins
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);
@@ -1459,13 +1526,18 @@ export const $ZodDefault = /*@__PURE__*/ core.$constructor("$ZodDefault", (inst,
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));
@@ -1484,6 +1556,10 @@ export const $ZodPrefault = /*@__PURE__*/ core.$constructor("$ZodPrefault", (ins
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;
}
@@ -1518,6 +1594,9 @@ function handleNonOptionalResult(payload, inst) {
export const $ZodSuccess = /*@__PURE__*/ core.$constructor("$ZodSuccess", (inst, def) => {
$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) => {
@@ -1531,10 +1610,14 @@ export const $ZodSuccess = /*@__PURE__*/ core.$constructor("$ZodSuccess", (inst,
});
export const $ZodCatch = /*@__PURE__*/ core.$constructor("$ZodCatch", (inst, def) => {
$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) => {
@@ -1586,27 +1669,94 @@ export const $ZodPipe = /*@__PURE__*/ 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);
}
export const $ZodCodec = /*@__PURE__*/ core.$constructor("$ZodCodec", (inst, def) => {
$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);
}
export const $ZodReadonly = /*@__PURE__*/ core.$constructor("$ZodReadonly", (inst, def) => {
$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);
@@ -1622,7 +1772,8 @@ export const $ZodTemplateLiteral = /*@__PURE__*/ core.$constructor("$ZodTemplate
$ZodType.init(inst, def);
const regexParts = [];
for (const part of def.parts) {
if (part instanceof $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()}`);
@@ -1658,7 +1809,7 @@ export const $ZodTemplateLiteral = /*@__PURE__*/ core.$constructor("$ZodTemplate
input: payload.value,
inst,
code: "invalid_format",
format: "template_literal",
format: def.format ?? "template_literal",
pattern: inst._zod.pattern.source,
});
return payload;
@@ -1666,6 +1817,85 @@ export const $ZodTemplateLiteral = /*@__PURE__*/ core.$constructor("$ZodTemplate
return payload;
};
});
export const $ZodFunction = /*@__PURE__*/ core.$constructor("$ZodFunction", (inst, def) => {
$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 ? parse(inst._def.input, args) : args;
const result = Reflect.apply(func, this, parsedArgs);
if (inst._def.output) {
return 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 parseAsync(inst._def.input, args) : args;
const result = await Reflect.apply(func, this, parsedArgs);
if (inst._def.output) {
return await 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 $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;
});
export const $ZodPromise = /*@__PURE__*/ core.$constructor("$ZodPromise", (inst, def) => {
$ZodType.init(inst, def);
inst._zod.parse = (payload, ctx) => {
@@ -1674,11 +1904,18 @@ export const $ZodPromise = /*@__PURE__*/ core.$constructor("$ZodPromise", (inst,
});
export const $ZodLazy = /*@__PURE__*/ core.$constructor("$ZodLazy", (inst, def) => {
$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);