openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1,489 lines (1,488 loc) • 48.2 kB
JavaScript
import { An as isValidCreditCard, Bn as validateURL, Dn as isValidBase64, Hn as Doc, Ln as stripTabAndNewline, Mi as sizeCaps, Mn as isValidIPv6, Nn as isValidJWT, On as isValidBase64URL, Pn as mergeValues, Qr as $ZodAsyncError, Rn as urlHostnameOk, _r as number, ai as CONSTANT_CATCH, ci as codePointLength, di as esc, gi as isPlainObject, ji as shallowClone, jn as isValidIBAN, kn as isValidCIDRv6, pi as floatSafeRemainder, si as clone, zn as urlProtocolOk } from "./json-schema-processors-CIhP-F53.mjs";
import { Ha as isRecursiveSchema, Va as isBackEdge } from "./schemas-Cy6abvsu.mjs";
//#region node_modules/.pnpm/zod@4.6.7/node_modules/zod/v4/core/compile.js
/** @internal Sentinel the compiled fast path returns when validation fails. */
const INVALID = Symbol.for("zod.compile.invalid");
const FALLBACK_FLAG = Symbol.for("zod.compile.fallback");
/** Raised when the schema contains async refinements or transforms. Surfaces only under `compile(schema, { strict: true })`. */
var ZodCompileAsyncError = class extends Error {
constructor(e = "z.compile does not support async refinements, transforms, or checks") {
super(e);
this.name = "ZodCompileAsyncError";
}
};
/**
* Raised when the schema contains a feature whose semantics the fast path
* can't fully model. Both the shim in `zod/compile` and the default
* `compile()` fall back to the runtime parser for that schema; only
* `compile(schema, { strict: true })` lets it surface.
*/
var ZodCompileUnsupportedError = class extends Error {
constructor(e, t = true) {
super(`z.compile does not support ${e}; this schema must use the runtime parser`);
this.name = "ZodCompileUnsupportedError";
this.islandable = t;
}
};
/**
* Build the validator `validate` calls: the same codegen as the parser with the output construction
* dropped. A schema the flag cannot express reuses the parser, which still answers correctly — it
* just builds a value nothing reads.
*/
function compileValidator(e, t) {
try {
return compileFn(e, { assertOnly: true });
} catch {
return t;
}
}
/**
* AOT-compile a Zod schema. Returns a clone whose `_zod.run` calls a generated
* fast path first and falls back to the original runtime parser on failure.
*
* - Forward direction only. Backward (encode), async, and `skipChecks` paths
* bypass the fast path and use the runtime directly.
* - Never throws. A schema the fast path can't model is returned unchanged and
* keeps using the runtime parser. Pass `{ strict: true }` to get the refusal
* as a thrown `ZodCompileUnsupportedError` / `ZodCompileAsyncError` instead.
* - The original schema is unchanged. The clone shares children by reference.
*/
function compile(e, t) {
try {
const t = compileFn(e);
const n = withParser(e, t);
n._zod.bag.validator = compileValidator(e, t);
return n;
} catch (n) {
if (t?.strict) throw n;
return e;
}
}
/**
* Install an already-generated parser as a schema's fast path. Returns a clone; the original is
* unchanged.
*
* The parser takes the input and returns the parsed value, or `INVALID` to hand the parse to the
* runtime. It must be synchronous and forward-direction, and it must build fresh output rather than
* return its input — Zod cannot check either, and a wrong *success* is returned to the caller as-is.
*
* `compile()` is the ordinary entry point. This is for a build-time or native compiler that produces
* a parser where `new Function` is unavailable.
*/
function withParser(e, t) {
if (isRecursiveSchema(e)) throw new ZodCompileUnsupportedError("a schema whose subtree contains a reference cycle");
const n = clone(e);
const r = e._zod.run;
const i = r.__originalRun ?? r;
const wrapped = (e, n) => {
if (n?.async || n?.direction === "backward" || n?.skipChecks || n?.[FALLBACK_FLAG]) return i(e, n);
if (n && isBackEdge(n, e.value)) return i(e, n);
const r = t(e.value);
if (r !== INVALID) {
e.value = r;
return e;
}
if (n) n[FALLBACK_FLAG] = true;
return i(e, n);
};
wrapped.__originalRun = i;
n._zod.bag.fallbackRun = i;
n._zod.bag.validator = t;
n._zod.run = wrapped;
if (!r.__originalRun) installCompiledUserMethods(n, e, t);
return n;
}
function installCompiledUserMethods(e, t, n) {
const r = e;
const i = t;
if (typeof i.safeParse === "function") {
const e = i.safeParse;
r.safeParse = (t, r) => {
const i = n(t);
if (i !== INVALID) return {
success: true,
data: i
};
return e(t, r);
};
}
if (typeof i.parse === "function") {
const e = i.parse;
r.parse = (t, r) => {
const i = n(t);
if (i !== INVALID) return i;
return e(t, r);
};
}
}
/**
* @internal Generate the standalone compiled function: a parser by default, a validator under
* `assertOnly`. Returns the parsed value, `true` where nothing reads the output, or `INVALID`. Consumers use `compile()`.
*/
function compileFn(e, t) {
let n = true;
try {
n = isRecursiveSchema(e);
} catch {}
if (n) throw new ZodCompileUnsupportedError("a schema whose subtree contains a reference cycle");
const r = {
constants: /* @__PURE__ */ new Map(),
constantCounter: 0,
varCounter: 0,
definite: true
};
const i = new Doc(["input"]);
const a = generateCheck(i, r, e, "input", !t?.assertOnly);
i.write(a === null ? `return true;` : `return ${a};`);
const o = ["INVALID", ...r.constants.keys()];
const s = [INVALID, ...r.constants.values()];
const c = i.content.join("\n");
const l = t?.debug ? o.length > 0 ? `// Constants: ${o.join(", ")}\n${c}` : c : "";
const u = Function;
const d = `return (input) => {\n${c}\n}`;
let f;
try {
f = new u(...o, d)(...s);
} catch (e) {
throw new ZodCompileUnsupportedError(`this schema (generated code failed to evaluate: ${e.message})`);
}
if (t?.debug) f.code = l;
f.definite = r.definite;
return f;
}
function addConstant(e, t) {
for (const [n, r] of e.constants) if (r === t) return n;
const n = `c${e.constantCounter++}`;
e.constants.set(n, t);
return n;
}
/** Hoists a user-supplied callback. Anything the schema's author wrote can throw, and generated code can reject an earlier sibling before ever reaching it, so this clears `definite` — a rejection is then no longer proof that the interpreter would have rejected rather than thrown. */
function addUserConstant(e, t) {
e.definite = false;
return addConstant(e, t);
}
function newVar(e) {
return `v${e.varCounter++}`;
}
function runtimeRun(e, t) {
const n = e._zod.run({
value: t,
issues: []
}, {});
if (n && typeof n.then === "function") return INVALID;
const r = n;
return r.issues.length === 0 ? r.value : INVALID;
}
function compileChild(e, t, n, r, i = true) {
const a = e.content.length;
const o = t.constants.size;
const s = t.constantCounter;
const c = t.varCounter;
try {
return generateCheck(e, t, n, r, i);
} catch (i) {
if (!(i instanceof ZodCompileUnsupportedError) || !i.islandable) throw i;
e.content.length = a;
if (t.constants.size > o) {
const e = Array.from(t.constants.keys()).slice(o);
for (const n of e) t.constants.delete(n);
}
t.constantCounter = s;
t.varCounter = c;
return emitRuntimeIsland(e, t, n, r);
}
}
function emitRuntimeIsland(e, t, n, r) {
t.definite = false;
const i = addConstant(t, n);
const a = addConstant(t, runtimeRun);
const o = newVar(t);
e.write(`const ${o} = ${a}(${i}, ${r});`);
e.write(`if (${o} === INVALID) return INVALID;`);
return o;
}
const WHEN_DEFAULTED_CHECKS = /* @__PURE__ */ new Set([
"max_size",
"min_size",
"size_equals",
"max_length",
"min_length",
"length_equals"
]);
function generateChecks(e, t, n, r) {
const i = n._zod.def.checks;
if (!i || i.length === 0) return r;
let a = r;
for (const n of i) {
const r = n._zod.def;
if (r.when && !WHEN_DEFAULTED_CHECKS.has(r.check)) throw new ZodCompileUnsupportedError(`check with a custom "when" condition`);
switch (r.check) {
case "greater_than":
generateGreaterThanCheck(e, t, r, a);
break;
case "less_than":
generateLessThanCheck(e, t, r, a);
break;
case "multiple_of":
generateMultipleOfCheck(e, t, r, a);
break;
case "number_format":
generateNumberFormatCheck(e, r, a);
break;
case "min_length": {
const n = numericOperand(r.minimum, "min_length");
const i = codePointLengthVar(e, t, a, `${a}.length >= ${n} && ${a}.length < ${r.minimum * 2}`);
e.write(`if (${i} < ${n}) return INVALID;`);
break;
}
case "max_length": {
const n = numericOperand(r.maximum, "max_length");
const i = codePointLengthVar(e, t, a, `${a}.length > ${n}`);
e.write(`if (${i} > ${n}) return INVALID;`);
break;
}
case "length_equals": {
const n = numericOperand(r.length, "length_equals");
const i = codePointLengthVar(e, t, a, `${a}.length >= ${n} && ${a}.length <= ${r.length * 2}`);
e.write(`if (${i} !== ${n}) return INVALID;`);
break;
}
case "min_size":
e.write(`if (${a}.size < ${numericOperand(r.minimum, "min_size")}) return INVALID;`);
break;
case "max_size":
e.write(`if (${a}.size > ${numericOperand(r.maximum, "max_size")}) return INVALID;`);
break;
case "size_equals":
e.write(`if (${a}.size !== ${numericOperand(r.size, "size_equals")}) return INVALID;`);
break;
case "string_format":
a = generateStringFormatCheck(e, t, r, a);
break;
case "custom":
a = generateCustomRefineCheck(e, t, n, a);
break;
case "bigint_format":
generateBigIntFormatCheck(e, r, a);
break;
case "mime_type":
generateMimeTypeCheck(e, t, r, a);
break;
case "property":
generatePropertyCheck(e, t, r, a);
break;
case "properties":
generatePropertiesChecks(e, t, r, a);
break;
case "overwrite": {
const r = newVar(t);
generateOverwriteCheck(e, t, n, a, r);
a = r;
break;
}
default: throw new ZodCompileUnsupportedError(`check type ${r.check}`);
}
}
return a;
}
function codePointLengthVar(e, t, n, r) {
const i = addConstant(t, codePointLength);
const a = newVar(t);
e.write(`const ${a} = typeof ${n} === "string" && ${r} ? ${i}(${n}) : ${n}.length;`);
return a;
}
/**
* A count bound reaches generated source verbatim, so a non-number would be
* emitted as code rather than as a value — `min('0) {} evil(); if (0')` writes an
* arbitrary statement into the function body. TypeScript types these as `number`
* and fromJSONSchema guards them, so this is a backstop rather than a live hole,
* but generated source is the one place a wrong type stops being a type error.
*/
function numericOperand(e, t) {
if (typeof e !== "number" || !Number.isFinite(e)) throw new ZodCompileUnsupportedError(`${t} bound of type ${typeof e}`);
return `${e}`;
}
function comparisonOperand(e, t) {
if (typeof t === "bigint") return `${t}n`;
if (typeof t === "number") {
if (Number.isNaN(t)) throw new ZodCompileUnsupportedError("comparison check with NaN bound");
return `${t}`;
}
if (t instanceof Date) {
if (Number.isNaN(t.getTime())) throw new ZodCompileUnsupportedError("comparison check with Invalid Date bound");
return addConstant(e, t);
}
throw new ZodCompileUnsupportedError(`comparison check bound of type ${typeof t}`);
}
function generateGreaterThanCheck(e, t, n, r) {
const i = n.inclusive ? "<" : "<=";
e.write(`if (${r} ${i} ${comparisonOperand(t, n.value)}) return INVALID;`);
}
function generateLessThanCheck(e, t, n, r) {
const i = n.inclusive ? ">" : ">=";
e.write(`if (${r} ${i} ${comparisonOperand(t, n.value)}) return INVALID;`);
}
function generateMultipleOfCheck(e, t, n, r) {
if (typeof n.value === "bigint") {
if (n.value === BigInt(0)) throw new ZodCompileUnsupportedError("multiple_of check with a zero divisor");
e.write(`if (${r} % ${n.value}n !== 0n) return INVALID;`);
} else {
const i = addConstant(t, floatSafeRemainder);
e.write(`if (${i}(${r}, ${numericOperand(n.value, "multiple_of")}) !== 0) return INVALID;`);
}
}
function generateNumberFormatCheck(e, t, n) {
const r = t.format;
switch (r) {
case "safeint":
e.write(`if (!Number.isSafeInteger(${n})) return INVALID;`);
break;
case "int32":
e.write(`if (!Number.isInteger(${n}) || ${n} < -2147483648 || ${n} > 2147483647) return INVALID;`);
break;
case "uint32":
e.write(`if (!Number.isInteger(${n}) || ${n} < 0 || ${n} > 4294967295) return INVALID;`);
break;
case "float32":
e.write(`if (!Number.isFinite(${n}) || ${n} < -3.4028234663852886e38 || ${n} > 3.4028234663852886e38) return INVALID;`);
break;
case "float64":
e.write(`if (!Number.isFinite(${n})) return INVALID;`);
break;
default: throw new ZodCompileUnsupportedError(`number format ${r}`);
}
}
function generateBigIntFormatCheck(e, t, n) {
const r = t.format;
if (!r) return;
switch (r) {
case "int64":
e.write(`if (${n} < -9223372036854775808n || ${n} > 9223372036854775807n) return INVALID;`);
break;
case "uint64":
e.write(`if (${n} < 0n || ${n} > 18446744073709551615n) return INVALID;`);
break;
default: throw new ZodCompileUnsupportedError(`bigint format ${r}`);
}
}
function generateMimeTypeCheck(e, t, n, r) {
const i = n.mime;
if (i && i.length > 0) {
const n = addConstant(t, new Set(i));
e.write(`if (!${n}.has(${r}.type)) return INVALID;`);
}
}
function generatePropertiesChecks(e, t, n, r) {
if (n.when) throw new ZodCompileUnsupportedError(`check with a custom "when" condition`);
e.write(`if (${r} == null) return INVALID;`);
const i = n.shape;
for (const n of Reflect.ownKeys(i)) {
const a = typeof n === "symbol" ? addConstant(t, n) : esc(n);
const o = newVar(t);
e.write(`const ${o} = ${r}[${a}];`);
compileChild(e, t, i[n], o, false);
}
}
function generatePropertyCheck(e, t, n, r) {
const i = `${r}[${JSON.stringify(n.property)}]`;
generateCheck(e, t, n.schema, i);
}
function generateOverwriteCheck(e, t, n, r, i) {
const a = n._zod.def.tx;
if (!a) throw new ZodCompileUnsupportedError("overwrite check without a transform function");
if (isAsyncFunction(a)) throw new ZodCompileAsyncError("z.compile: async overwrite transforms are not supported");
const o = addConstant(t, a);
e.write(`const ${i} = ${o}(${r});`);
}
/** A predicate that hands back a thenable is an async check reached synchronously, and the interpreter throws `$ZodAsyncError` for it. Returning INVALID instead would be a bail-out, and a union reads a bail-out as a rejected branch and answers with a later one — so the throw has to survive into generated code. */
function throwAsync() {
throw new $ZodAsyncError();
}
/** Shared `addIssue` for the spoofed payloads a refine, check or transform receives. Allocating one per call — a fresh closure plus a `this`-bound method on a fresh literal — pinned every payload-allocating schema at ~2.7M ops/sec against 135M for a plain object literal. It captures nothing per call; it only reaches `this.issues`. */
function pushIssue(e) {
this.issues.push(e);
}
function generateCustomRefineCheck(e, t, n, r) {
const i = n._zod.def;
if (i.fn) {
if (isAsyncFunction(i.fn)) throw new ZodCompileAsyncError("z.compile: async .refine() predicates are not supported");
const n = addUserConstant(t, i.fn);
const a = addConstant(t, throwAsync);
const o = newVar(t);
e.write(`const ${o} = ${n}(${r});`);
e.write(`if (${o} instanceof Promise) ${a}();`);
e.write(`if (!${o}) return INVALID;`);
return r;
}
if (n._zod.check) {
if (isAsyncFunction(n._zod.check)) throw new ZodCompileAsyncError("z.compile: async .superRefine() / check functions are not supported");
const i = n._zod.check;
const helperFn = (e) => {
const t = {
value: e,
issues: [],
addIssue: pushIssue
};
if (i(t) instanceof Promise) throwAsync();
return t.issues.length === 0 ? t.value : INVALID;
};
const a = addUserConstant(t, helperFn);
const o = newVar(t);
e.write(`const ${o} = ${a}(${r});`);
e.write(`if (${o} === INVALID) return INVALID;`);
return o;
}
throw new ZodCompileUnsupportedError("custom check without a predicate or check function");
}
/**
* Built-in formats that validate with nothing but `def.pattern`, so compiling
* the regex reproduces the runtime exactly. Deliberately an allowlist: a format
* missing from it loses its fast path, while a format wrongly added to it
* silently accepts input the runtime rejects. Formats that layer extra
* validation over a shape-only pattern (`credit_card`, `base64`, `ipv6`, …) are
* handled above by hoisting the runtime validator itself.
*/
const PATTERN_IS_COMPLETE = /* @__PURE__ */ new Set([
"cidrv4",
"cuid",
"cuid2",
"date",
"datetime",
"duration",
"e164",
"email",
"emoji",
"ends_with",
"guid",
"includes",
"ipv4",
"ksuid",
"lowercase",
"mac",
"nanoid",
"regex",
"starts_with",
"time",
"ulid",
"uppercase",
"uuid",
"xid"
]);
function generateStringFormatCheck(e, t, n, r, i = true) {
const a = n.format;
if (a === "base64") {
const n = addConstant(t, isValidBase64);
e.write(`if (!${n}(${r})) return INVALID;`);
return r;
}
if (a === "base64url") {
const n = addConstant(t, isValidBase64URL);
e.write(`if (!${n}(${r})) return INVALID;`);
return r;
}
if (a === "jwt") {
const i = addConstant(t, isValidJWT);
const a = addConstant(t, n.alg ?? null);
e.write(`if (!${i}(${r}, ${a})) return INVALID;`);
return r;
}
if (a === "ipv6") {
const n = addConstant(t, isValidIPv6);
e.write(`if (!${n}(${r})) return INVALID;`);
return r;
}
if (a === "cidrv6") {
const n = addConstant(t, isValidCIDRv6);
e.write(`if (!${n}(${r})) return INVALID;`);
return r;
}
if (a === "credit_card") {
const n = addConstant(t, isValidCreditCard);
e.write(`if (!${n}(${r})) return INVALID;`);
return r;
}
if (a === "iban") {
const n = addConstant(t, isValidIBAN);
e.write(`if (!${n}(${r})) return INVALID;`);
return r;
}
const o = n;
if (a === "url" || a === "httpurl" || o.normalize || o.hostname !== void 0 || o.protocol !== void 0) {
const a = addConstant(t, validateURL);
const s = addConstant(t, n);
const c = newVar(t);
const l = newVar(t);
e.write(`const ${c} = ${r}.trim();`);
e.write(`const ${l} = ${a}(${c}, ${s});`);
e.write(`if (typeof ${l} === "number") return INVALID;`);
if (o.hostname !== void 0) {
const n = addConstant(t, urlHostnameOk);
e.write(`if (!${n}(${l}, ${s}.hostname)) return INVALID;`);
}
if (o.protocol !== void 0) {
const n = addConstant(t, urlProtocolOk);
e.write(`if (!${n}(${l}, ${s}.protocol)) return INVALID;`);
}
if (!i) return null;
const u = newVar(t);
const d = o.normalize ? `${l}.href` : `${addConstant(t, stripTabAndNewline)}(${c})`;
e.write(`const ${u} = ${d};`);
return u;
}
const s = n.fn;
if (s) {
if (isAsyncFunction(s)) throw new ZodCompileUnsupportedError(`async string format ${a}`);
const n = addConstant(t, s);
e.write(`if (!${n}(${r})) return INVALID;`);
return r;
}
if (PATTERN_IS_COMPLETE.has(a) && n.pattern) {
const i = addConstant(t, n.pattern);
e.write(`${i}.lastIndex = 0;`);
e.write(`if (!${i}.test(${r})) return INVALID;`);
return r;
}
const c = n.format;
switch (c) {
case "regex": throw new ZodCompileUnsupportedError("regex format without a pattern");
case "lowercase":
e.write(`if (${r} !== ${r}.toLowerCase()) return INVALID;`);
break;
case "uppercase":
e.write(`if (${r} !== ${r}.toUpperCase()) return INVALID;`);
break;
case "includes":
e.write(`if (!${r}.includes(${esc(n.includes)})) return INVALID;`);
break;
case "starts_with": {
const t = n.prefix;
e.write(`if (${r}.slice(0, ${t.length}) !== ${esc(t)}) return INVALID;`);
break;
}
case "ends_with": {
const t = n.suffix;
e.write(`if (${r}.slice(-${t.length}) !== ${esc(t)}) return INVALID;`);
break;
}
default: throw new ZodCompileUnsupportedError(`string format ${c}`);
}
return r;
}
function generateCheck(e, t, n, r, i = true) {
const a = n._zod.def;
const o = a.type;
if (a.coerce) throw new ZodCompileUnsupportedError(`coercion (z.coerce.${o}())`);
const s = i || !!a.checks?.length;
let c;
switch (o) {
case "string":
c = generateStringCheck(e, t, n, r, s);
break;
case "number":
c = generateNumberCheck(e, n, r);
break;
case "boolean":
c = generateBooleanCheck(e, r);
break;
case "bigint":
c = generateBigIntCheck(e, n, r);
break;
case "symbol":
c = generateSymbolCheck(e, r);
break;
case "undefined":
c = generateUndefinedCheck(e, r);
break;
case "null":
c = generateNullCheck(e, r);
break;
case "any":
case "unknown":
c = r;
break;
case "never":
e.write("return INVALID;");
c = r;
break;
case "void":
c = generateVoidCheck(e, r);
break;
case "nan":
c = generateNaNCheck(e, r);
break;
case "date":
c = generateDateCheck(e, r);
break;
case "object":
c = generateObjectCheck(e, t, n, r, s);
break;
case "optional":
c = generateOptionalCheck(e, t, n, r, s);
break;
case "nullable":
c = generateNullableCheck(e, t, n, r, s);
break;
case "array":
c = generateArrayCheck(e, t, n, r, s);
break;
case "literal":
c = generateLiteralCheck(e, t, n, r);
break;
case "enum":
c = generateEnumCheck(e, t, n, r);
break;
case "readonly": {
const i = generateWrapperCheck(e, t, n, r);
const a = newVar(t);
e.write(`const ${a} = Object.freeze(${i});`);
c = a;
break;
}
case "success":
generateWrapperCheck(e, t, n, r);
c = "true";
break;
case "default":
case "prefault":
c = generateDefaultCheck(e, t, n, r);
break;
case "nonoptional":
c = generateNonOptionalCheck(e, t, n, r);
break;
case "tuple":
c = generateTupleCheck(e, t, n, r);
break;
case "union":
c = generateUnionCheck(e, t, n, r);
break;
case "intersection":
c = generateIntersectionCheck(e, t, n, r);
break;
case "record":
c = generateRecordCheck(e, t, n, r);
break;
case "map":
c = generateMapCheck(e, t, n, r);
break;
case "set":
c = generateSetCheck(e, t, n, r);
break;
case "file":
c = generateFileCheck(e, r);
break;
case "template_literal":
c = generateTemplateLiteralCheck(e, t, n, r);
break;
case "lazy":
c = generateLazyCheck(e, t, n, r);
break;
case "pipe":
c = generatePipeCheck(e, t, n, r);
break;
case "custom":
c = generateCustomCheck(e, t, n, r);
break;
case "transform":
c = generateTransformCheck(e, t, n, r);
break;
case "catch":
c = generateCatchCheck(e, t, n, r);
break;
default: throw new ZodCompileUnsupportedError(`schema type ${o}`);
}
if (c === null) return null;
return generateChecks(e, t, n, c);
}
function generateStringCheck(e, t, n, r, i = true) {
e.write(`if (typeof ${r} !== "string") return INVALID;`);
const a = n._zod.def;
if (a.format === void 0) return r;
return generateStringFormatCheck(e, t, a, r, i);
}
function generateNumberCheck(e, t, n) {
e.write(`if (typeof ${n} !== "number" || !Number.isFinite(${n})) return INVALID;`);
const r = t._zod.def;
if (r.check === "number_format" && r.format) generateNumberFormatCheck(e, { format: r.format }, n);
return n;
}
function generateBooleanCheck(e, t) {
e.write(`if (typeof ${t} !== "boolean") return INVALID;`);
return t;
}
function generateBigIntCheck(e, t, n) {
e.write(`if (typeof ${n} !== "bigint") return INVALID;`);
const r = t._zod.def;
if (r.format) switch (r.format) {
case "int64":
e.write(`if (${n} < -9223372036854775808n || ${n} > 9223372036854775807n) return INVALID;`);
break;
case "uint64": e.write(`if (${n} < 0n || ${n} > 18446744073709551615n) return INVALID;`);
}
return n;
}
function generateSymbolCheck(e, t) {
e.write(`if (typeof ${t} !== "symbol") return INVALID;`);
return t;
}
function generateUndefinedCheck(e, t) {
e.write(`if (${t} !== undefined) return INVALID;`);
return t;
}
function generateNullCheck(e, t) {
e.write(`if (${t} !== null) return INVALID;`);
return t;
}
function generateVoidCheck(e, t) {
e.write(`if (${t} !== undefined) return INVALID;`);
return t;
}
function generateNaNCheck(e, t) {
e.write(`if (typeof ${t} !== "number" || !Number.isNaN(${t})) return INVALID;`);
return t;
}
function generateDateCheck(e, t) {
e.write(`if (!(${t} instanceof Date) || Number.isNaN(${t}.getTime())) return INVALID;`);
return t;
}
function generateObjectCheck(e, t, n, r, i = true) {
const a = n._zod.def;
e.write(`if (typeof ${r} !== "object" || ${r} === null || Array.isArray(${r})) return INVALID;`);
const o = a.shape;
const s = Object.keys(o);
const c = Object.getOwnPropertySymbols(o);
const l = c.length ? [...s, ...c] : s;
const keyExpr = (e) => typeof e === "symbol" ? addConstant(t, e) : esc(e);
const propKey = (e) => typeof e === "symbol" ? `[${keyExpr(e)}]` : esc(e);
const u = o;
if (s.includes("__proto__")) throw new ZodCompileUnsupportedError("object shape key \"__proto__\"");
const d = /* @__PURE__ */ new Map();
for (const n of l) {
const a = u[n];
const o = keyExpr(n);
const s = newVar(t);
e.write(`const ${s} = ${r}[${o}];`);
if (a._zod.optin !== void 0) {
const i = newVar(t);
e.write(`let ${i} = (() => {`);
e.indented((e) => {
const n = compileChild(e, t, a, s);
e.write(`return ${n};`);
});
e.write(`})();`);
if (a._zod.optout === "optional") {
e.write(`if (${i} === INVALID) {`);
e.indented((e) => {
e.write(`if (${o} in ${r}) return INVALID;`);
e.write(`${i} = undefined;`);
});
e.write(`}`);
} else e.write(`if (${i} === INVALID) return INVALID;`);
d.set(n, i);
} else {
if (requiresPresenceCheck(a)) e.write(`if (!(${o} in ${r})) return INVALID;`);
const c = compileChild(e, t, a, s, i);
if (c !== null) d.set(n, c);
}
}
const f = a.catchall;
let p = "none";
if (f) {
const t = f._zod.def.type;
if (t === "never") {
const t = s.map((e) => `k !== ${esc(e)}`).join(" && ") || "true";
e.write(`for (const k in ${r}) {`);
e.indented((e) => {
e.write(`if (${t}) return INVALID;`);
});
e.write(`}`);
} else if ((t === "unknown" || t === "any") && !f._zod.def.checks?.length) p = "passthrough";
else p = "schema";
}
const m = newVar(t);
const h = l.some((e) => mayOmitUndefined(u[e]) || dropsWhenAbsent(u[e]));
if (!i) {
if (p === "schema") {
const n = s.length > 0 ? addConstant(t, new Set(s)) : null;
e.write(`for (const k in ${r}) {`);
e.indented((e) => {
e.write(`if (k === "__proto__") continue;`);
if (n) e.write(`if (${n}.has(k)) continue;`);
const i = newVar(t);
e.write(`const ${i} = ${r}[k];`);
compileChild(e, t, f, i, false);
});
e.write(`}`);
}
return null;
}
if (!h) {
const t = l.map((e) => `${propKey(e)}: ${d.get(e)}`).join(", ");
e.write(`const ${m} = { ${t} };`);
} else {
e.write(`const ${m} = {};`);
for (const t of l) {
const n = keyExpr(t);
const i = d.get(t);
if (dropsWhenAbsent(u[t])) e.write(`if (${n} in ${r}) ${m}[${n}] = ${i};`);
else if (mayOmitUndefined(u[t])) e.write(`if (${i} !== undefined || ${n} in ${r}) ${m}[${n}] = ${i};`);
else e.write(`${m}[${n}] = ${i};`);
}
}
if (p !== "none") {
const n = s.length > 0 ? addConstant(t, new Set(s)) : null;
e.write(`for (const k in ${r}) {`);
e.indented((e) => {
e.write(`if (k === "__proto__") continue;`);
if (n) e.write(`if (${n}.has(k)) continue;`);
if (p === "passthrough") e.write(`${m}[k] = ${r}[k];`);
else {
const n = newVar(t);
e.write(`const ${n} = ${r}[k];`);
const i = compileChild(e, t, f, n);
e.write(`${m}[k] = ${i};`);
}
});
e.write(`}`);
}
return m;
}
function generateOptionalCheck(e, t, n, r, i = true) {
const a = n._zod.def;
if (isExactOptional(n)) return generateCheck(e, t, a.innerType, r, i);
if (a.innerType._zod.optin === "defaulted") {
const n = newVar(t);
const i = newVar(t);
e.write(`let ${n};`);
e.write(`if (${r} === undefined) {`);
e.indented((e) => {
e.write(`const ${i} = (() => {`);
e.indented((e) => {
const n = generateCheck(e, t, a.innerType, r);
e.write(`return ${n};`);
});
e.write(`})();`);
e.write(`if (${i} !== INVALID) ${n} = ${i};`);
});
e.write(`} else {`);
e.indented((e) => {
const i = generateCheck(e, t, a.innerType, r);
e.write(`${n} = ${i};`);
});
e.write(`}`);
return n;
}
const o = i ? newVar(t) : null;
if (o) e.write(`let ${o};`);
e.write(`if (${r} !== undefined) {`);
e.indented((e) => {
const n = generateCheck(e, t, a.innerType, r, i);
if (o && n !== null) e.write(`${o} = ${n};`);
});
e.write(`}`);
return o;
}
function isExactOptional(e) {
return e._zod.traits?.has("$ZodExactOptional") === true;
}
function requiresPresenceCheck(e) {
return e._zod.optin === void 0 && fastPathAcceptsAbsence(e);
}
function fastPathAcceptsAbsence(e) {
if (e._zod.def.coerce) return true;
const t = e._zod.def;
switch (t.type) {
case "any":
case "unknown":
case "undefined":
case "void":
case "default":
case "prefault":
case "transform":
case "custom":
case "lazy": return true;
case "string":
case "number":
case "boolean":
case "bigint":
case "symbol":
case "null":
case "never":
case "nan":
case "date":
case "object":
case "array":
case "tuple":
case "record":
case "map":
case "set":
case "file":
case "template_literal": return false;
case "nonoptional": return t.innerType ? fastPathAcceptsAbsence(t.innerType) : false;
case "literal": return !!t.values?.includes(void 0);
case "enum": return !!e._zod.values?.has(void 0);
case "optional":
case "nullable":
case "readonly":
case "success": return t.innerType ? fastPathAcceptsAbsence(t.innerType) : true;
case "catch": return true;
case "union": return t.options ? t.options.some(fastPathAcceptsAbsence) : true;
case "intersection":
if (!t.left || !t.right) return true;
return fastPathAcceptsAbsence(t.left) && fastPathAcceptsAbsence(t.right);
case "pipe": return t.in ? fastPathAcceptsAbsence(t.in) : true;
default: return true;
}
}
/** The middle rung permits absence without supplying anything in its place, so an absent key contributes nothing — mirrors the leading gate in `handlePropertyResult`. */
function dropsWhenAbsent(e) {
return e._zod.optin === "optional" && e._zod.optout === "optional";
}
function mayOmitUndefined(e) {
return (e._zod.optin !== "defaulted" || e._zod.optout === "optional") && mayOutputUndefined(e);
}
function mayOutputUndefined(e) {
const t = e._zod.def;
switch (t.type) {
case "string":
case "number":
case "boolean":
case "bigint":
case "symbol":
case "null":
case "nan":
case "date":
case "object":
case "array":
case "tuple":
case "record":
case "map":
case "set":
case "file":
case "template_literal":
case "never":
case "success": return false;
case "literal": return !!t.values?.includes(void 0);
case "enum": return !!e._zod.values?.has(void 0);
case "optional": return true;
case "nullable":
case "readonly":
case "nonoptional": return t.innerType ? mayOutputUndefined(t.innerType) : true;
case "union": return t.options ? t.options.some(mayOutputUndefined) : true;
case "intersection": return !t.left || !t.right || mayOutputUndefined(t.left) || mayOutputUndefined(t.right);
case "pipe": return t.out ? mayOutputUndefined(t.out) : true;
default: return true;
}
}
function generateNullableCheck(e, t, n, r, i = true) {
const a = n._zod.def;
const o = i ? newVar(t) : null;
if (o) e.write(`let ${o} = null;`);
e.write(`if (${r} !== null) {`);
e.indented((e) => {
const n = generateCheck(e, t, a.innerType, r, i);
if (o && n !== null) e.write(`${o} = ${n};`);
});
e.write(`}`);
return o;
}
function generateSizeGate(e, t, n) {
for (const [r, i] of sizeCaps(t._zod.def.checks)) e.write(`if (${n} > ${numericOperand(r, i._zod.def.check)}) return INVALID;`);
}
function generateArrayCheck(e, t, n, r, i = true) {
const a = n._zod.def;
e.write(`if (!Array.isArray(${r})) return INVALID;`);
generateSizeGate(e, n, `${r}.length`);
const o = i ? newVar(t) : null;
const s = newVar(t);
const c = newVar(t);
if (o) e.write(`const ${o} = new Array(${r}.length);`);
e.write(`for (let ${s} = 0; ${s} < ${r}.length; ${s}++) {`);
e.indented((e) => {
e.write(`const ${c} = ${r}[${s}];`);
const n = compileChild(e, t, a.element, c, i);
if (o && n !== null) e.write(`${o}[${s}] = ${n};`);
});
e.write(`}`);
return o;
}
function generateLiteralCheck(e, t, n, r) {
const i = n._zod.def.values;
if (i.length !== 1) {
const n = addConstant(t, new Set(i));
e.write(`if (!${n}.has(${r})) return INVALID;`);
return r;
}
const a = i[0];
if (typeof a === "number" && Number.isNaN(a)) {
const n = addConstant(t, new Set(i));
e.write(`if (!${n}.has(${r})) return INVALID;`);
return r;
}
if (typeof a === "string") e.write(`if (${r} !== ${esc(a)}) return INVALID;`);
else if (typeof a === "number" || typeof a === "boolean") e.write(`if (${r} !== ${a}) return INVALID;`);
else if (a === null) e.write(`if (${r} !== null) return INVALID;`);
else if (a === void 0) e.write(`if (${r} !== undefined) return INVALID;`);
else if (typeof a === "bigint") e.write(`if (${r} !== ${a}n) return INVALID;`);
else throw new ZodCompileUnsupportedError(`literal type ${typeof a}`);
return r;
}
function generateEnumCheck(e, t, n, r) {
const i = n._zod.values;
if (!i) throw new ZodCompileUnsupportedError("enum schema without enumerated values");
const a = addConstant(t, i);
e.write(`if (!${a}.has(${r})) return INVALID;`);
return r;
}
function generateWrapperCheck(e, t, n, r) {
const i = n._zod.def;
return generateCheck(e, t, i.innerType, r);
}
function generateDefaultCheck(e, t, n, r) {
const i = n._zod.def;
const a = Object.getOwnPropertyDescriptor(n._zod.def, "defaultValue") ? () => n._zod.def.defaultValue : void 0;
if (n._zod.def.type === "prefault") {
if (!a) return generateCheck(e, t, i.innerType, r);
const n = addConstant(t, a);
const o = newVar(t);
e.write(`let ${o} = ${r};`);
e.write(`if (${r} === undefined) ${o} = ${n}();`);
return generateCheck(e, t, i.innerType, o);
}
const o = newVar(t);
if (a) {
const n = addConstant(t, a);
const s = addConstant(t, shallowClone);
e.write(`let ${o};`);
e.write(`if (${r} === undefined) {`);
e.indented((e) => {
e.write(`${o} = ${s}(${n}());`);
});
e.write(`} else {`);
e.indented((e) => {
const a = generateCheck(e, t, i.innerType, r);
e.write(`${o} = ${a} === undefined ? ${s}(${n}()) : ${a};`);
});
e.write(`}`);
} else {
e.write(`let ${o};`);
e.write(`if (${r} !== undefined) {`);
e.indented((e) => {
const n = generateCheck(e, t, i.innerType, r);
e.write(`${o} = ${n};`);
});
e.write(`}`);
}
return o;
}
function generateNonOptionalCheck(e, t, n, r) {
const i = n._zod.def;
const a = generateCheck(e, t, i.innerType, r);
const o = newVar(t);
e.write(`const ${o} = ${a};`);
e.write(`if (${o} === undefined) return INVALID;`);
return o;
}
function generateTupleCheck(e, t, n, r) {
const i = n._zod.def;
const a = i.items;
const o = i.rest;
e.write(`if (!Array.isArray(${r})) return INVALID;`);
const s = getTupleOptStart(a, "optin");
const c = getTupleOptStart(a, "optout");
if (o) e.write(`if (${r}.length < ${s}) return INVALID;`);
else e.write(`if (${r}.length < ${s} || ${r}.length > ${a.length}) return INVALID;`);
const l = newVar(t);
e.write(`const ${l} = [];`);
for (let n = 0; n < a.length; n++) {
const i = a[n];
if (n >= c) {
e.write(`if (${l}.length === ${n}) {`);
e.indented((e) => {
e.write(`if (${n} < ${r}.length) {`);
e.indented((e) => {
const a = newVar(t);
e.write(`const ${a} = ${r}[${n}];`);
const o = compileChild(e, t, i, a);
e.write(`${l}[${n}] = ${o};`);
});
e.write(`} else {`);
e.indented((e) => {
if (dropsWhenAbsent(i)) {
e.write(`${l}.length = ${n};`);
return;
}
const r = newVar(t);
const a = newVar(t);
e.write(`const ${r} = undefined;`);
e.write(`const ${a} = (() => {`);
e.indented((e) => {
const n = compileChild(e, t, i, r);
e.write(`return ${n};`);
});
e.write(`})();`);
e.write(`if (${a} === INVALID || ${a} === undefined) ${l}.length = ${n};`);
e.write(`else ${l}[${n}] = ${a};`);
});
e.write(`}`);
});
e.write(`}`);
} else {
const a = newVar(t);
e.write(`const ${a} = ${r}[${n}];`);
const o = compileChild(e, t, i, a);
e.write(`${l}[${n}] = ${o};`);
}
}
if (o) {
const n = newVar(t);
const i = newVar(t);
e.write(`for (let ${n} = ${a.length}; ${n} < ${r}.length; ${n}++) {`);
e.indented((e) => {
e.write(`const ${i} = ${r}[${n}];`);
const a = compileChild(e, t, o, i);
e.write(`${l}[${n}] = ${a};`);
});
e.write(`}`);
}
return l;
}
function getTupleOptStart(e, t) {
for (let n = e.length - 1; n >= 0; n--) if (!(t === "optin" ? e[n]._zod.optin !== void 0 : e[n]._zod.optout === "optional")) return n + 1;
return 0;
}
function generateUnionCheck(e, t, n, r) {
const i = n._zod.def;
const a = i.options;
if (i.discriminator) return generateDiscriminatedUnionCheck(e, t, i, r);
if (i.inclusive === false) throw new ZodCompileUnsupportedError("exclusive unions (z.xor)");
if (a.length === 0) {
e.write("return INVALID;");
return r;
}
if (a.length === 1) return generateCheck(e, t, a[0], r);
if (a.every((e) => e._zod.def.type === "literal" && !e._zod.def.checks?.length)) {
const n = addConstant(t, new Set(a.flatMap((e) => e._zod.def.values)));
e.write(`if (!${n}.has(${r})) return INVALID;`);
return r;
}
const o = newVar(t);
e.write(`let ${o};`);
for (let n = 0; n < a.length; n++) {
const i = a[n];
if (n === 0) e.write(`${o} = (() => {`);
else e.write(`if (${o} === INVALID) ${o} = (() => {`);
e.indented((e) => {
const n = generateCheck(e, t, i, r);
e.write(`return ${n};`);
});
e.write(`})();`);
}
e.write(`if (${o} === INVALID) return INVALID;`);
return o;
}
function generateDiscriminatedUnionCheck(e, t, n, r) {
if (n.unionFallback) throw new ZodCompileUnsupportedError("discriminated union with unionFallback");
if (n.options.length === 0) {
e.write("return INVALID;");
return r;
}
const i = newVar(t);
const a = newVar(t);
e.write(`const ${i} = ${r}?.[${esc(n.discriminator)}];`);
e.write(`let ${a};`);
let o = true;
const s = /* @__PURE__ */ new Set();
for (const c of n.options) {
const l = c._zod.propValues?.[n.discriminator];
if (!l || l.size === 0) throw new ZodCompileUnsupportedError("discriminated union option without static discriminator values");
for (const e of l) {
if (s.has(e)) throw new ZodCompileUnsupportedError(`duplicate discriminator value ${String(e)}`);
s.add(e);
}
const u = Array.from(l, (e) => literalEquality(t, i, e));
const d = o ? "if" : "else if";
e.write(`${d} (${u.join(" || ")}) {`);
e.indented((e) => {
const n = generateCheck(e, t, c, r);
e.write(`${a} = ${n};`);
});
e.write(`}`);
o = false;
}
e.write(`else { return INVALID; }`);
return a;
}
function literalEquality(e, t, n) {
if (typeof n === "string") return `${t} === ${esc(n)}`;
if (typeof n === "number") {
if (Number.isNaN(n)) return `Number.isNaN(${t})`;
return `${t} === ${n}`;
}
if (typeof n === "boolean") return `${t} === ${n}`;
if (n === null) return `${t} === null`;
if (n === void 0) return `${t} === undefined`;
if (typeof n === "bigint") return `${t} === ${n}n`;
if (typeof n === "symbol") return `${t} === ${addConstant(e, n)}`;
throw new ZodCompileUnsupportedError(`literal discriminator value ${String(n)}`);
}
function generateIntersectionCheck(e, t, n, r) {
const i = n._zod.def;
t.definite = false;
const a = compileChild(e, t, i.left, r);
const o = compileChild(e, t, i.right, r);
const s = addConstant(t, mergeValues);
const c = newVar(t);
e.write(`const ${c} = ${s}(${a}, ${o});`);
e.write(`if (!${c}.valid) return INVALID;`);
return `${c}.data`;
}
function generateRecordCheck(e, t, n, r) {
const i = n._zod.def;
const a = addConstant(t, isPlainObject);
e.write(`if (!${a}(${r})) return INVALID;`);
const o = newVar(t);
const s = newVar(t);
const c = newVar(t);
e.write(`const ${o} = {};`);
const l = i;
const u = l.partial ? void 0 : i.keyType._zod.values;
if (u) {
const n = [];
for (const a of u) {
if (!(typeof a === "string" || typeof a === "number" || typeof a === "symbol")) throw new ZodCompileUnsupportedError(`record key value ${String(a)}`);
const s = typeof a === "number" ? a.toString() : a;
if (s === "__proto__") throw new ZodCompileUnsupportedError("record key \"__proto__\"");
n.push(s);
const c = addConstant(t, a);
const l = generateCheck(e, t, i.keyType, c);
const u = newVar(t);
e.write(`const ${u} = ${r}[${literalPropertyKey(t, s)}];`);
const d = compileChild(e, t, i.valueType, u);
e.write(`${o}[${l}] = ${d};`);
}
const a = addConstant(t, new Set(n));
e.write(`for (const ${s} in ${r}) {`);
e.indented((e) => {
e.write(`if (${a}.has(${s})) continue;`);
if (l.mode === "loose") e.write(`if (${s} !== "__proto__") ${o}[${s}] = ${r}[${s}];`);
else e.write(`return INVALID;`);
});
e.write(`}`);
return o;
}
const d = i.keyType._zod.def;
if (!(d.type === "string" && d.format === void 0 && !d.coerce && (d.checks?.length ?? 0) === 0)) {
const n = i.mode === "loose";
const a = compileFn(i.keyType);
if (a.definite === false) t.definite = false;
const c = addConstant(t, a);
const l = addConstant(t, number);
const u = newVar(t);
emitOwnKeys(e, t, r, s, (e) => {
e.write(`let ${u} = ${c}(${s});`);
e.write(`if (${u} === INVALID && typeof ${s} === "string" && ${l}.test(${s})) ${u} = ${c}(Number(${s}));`);
if (n) e.write(`if (${u} === INVALID) { ${o}[${s}] = ${r}[${s}]; continue; }`);
else e.write(`if (${u} === INVALID) return INVALID;`);
e.write(`if (${u} === "__proto__") continue;`);
const a = newVar(t);
e.write(`const ${a} = ${r}[${s}];`);
const d = compileChild(e, t, i.valueType, a);
e.write(`${o}[${u}] = ${d};`);
});
return o;
}
emitOwnKeys(e, t, r, s, (e) => {
e.write(`const ${c} = ${r}[${s}];`);
const n = compileChild(e, t, i.valueType, c);
e.write(`${o}[${s}] = ${n};`);
}, `return INVALID;`);
return o;
}
function emitOwnKeys(e, t, n, r, i, a) {
const o = addConstant(t, Object.prototype.propertyIsEnumerable);
const s = newVar(t);
const c = newVar(t);
const l = newVar(t);
e.write(`const ${s} = Object.getOwnPropertySymbols(${n});`);
e.write(`const ${c} = Object.getOwnPropertyNames(${n});`);
e.write(`for (let ${l} = 0; ${l} < ${c}.length; ${l}++) {`);
e.indented((e) => {
e.write(`const ${r} = ${c}[${l}];`);
e.write(`if (${r} === "__proto__" || !${o}.call(${n}, ${r})) continue;`);
i(e);
});
e.write(`}`);
e.write(`for (let ${l} = 0; ${l} < ${s}.length; ${l}++) {`);
e.indented((e) => {
e.write(`const ${r} = ${s}[${l}];`);
e.write(`if (!${o}.call(${n}, ${r})) continue;`);
if (a) e.write(a);
else i(e);
});
e.write(`}`);
}
function literalPropertyKey(e, t) {
if (typeof t === "string") return esc(t);
return addConstant(e, t);
}
function generateMapCheck(e, t, n, r) {
const i = n._zod.def;
e.write(`if (!(${r} instanceof Map)) return INVALID;`);
generateSizeGate(e, n, `${r}.size`);
const a = newVar(t);
const o = newVar(t);
const s = newVar(t);
e.write(`const ${a} = new Map();`);
e.write(`for (const [${o}, ${s}] of ${r}) {`);
e.indented((e) => {
const n = generateCheck(e, t, i.keyType, o);
const r = generateCheck(e, t, i.valueType, s);
e.write(`${a}.set(${n}, ${r});`);
});
e.write(`}`);
return a;
}
function generateSetCheck(e, t, n, r) {
const i = n._zod.def;
e.write(`if (!(${r} instanceof Set)) return INVALID;`);
generateSizeGate(e, n, `${r}.size`);
const a = newVar(t);
const o = newVar(t);
e.write(`const ${a} = new Set();`);
e.write(`for (const ${o} of ${r}) {`);
e.indented((e) => {
const n = generateCheck(e, t, i.valueType, o);
e.write(`${a}.add(${n});`);
});
e.write(`}`);
return a;
}
function generateFileCheck(e, t) {
e.write(`if (!(${t} instanceof File)) return INVALID;`);
return t;
}
function generateTemplateLiteralCheck(e, t, n, r) {
e.write(`if (typeof ${r} !== "string") return INVALID;`);
const i = n._zod.pattern;
if (i) {
const n = addConstant(t, i);
e.write(`${n}.lastIndex = 0;`);
e.write(`if (!${n}.test(${r})) return INVALID;`);
}
return r;
}
function generateLazyCheck(e, t, n, r) {
const i = n._zod.def;
const a = addUserConstant(t, i.getter);
const o = addConstant(t, { parser: null });
e.write(`if (!${o}.parser) {`);
e.indented((e) => {
e.write(`const inner = ${a}();`);
e.write(`${o}.parser = function(input) {`);
e.indented((e) => {
e.write(`const result = inner._zod.run({ value: input, issues: [] }, {});`);
e.write(`return result.issues.length === 0 ? result.value : INVALID;`);
});
e.write(`};`);
});
e.write(`}`);
const s = newVar(t);
e.write(`const ${s} = ${o}.parser(${r});`);
e.write(`if (${s} === INVALID) return INVALID;`);
return s;
}
function generatePipeCheck(e, t, n, r) {
const i = n._zod.def;
const a = generateCheck(e, t, i.in, r);
if (i.transform) {
if (isAsyncFunction(i.transform)) throw new ZodCompileAsyncError("z.compile: async transforms in pipes are not supported");
const n = i.transform;
const helperFn = (e) => {
const t = {
value: e,
issues: [],
addIssue: pushIssue
};
const r = n(e, t);
if (r instanceof Promise) return INVALID;
return t.issues.length === 0 ? r : INVALID;
};
const r = addUserConstant(t, helperFn);
const o = newVar(t);
e.write(`const ${o} = ${r}(${a});`);
e.write(`if (${o} === INVALID) return INVALID;`);
return generateCheck(e, t, i.out, o);
} else return generateCheck(e, t, i.out, a);
}
function isAsyncFunction(e) {
return typeof e === "function" && (e.constructor.name === "AsyncFunction" || e[Symbol.toStringTag] === "AsyncFunction");
}
function generateCustomCheck(e, t, n, r) {
const i = n._zod.def;
if (i.fn) {
if (isAsyncFunction(i.fn)) throw new ZodCompileAsyncError("z.compile: async custom predicates are not supported");
const n = addUserConstant(t, i.fn);
const a = addConstant(t, throwAsync);
const o = newVar(t);
e.write(`const ${o} = ${n}(${r});`);
e.write(`if (${o} instanceof Promise) ${a}();`);
e.write(`if (!${o}) return INVALID;`);
} else throw new ZodCompileUnsupportedError("custom schema without a predicate function");
return r;
}
function runtimeCatch(e, t, n) {
const r = e._zod.run({
value: n,
issues: []
}, {});
if (r && typeof r.then === "function") return INVALID;
const i = r;
if (i.issues.length === 0) return i.value;
return t();
}
function generateCatchCheck(e, t, n, r) {
const i = n._zod.def;
if (!i.catchValue["~constantCatch"]) throw new ZodCompileUnsupportedError("catch with a callback (only a constant catch value compiles)", false);
const a = newVar(t);
e.write(`let ${a} = (() => {`);
e.indented((e) => {
const n = compileChild(e, t, i.innerType, r);
e.write(`return ${n};`);
});
e.write(`})();`);
const o = addConstant(t, i.innerType);
const s = addUserConstant(t, i.catchValue);
const c = addConstant(t, runtimeCatch);
e.write(`if (${a} === INVALID) {`);
e.indented((e) => {
e.write(`${a} = ${c}(${o}, ${s}, ${r});`);
e.write(`if (${a} === INVALID) return INVALID;`);
});
e.write(`}`);
return a;
}
function generateTransformCheck(e, t, n, r) {
const i = n._zod.def;
if (i.transform) {
if (isAsyncFunction(i.transform)) throw new ZodCompileAsyncError("z.compile: async transforms are not supported");
const n = i.transform;
const helperFn = (e) => {
const t = {
value: e,
issues: [],
addIssue: pushIssue
};
const r = n(e, t);
if (r instanceof Promise) return INVALID;
return t.issues.length === 0 ? r : INVALID;
};
const a = addUserConstant(t, helperFn);
const o = newVar(t);
e.write(`const ${o} = ${a}(${r});`);
e.write(`if (${o} === INVALID) return INVALID;`);
return o;
}
return r;
}
//#endregion
export { compileFn as a, compile as i, ZodCompileAsyncError as n, withParser as o, ZodCompileUnsupportedError as r, INVALID as t };