@brainervirus/workit-pi 8.22.1 → 8.22.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/worker.js +0 -1639
- package/dist/workit.js +68 -1663
- package/package.json +1 -1
package/dist/workit.js
CHANGED
|
@@ -963,8 +963,6 @@ var base64 = /^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/
|
|
|
963
963
|
var base64url = /^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2,3})?$/;
|
|
964
964
|
var httpProtocol = /^https?$/;
|
|
965
965
|
var e164 = /^\+[1-9]\d{6,14}$/;
|
|
966
|
-
var creditCard = /^\d(?:[ -]?\d){11,18}$/;
|
|
967
|
-
var iban = /^[A-Z]{2}(?!00|01|99)\d{2}[A-Z0-9]{11,30}$/;
|
|
968
966
|
var dateSource = `(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))`;
|
|
969
967
|
function anchor(source) {
|
|
970
968
|
return new RegExp(`^${source}$`);
|
|
@@ -1810,41 +1808,6 @@ var $ZodE164 = /* @__PURE__ */ $constructor("$ZodE164", (inst, def) => {
|
|
|
1810
1808
|
def.pattern ?? (def.pattern = e164);
|
|
1811
1809
|
$ZodStringFormat.init(inst, def);
|
|
1812
1810
|
});
|
|
1813
|
-
var CC_SANITIZE = /[- ]/g;
|
|
1814
|
-
function isLuhnAlgo(digits) {
|
|
1815
|
-
let length = digits.length;
|
|
1816
|
-
let bit = 1;
|
|
1817
|
-
let sum = 0;
|
|
1818
|
-
while (length) {
|
|
1819
|
-
const value = digits.charCodeAt(--length) - 48;
|
|
1820
|
-
bit ^= 1;
|
|
1821
|
-
sum += bit ? [0, 2, 4, 6, 8, 1, 3, 5, 7, 9][value] : value;
|
|
1822
|
-
}
|
|
1823
|
-
return sum % 10 === 0;
|
|
1824
|
-
}
|
|
1825
|
-
function isValidCreditCard(input) {
|
|
1826
|
-
if (!creditCard.test(input))
|
|
1827
|
-
return false;
|
|
1828
|
-
return isLuhnAlgo(input.replace(CC_SANITIZE, ""));
|
|
1829
|
-
}
|
|
1830
|
-
function isIso7064Mod97(iban2) {
|
|
1831
|
-
let remainder = 0;
|
|
1832
|
-
const len = iban2.length;
|
|
1833
|
-
for (let i = 4;i < len; i++) {
|
|
1834
|
-
const code = iban2.charCodeAt(i);
|
|
1835
|
-
remainder = (code >= 65 ? remainder * 100 + (code - 55) : remainder * 10 + (code - 48)) % 97;
|
|
1836
|
-
}
|
|
1837
|
-
for (let i = 0;i < 4; i++) {
|
|
1838
|
-
const code = iban2.charCodeAt(i);
|
|
1839
|
-
remainder = (code >= 65 ? remainder * 100 + (code - 55) : remainder * 10 + (code - 48)) % 97;
|
|
1840
|
-
}
|
|
1841
|
-
return remainder === 1;
|
|
1842
|
-
}
|
|
1843
|
-
function isValidIBAN(input) {
|
|
1844
|
-
if (!iban.test(input))
|
|
1845
|
-
return false;
|
|
1846
|
-
return isIso7064Mod97(input);
|
|
1847
|
-
}
|
|
1848
1811
|
function isValidJWT(token, algorithm = null) {
|
|
1849
1812
|
try {
|
|
1850
1813
|
const tokensParts = token.split(".");
|
|
@@ -3157,9 +3120,6 @@ function settle(inst, answer) {
|
|
|
3157
3120
|
recursive.set(inst, answer === PROVEN);
|
|
3158
3121
|
return answer;
|
|
3159
3122
|
}
|
|
3160
|
-
function isRecursiveSchema(inst) {
|
|
3161
|
-
return isRecursive(inst, new Set, true) !== NONE;
|
|
3162
|
-
}
|
|
3163
3123
|
function bucketFor(state, inst) {
|
|
3164
3124
|
let bucket = state.buckets.get(inst);
|
|
3165
3125
|
if (!bucket) {
|
|
@@ -3446,1604 +3406,6 @@ function registry() {
|
|
|
3446
3406
|
}
|
|
3447
3407
|
(_a2 = globalThis).__zod_globalRegistry ?? (_a2.__zod_globalRegistry = registry());
|
|
3448
3408
|
var globalRegistry = globalThis.__zod_globalRegistry;
|
|
3449
|
-
// node_modules/zod/v4/core/compile.js
|
|
3450
|
-
var INVALID = Symbol.for("zod.compile.invalid");
|
|
3451
|
-
var FALLBACK_FLAG = Symbol.for("zod.compile.fallback");
|
|
3452
|
-
|
|
3453
|
-
class ZodCompileAsyncError extends Error {
|
|
3454
|
-
constructor(message = "z.compile does not support async refinements, transforms, or checks") {
|
|
3455
|
-
super(message);
|
|
3456
|
-
this.name = "ZodCompileAsyncError";
|
|
3457
|
-
}
|
|
3458
|
-
}
|
|
3459
|
-
|
|
3460
|
-
class ZodCompileUnsupportedError extends Error {
|
|
3461
|
-
constructor(feature, islandable = true) {
|
|
3462
|
-
super(`z.compile does not support ${feature}; this schema must use the runtime parser`);
|
|
3463
|
-
this.name = "ZodCompileUnsupportedError";
|
|
3464
|
-
this.islandable = islandable;
|
|
3465
|
-
}
|
|
3466
|
-
}
|
|
3467
|
-
function compileValidator(schema, parser) {
|
|
3468
|
-
try {
|
|
3469
|
-
return compileFn(schema, { assertOnly: true });
|
|
3470
|
-
} catch {
|
|
3471
|
-
return parser;
|
|
3472
|
-
}
|
|
3473
|
-
}
|
|
3474
|
-
function compile(schema, options) {
|
|
3475
|
-
try {
|
|
3476
|
-
const parser = compileFn(schema);
|
|
3477
|
-
const clone = withParser(schema, parser);
|
|
3478
|
-
clone._zod.bag.validator = compileValidator(schema, parser);
|
|
3479
|
-
return clone;
|
|
3480
|
-
} catch (err) {
|
|
3481
|
-
if (options?.strict)
|
|
3482
|
-
throw err;
|
|
3483
|
-
return schema;
|
|
3484
|
-
}
|
|
3485
|
-
}
|
|
3486
|
-
function withParser(schema, parser) {
|
|
3487
|
-
if (isRecursiveSchema(schema)) {
|
|
3488
|
-
throw new ZodCompileUnsupportedError("a schema whose subtree contains a reference cycle");
|
|
3489
|
-
}
|
|
3490
|
-
const clone2 = clone(schema);
|
|
3491
|
-
const liveRun = schema._zod.run;
|
|
3492
|
-
const originalRun = liveRun.__originalRun ?? liveRun;
|
|
3493
|
-
const wrapped = (payload, ctx) => {
|
|
3494
|
-
if (ctx?.async || ctx?.direction === "backward" || ctx?.skipChecks || ctx?.[FALLBACK_FLAG]) {
|
|
3495
|
-
return originalRun(payload, ctx);
|
|
3496
|
-
}
|
|
3497
|
-
if (ctx && isBackEdge(ctx, payload.value)) {
|
|
3498
|
-
return originalRun(payload, ctx);
|
|
3499
|
-
}
|
|
3500
|
-
const out = parser(payload.value);
|
|
3501
|
-
if (out !== INVALID) {
|
|
3502
|
-
payload.value = out;
|
|
3503
|
-
return payload;
|
|
3504
|
-
}
|
|
3505
|
-
if (ctx)
|
|
3506
|
-
ctx[FALLBACK_FLAG] = true;
|
|
3507
|
-
return originalRun(payload, ctx);
|
|
3508
|
-
};
|
|
3509
|
-
wrapped.__originalRun = originalRun;
|
|
3510
|
-
clone2._zod.bag.fallbackRun = originalRun;
|
|
3511
|
-
clone2._zod.bag.validator = parser;
|
|
3512
|
-
clone2._zod.run = wrapped;
|
|
3513
|
-
if (!liveRun.__originalRun)
|
|
3514
|
-
installCompiledUserMethods(clone2, schema, parser);
|
|
3515
|
-
return clone2;
|
|
3516
|
-
}
|
|
3517
|
-
function installCompiledUserMethods(target, source, parser) {
|
|
3518
|
-
const targetAny = target;
|
|
3519
|
-
const sourceAny = source;
|
|
3520
|
-
if (typeof sourceAny.safeParse === "function") {
|
|
3521
|
-
const originalSafeParse = sourceAny.safeParse;
|
|
3522
|
-
targetAny.safeParse = (data, params) => {
|
|
3523
|
-
const out = parser(data);
|
|
3524
|
-
if (out !== INVALID) {
|
|
3525
|
-
return { success: true, data: out };
|
|
3526
|
-
}
|
|
3527
|
-
return originalSafeParse(data, params);
|
|
3528
|
-
};
|
|
3529
|
-
}
|
|
3530
|
-
if (typeof sourceAny.parse === "function") {
|
|
3531
|
-
const originalParse = sourceAny.parse;
|
|
3532
|
-
targetAny.parse = (data, params) => {
|
|
3533
|
-
const out = parser(data);
|
|
3534
|
-
if (out !== INVALID) {
|
|
3535
|
-
return out;
|
|
3536
|
-
}
|
|
3537
|
-
return originalParse(data, params);
|
|
3538
|
-
};
|
|
3539
|
-
}
|
|
3540
|
-
}
|
|
3541
|
-
function compileFn(schema, options) {
|
|
3542
|
-
let recursive = true;
|
|
3543
|
-
try {
|
|
3544
|
-
recursive = isRecursiveSchema(schema);
|
|
3545
|
-
} catch {}
|
|
3546
|
-
if (recursive) {
|
|
3547
|
-
throw new ZodCompileUnsupportedError("a schema whose subtree contains a reference cycle");
|
|
3548
|
-
}
|
|
3549
|
-
const ctx = {
|
|
3550
|
-
constants: new Map,
|
|
3551
|
-
constantCounter: 0,
|
|
3552
|
-
varCounter: 0,
|
|
3553
|
-
definite: true
|
|
3554
|
-
};
|
|
3555
|
-
const doc = new Doc(["input"]);
|
|
3556
|
-
const outputAccessor = generateCheck(doc, ctx, schema, "input", !options?.assertOnly);
|
|
3557
|
-
doc.write(outputAccessor === null ? `return true;` : `return ${outputAccessor};`);
|
|
3558
|
-
const constantNames = ["INVALID", ...ctx.constants.keys()];
|
|
3559
|
-
const constantValues = [INVALID, ...ctx.constants.values()];
|
|
3560
|
-
const code = doc.content.join(`
|
|
3561
|
-
`);
|
|
3562
|
-
const fullCode = options?.debug ? constantNames.length > 0 ? `// Constants: ${constantNames.join(", ")}
|
|
3563
|
-
${code}` : code : "";
|
|
3564
|
-
const F = Function;
|
|
3565
|
-
const factoryCode = `return (input) => {
|
|
3566
|
-
${code}
|
|
3567
|
-
}`;
|
|
3568
|
-
let fn;
|
|
3569
|
-
try {
|
|
3570
|
-
const factory = new F(...constantNames, factoryCode);
|
|
3571
|
-
fn = factory(...constantValues);
|
|
3572
|
-
} catch (err) {
|
|
3573
|
-
throw new ZodCompileUnsupportedError(`this schema (generated code failed to evaluate: ${err.message})`);
|
|
3574
|
-
}
|
|
3575
|
-
if (options?.debug) {
|
|
3576
|
-
fn.code = fullCode;
|
|
3577
|
-
}
|
|
3578
|
-
fn.definite = ctx.definite;
|
|
3579
|
-
return fn;
|
|
3580
|
-
}
|
|
3581
|
-
function addConstant(ctx, value) {
|
|
3582
|
-
for (const [name, v] of ctx.constants) {
|
|
3583
|
-
if (v === value)
|
|
3584
|
-
return name;
|
|
3585
|
-
}
|
|
3586
|
-
const name = `c${ctx.constantCounter++}`;
|
|
3587
|
-
ctx.constants.set(name, value);
|
|
3588
|
-
return name;
|
|
3589
|
-
}
|
|
3590
|
-
function addUserConstant(ctx, fn) {
|
|
3591
|
-
ctx.definite = false;
|
|
3592
|
-
return addConstant(ctx, fn);
|
|
3593
|
-
}
|
|
3594
|
-
function newVar(ctx) {
|
|
3595
|
-
return `v${ctx.varCounter++}`;
|
|
3596
|
-
}
|
|
3597
|
-
function runtimeRun(schema, value) {
|
|
3598
|
-
const result = schema._zod.run({ value, issues: [] }, {});
|
|
3599
|
-
if (result && typeof result.then === "function")
|
|
3600
|
-
return INVALID;
|
|
3601
|
-
const r = result;
|
|
3602
|
-
return r.issues.length === 0 ? r.value : INVALID;
|
|
3603
|
-
}
|
|
3604
|
-
function compileChild(doc, ctx, schema, accessor, needsValue = true) {
|
|
3605
|
-
const contentLen = doc.content.length;
|
|
3606
|
-
const constantCount = ctx.constants.size;
|
|
3607
|
-
const constantCounter = ctx.constantCounter;
|
|
3608
|
-
const varCounter = ctx.varCounter;
|
|
3609
|
-
try {
|
|
3610
|
-
return generateCheck(doc, ctx, schema, accessor, needsValue);
|
|
3611
|
-
} catch (err) {
|
|
3612
|
-
if (!(err instanceof ZodCompileUnsupportedError) || !err.islandable)
|
|
3613
|
-
throw err;
|
|
3614
|
-
doc.content.length = contentLen;
|
|
3615
|
-
if (ctx.constants.size > constantCount) {
|
|
3616
|
-
const trailing = Array.from(ctx.constants.keys()).slice(constantCount);
|
|
3617
|
-
for (const k of trailing)
|
|
3618
|
-
ctx.constants.delete(k);
|
|
3619
|
-
}
|
|
3620
|
-
ctx.constantCounter = constantCounter;
|
|
3621
|
-
ctx.varCounter = varCounter;
|
|
3622
|
-
return emitRuntimeIsland(doc, ctx, schema, accessor);
|
|
3623
|
-
}
|
|
3624
|
-
}
|
|
3625
|
-
function emitRuntimeIsland(doc, ctx, schema, accessor) {
|
|
3626
|
-
ctx.definite = false;
|
|
3627
|
-
const schemaConst = addConstant(ctx, schema);
|
|
3628
|
-
const runConst = addConstant(ctx, runtimeRun);
|
|
3629
|
-
const outVar = newVar(ctx);
|
|
3630
|
-
doc.write(`const ${outVar} = ${runConst}(${schemaConst}, ${accessor});`);
|
|
3631
|
-
doc.write(`if (${outVar} === INVALID) return INVALID;`);
|
|
3632
|
-
return outVar;
|
|
3633
|
-
}
|
|
3634
|
-
var WHEN_DEFAULTED_CHECKS = new Set([
|
|
3635
|
-
"max_size",
|
|
3636
|
-
"min_size",
|
|
3637
|
-
"size_equals",
|
|
3638
|
-
"max_length",
|
|
3639
|
-
"min_length",
|
|
3640
|
-
"length_equals"
|
|
3641
|
-
]);
|
|
3642
|
-
function generateChecks(doc, ctx, schema, accessor) {
|
|
3643
|
-
const schemaChecks = schema._zod.def.checks;
|
|
3644
|
-
if (!schemaChecks || schemaChecks.length === 0)
|
|
3645
|
-
return accessor;
|
|
3646
|
-
let currentAccessor = accessor;
|
|
3647
|
-
for (const check of schemaChecks) {
|
|
3648
|
-
const def = check._zod.def;
|
|
3649
|
-
if (def.when && !WHEN_DEFAULTED_CHECKS.has(def.check)) {
|
|
3650
|
-
throw new ZodCompileUnsupportedError(`check with a custom "when" condition`);
|
|
3651
|
-
}
|
|
3652
|
-
switch (def.check) {
|
|
3653
|
-
case "greater_than":
|
|
3654
|
-
generateGreaterThanCheck(doc, ctx, def, currentAccessor);
|
|
3655
|
-
break;
|
|
3656
|
-
case "less_than":
|
|
3657
|
-
generateLessThanCheck(doc, ctx, def, currentAccessor);
|
|
3658
|
-
break;
|
|
3659
|
-
case "multiple_of":
|
|
3660
|
-
generateMultipleOfCheck(doc, ctx, def, currentAccessor);
|
|
3661
|
-
break;
|
|
3662
|
-
case "number_format":
|
|
3663
|
-
generateNumberFormatCheck(doc, def, currentAccessor);
|
|
3664
|
-
break;
|
|
3665
|
-
case "min_length": {
|
|
3666
|
-
const min = numericOperand(def.minimum, "min_length");
|
|
3667
|
-
const len = codePointLengthVar(doc, ctx, currentAccessor, `${currentAccessor}.length >= ${min} && ${currentAccessor}.length < ${def.minimum * 2}`);
|
|
3668
|
-
doc.write(`if (${len} < ${min}) return INVALID;`);
|
|
3669
|
-
break;
|
|
3670
|
-
}
|
|
3671
|
-
case "max_length": {
|
|
3672
|
-
const max = numericOperand(def.maximum, "max_length");
|
|
3673
|
-
const len = codePointLengthVar(doc, ctx, currentAccessor, `${currentAccessor}.length > ${max}`);
|
|
3674
|
-
doc.write(`if (${len} > ${max}) return INVALID;`);
|
|
3675
|
-
break;
|
|
3676
|
-
}
|
|
3677
|
-
case "length_equals": {
|
|
3678
|
-
const exact = numericOperand(def.length, "length_equals");
|
|
3679
|
-
const len = codePointLengthVar(doc, ctx, currentAccessor, `${currentAccessor}.length >= ${exact} && ${currentAccessor}.length <= ${def.length * 2}`);
|
|
3680
|
-
doc.write(`if (${len} !== ${exact}) return INVALID;`);
|
|
3681
|
-
break;
|
|
3682
|
-
}
|
|
3683
|
-
case "min_size":
|
|
3684
|
-
doc.write(`if (${currentAccessor}.size < ${numericOperand(def.minimum, "min_size")}) return INVALID;`);
|
|
3685
|
-
break;
|
|
3686
|
-
case "max_size":
|
|
3687
|
-
doc.write(`if (${currentAccessor}.size > ${numericOperand(def.maximum, "max_size")}) return INVALID;`);
|
|
3688
|
-
break;
|
|
3689
|
-
case "size_equals":
|
|
3690
|
-
doc.write(`if (${currentAccessor}.size !== ${numericOperand(def.size, "size_equals")}) return INVALID;`);
|
|
3691
|
-
break;
|
|
3692
|
-
case "string_format":
|
|
3693
|
-
currentAccessor = generateStringFormatCheck(doc, ctx, def, currentAccessor);
|
|
3694
|
-
break;
|
|
3695
|
-
case "custom":
|
|
3696
|
-
currentAccessor = generateCustomRefineCheck(doc, ctx, check, currentAccessor);
|
|
3697
|
-
break;
|
|
3698
|
-
case "bigint_format":
|
|
3699
|
-
generateBigIntFormatCheck(doc, def, currentAccessor);
|
|
3700
|
-
break;
|
|
3701
|
-
case "mime_type":
|
|
3702
|
-
generateMimeTypeCheck(doc, ctx, def, currentAccessor);
|
|
3703
|
-
break;
|
|
3704
|
-
case "property":
|
|
3705
|
-
generatePropertyCheck(doc, ctx, def, currentAccessor);
|
|
3706
|
-
break;
|
|
3707
|
-
case "properties":
|
|
3708
|
-
generatePropertiesChecks(doc, ctx, def, currentAccessor);
|
|
3709
|
-
break;
|
|
3710
|
-
case "overwrite": {
|
|
3711
|
-
const newAccessor = newVar(ctx);
|
|
3712
|
-
generateOverwriteCheck(doc, ctx, check, currentAccessor, newAccessor);
|
|
3713
|
-
currentAccessor = newAccessor;
|
|
3714
|
-
break;
|
|
3715
|
-
}
|
|
3716
|
-
default: {
|
|
3717
|
-
throw new ZodCompileUnsupportedError(`check type ${def.check}`);
|
|
3718
|
-
}
|
|
3719
|
-
}
|
|
3720
|
-
}
|
|
3721
|
-
return currentAccessor;
|
|
3722
|
-
}
|
|
3723
|
-
function codePointLengthVar(doc, ctx, accessor, inDoubt) {
|
|
3724
|
-
const cpLen = addConstant(ctx, codePointLength);
|
|
3725
|
-
const v = newVar(ctx);
|
|
3726
|
-
doc.write(`const ${v} = typeof ${accessor} === "string" && ${inDoubt} ? ${cpLen}(${accessor}) : ${accessor}.length;`);
|
|
3727
|
-
return v;
|
|
3728
|
-
}
|
|
3729
|
-
function numericOperand(value, label) {
|
|
3730
|
-
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
3731
|
-
throw new ZodCompileUnsupportedError(`${label} bound of type ${typeof value}`);
|
|
3732
|
-
}
|
|
3733
|
-
return `${value}`;
|
|
3734
|
-
}
|
|
3735
|
-
function comparisonOperand(ctx, value) {
|
|
3736
|
-
if (typeof value === "bigint")
|
|
3737
|
-
return `${value}n`;
|
|
3738
|
-
if (typeof value === "number") {
|
|
3739
|
-
if (Number.isNaN(value))
|
|
3740
|
-
throw new ZodCompileUnsupportedError("comparison check with NaN bound");
|
|
3741
|
-
return `${value}`;
|
|
3742
|
-
}
|
|
3743
|
-
if (value instanceof Date) {
|
|
3744
|
-
if (Number.isNaN(value.getTime())) {
|
|
3745
|
-
throw new ZodCompileUnsupportedError("comparison check with Invalid Date bound");
|
|
3746
|
-
}
|
|
3747
|
-
return addConstant(ctx, value);
|
|
3748
|
-
}
|
|
3749
|
-
throw new ZodCompileUnsupportedError(`comparison check bound of type ${typeof value}`);
|
|
3750
|
-
}
|
|
3751
|
-
function generateGreaterThanCheck(doc, ctx, def, accessor) {
|
|
3752
|
-
const op = def.inclusive ? "<" : "<=";
|
|
3753
|
-
doc.write(`if (${accessor} ${op} ${comparisonOperand(ctx, def.value)}) return INVALID;`);
|
|
3754
|
-
}
|
|
3755
|
-
function generateLessThanCheck(doc, ctx, def, accessor) {
|
|
3756
|
-
const op = def.inclusive ? ">" : ">=";
|
|
3757
|
-
doc.write(`if (${accessor} ${op} ${comparisonOperand(ctx, def.value)}) return INVALID;`);
|
|
3758
|
-
}
|
|
3759
|
-
function generateMultipleOfCheck(doc, ctx, def, accessor) {
|
|
3760
|
-
if (typeof def.value === "bigint") {
|
|
3761
|
-
if (def.value === BigInt(0))
|
|
3762
|
-
throw new ZodCompileUnsupportedError("multiple_of check with a zero divisor");
|
|
3763
|
-
doc.write(`if (${accessor} % ${def.value}n !== 0n) return INVALID;`);
|
|
3764
|
-
} else {
|
|
3765
|
-
const remainder = addConstant(ctx, floatSafeRemainder);
|
|
3766
|
-
doc.write(`if (${remainder}(${accessor}, ${numericOperand(def.value, "multiple_of")}) !== 0) return INVALID;`);
|
|
3767
|
-
}
|
|
3768
|
-
}
|
|
3769
|
-
function generateNumberFormatCheck(doc, def, accessor) {
|
|
3770
|
-
const format = def.format;
|
|
3771
|
-
switch (format) {
|
|
3772
|
-
case "safeint":
|
|
3773
|
-
doc.write(`if (!Number.isSafeInteger(${accessor})) return INVALID;`);
|
|
3774
|
-
break;
|
|
3775
|
-
case "int32":
|
|
3776
|
-
doc.write(`if (!Number.isInteger(${accessor}) || ${accessor} < -2147483648 || ${accessor} > 2147483647) return INVALID;`);
|
|
3777
|
-
break;
|
|
3778
|
-
case "uint32":
|
|
3779
|
-
doc.write(`if (!Number.isInteger(${accessor}) || ${accessor} < 0 || ${accessor} > 4294967295) return INVALID;`);
|
|
3780
|
-
break;
|
|
3781
|
-
case "float32":
|
|
3782
|
-
doc.write(`if (!Number.isFinite(${accessor}) || ${accessor} < -3.4028234663852886e38 || ${accessor} > 3.4028234663852886e38) return INVALID;`);
|
|
3783
|
-
break;
|
|
3784
|
-
case "float64":
|
|
3785
|
-
doc.write(`if (!Number.isFinite(${accessor})) return INVALID;`);
|
|
3786
|
-
break;
|
|
3787
|
-
default: {
|
|
3788
|
-
throw new ZodCompileUnsupportedError(`number format ${format}`);
|
|
3789
|
-
}
|
|
3790
|
-
}
|
|
3791
|
-
}
|
|
3792
|
-
function generateBigIntFormatCheck(doc, def, accessor) {
|
|
3793
|
-
const format = def.format;
|
|
3794
|
-
if (!format)
|
|
3795
|
-
return;
|
|
3796
|
-
switch (format) {
|
|
3797
|
-
case "int64":
|
|
3798
|
-
doc.write(`if (${accessor} < -9223372036854775808n || ${accessor} > 9223372036854775807n) return INVALID;`);
|
|
3799
|
-
break;
|
|
3800
|
-
case "uint64":
|
|
3801
|
-
doc.write(`if (${accessor} < 0n || ${accessor} > 18446744073709551615n) return INVALID;`);
|
|
3802
|
-
break;
|
|
3803
|
-
default: {
|
|
3804
|
-
throw new ZodCompileUnsupportedError(`bigint format ${format}`);
|
|
3805
|
-
}
|
|
3806
|
-
}
|
|
3807
|
-
}
|
|
3808
|
-
function generateMimeTypeCheck(doc, ctx, def, accessor) {
|
|
3809
|
-
const mimeTypes = def.mime;
|
|
3810
|
-
if (mimeTypes && mimeTypes.length > 0) {
|
|
3811
|
-
const mimeSet = addConstant(ctx, new Set(mimeTypes));
|
|
3812
|
-
doc.write(`if (!${mimeSet}.has(${accessor}.type)) return INVALID;`);
|
|
3813
|
-
}
|
|
3814
|
-
}
|
|
3815
|
-
function generatePropertiesChecks(doc, ctx, def, accessor) {
|
|
3816
|
-
if (def.when) {
|
|
3817
|
-
throw new ZodCompileUnsupportedError(`check with a custom "when" condition`);
|
|
3818
|
-
}
|
|
3819
|
-
doc.write(`if (${accessor} == null) return INVALID;`);
|
|
3820
|
-
const shape = def.shape;
|
|
3821
|
-
for (const key of Reflect.ownKeys(shape)) {
|
|
3822
|
-
const keyExpr = typeof key === "symbol" ? addConstant(ctx, key) : esc(key);
|
|
3823
|
-
const inputVar = newVar(ctx);
|
|
3824
|
-
doc.write(`const ${inputVar} = ${accessor}[${keyExpr}];`);
|
|
3825
|
-
compileChild(doc, ctx, shape[key], inputVar, false);
|
|
3826
|
-
}
|
|
3827
|
-
}
|
|
3828
|
-
function generatePropertyCheck(doc, ctx, def, accessor) {
|
|
3829
|
-
const propAccessor = `${accessor}[${JSON.stringify(def.property)}]`;
|
|
3830
|
-
generateCheck(doc, ctx, def.schema, propAccessor);
|
|
3831
|
-
}
|
|
3832
|
-
function generateOverwriteCheck(doc, ctx, check, currentAccessor, newAccessor) {
|
|
3833
|
-
const tx = check._zod.def.tx;
|
|
3834
|
-
if (!tx) {
|
|
3835
|
-
throw new ZodCompileUnsupportedError("overwrite check without a transform function");
|
|
3836
|
-
}
|
|
3837
|
-
if (isAsyncFunction(tx)) {
|
|
3838
|
-
throw new ZodCompileAsyncError("z.compile: async overwrite transforms are not supported");
|
|
3839
|
-
}
|
|
3840
|
-
const txConst = addConstant(ctx, tx);
|
|
3841
|
-
doc.write(`const ${newAccessor} = ${txConst}(${currentAccessor});`);
|
|
3842
|
-
}
|
|
3843
|
-
function throwAsync() {
|
|
3844
|
-
throw new $ZodAsyncError;
|
|
3845
|
-
}
|
|
3846
|
-
function pushIssue(issue) {
|
|
3847
|
-
this.issues.push(issue);
|
|
3848
|
-
}
|
|
3849
|
-
function generateCustomRefineCheck(doc, ctx, check, accessor) {
|
|
3850
|
-
const def = check._zod.def;
|
|
3851
|
-
if (def.fn) {
|
|
3852
|
-
if (isAsyncFunction(def.fn)) {
|
|
3853
|
-
throw new ZodCompileAsyncError("z.compile: async .refine() predicates are not supported");
|
|
3854
|
-
}
|
|
3855
|
-
const fnConst = addUserConstant(ctx, def.fn);
|
|
3856
|
-
const throwAsyncConst = addConstant(ctx, throwAsync);
|
|
3857
|
-
const resVar = newVar(ctx);
|
|
3858
|
-
doc.write(`const ${resVar} = ${fnConst}(${accessor});`);
|
|
3859
|
-
doc.write(`if (${resVar} instanceof Promise) ${throwAsyncConst}();`);
|
|
3860
|
-
doc.write(`if (!${resVar}) return INVALID;`);
|
|
3861
|
-
return accessor;
|
|
3862
|
-
}
|
|
3863
|
-
if (check._zod.check) {
|
|
3864
|
-
if (isAsyncFunction(check._zod.check)) {
|
|
3865
|
-
throw new ZodCompileAsyncError("z.compile: async .superRefine() / check functions are not supported");
|
|
3866
|
-
}
|
|
3867
|
-
const checkFn = check._zod.check;
|
|
3868
|
-
const helperFn = (value) => {
|
|
3869
|
-
const fakePayload = { value, issues: [], addIssue: pushIssue };
|
|
3870
|
-
const result = checkFn(fakePayload);
|
|
3871
|
-
if (result instanceof Promise)
|
|
3872
|
-
throwAsync();
|
|
3873
|
-
return fakePayload.issues.length === 0 ? fakePayload.value : INVALID;
|
|
3874
|
-
};
|
|
3875
|
-
const helperConst = addUserConstant(ctx, helperFn);
|
|
3876
|
-
const outVar = newVar(ctx);
|
|
3877
|
-
doc.write(`const ${outVar} = ${helperConst}(${accessor});`);
|
|
3878
|
-
doc.write(`if (${outVar} === INVALID) return INVALID;`);
|
|
3879
|
-
return outVar;
|
|
3880
|
-
}
|
|
3881
|
-
throw new ZodCompileUnsupportedError("custom check without a predicate or check function");
|
|
3882
|
-
}
|
|
3883
|
-
var PATTERN_IS_COMPLETE = new Set([
|
|
3884
|
-
"cidrv4",
|
|
3885
|
-
"cuid",
|
|
3886
|
-
"cuid2",
|
|
3887
|
-
"date",
|
|
3888
|
-
"datetime",
|
|
3889
|
-
"duration",
|
|
3890
|
-
"e164",
|
|
3891
|
-
"email",
|
|
3892
|
-
"emoji",
|
|
3893
|
-
"ends_with",
|
|
3894
|
-
"guid",
|
|
3895
|
-
"includes",
|
|
3896
|
-
"ipv4",
|
|
3897
|
-
"ksuid",
|
|
3898
|
-
"lowercase",
|
|
3899
|
-
"mac",
|
|
3900
|
-
"nanoid",
|
|
3901
|
-
"regex",
|
|
3902
|
-
"starts_with",
|
|
3903
|
-
"time",
|
|
3904
|
-
"ulid",
|
|
3905
|
-
"uppercase",
|
|
3906
|
-
"uuid",
|
|
3907
|
-
"xid"
|
|
3908
|
-
]);
|
|
3909
|
-
function generateStringFormatCheck(doc, ctx, def, accessor, needsValue = true) {
|
|
3910
|
-
const fmt = def.format;
|
|
3911
|
-
if (fmt === "base64") {
|
|
3912
|
-
const validator = addConstant(ctx, isValidBase64);
|
|
3913
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
3914
|
-
return accessor;
|
|
3915
|
-
}
|
|
3916
|
-
if (fmt === "base64url") {
|
|
3917
|
-
const validator = addConstant(ctx, isValidBase64URL);
|
|
3918
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
3919
|
-
return accessor;
|
|
3920
|
-
}
|
|
3921
|
-
if (fmt === "jwt") {
|
|
3922
|
-
const validator = addConstant(ctx, isValidJWT);
|
|
3923
|
-
const alg = addConstant(ctx, def.alg ?? null);
|
|
3924
|
-
doc.write(`if (!${validator}(${accessor}, ${alg})) return INVALID;`);
|
|
3925
|
-
return accessor;
|
|
3926
|
-
}
|
|
3927
|
-
if (fmt === "ipv6") {
|
|
3928
|
-
const validator = addConstant(ctx, isValidIPv6);
|
|
3929
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
3930
|
-
return accessor;
|
|
3931
|
-
}
|
|
3932
|
-
if (fmt === "cidrv6") {
|
|
3933
|
-
const validator = addConstant(ctx, isValidCIDRv6);
|
|
3934
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
3935
|
-
return accessor;
|
|
3936
|
-
}
|
|
3937
|
-
if (fmt === "credit_card") {
|
|
3938
|
-
const validator = addConstant(ctx, isValidCreditCard);
|
|
3939
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
3940
|
-
return accessor;
|
|
3941
|
-
}
|
|
3942
|
-
if (fmt === "iban") {
|
|
3943
|
-
const validator = addConstant(ctx, isValidIBAN);
|
|
3944
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
3945
|
-
return accessor;
|
|
3946
|
-
}
|
|
3947
|
-
const formatDef = def;
|
|
3948
|
-
if (fmt === "url" || fmt === "httpurl" || formatDef.normalize || formatDef.hostname !== undefined || formatDef.protocol !== undefined) {
|
|
3949
|
-
const parseConst = addConstant(ctx, validateURL);
|
|
3950
|
-
const defConst = addConstant(ctx, def);
|
|
3951
|
-
const trimVar = newVar(ctx);
|
|
3952
|
-
const urlVar = newVar(ctx);
|
|
3953
|
-
doc.write(`const ${trimVar} = ${accessor}.trim();`);
|
|
3954
|
-
doc.write(`const ${urlVar} = ${parseConst}(${trimVar}, ${defConst});`);
|
|
3955
|
-
doc.write(`if (typeof ${urlVar} === "number") return INVALID;`);
|
|
3956
|
-
if (formatDef.hostname !== undefined) {
|
|
3957
|
-
const hostnameConst = addConstant(ctx, urlHostnameOk);
|
|
3958
|
-
doc.write(`if (!${hostnameConst}(${urlVar}, ${defConst}.hostname)) return INVALID;`);
|
|
3959
|
-
}
|
|
3960
|
-
if (formatDef.protocol !== undefined) {
|
|
3961
|
-
const protocolConst = addConstant(ctx, urlProtocolOk);
|
|
3962
|
-
doc.write(`if (!${protocolConst}(${urlVar}, ${defConst}.protocol)) return INVALID;`);
|
|
3963
|
-
}
|
|
3964
|
-
if (!needsValue)
|
|
3965
|
-
return null;
|
|
3966
|
-
const outputVar = newVar(ctx);
|
|
3967
|
-
const outputExpr = formatDef.normalize ? `${urlVar}.href` : `${addConstant(ctx, stripTabAndNewline)}(${trimVar})`;
|
|
3968
|
-
doc.write(`const ${outputVar} = ${outputExpr};`);
|
|
3969
|
-
return outputVar;
|
|
3970
|
-
}
|
|
3971
|
-
const customFn = def.fn;
|
|
3972
|
-
if (customFn) {
|
|
3973
|
-
if (isAsyncFunction(customFn))
|
|
3974
|
-
throw new ZodCompileUnsupportedError(`async string format ${fmt}`);
|
|
3975
|
-
const fnConst = addConstant(ctx, customFn);
|
|
3976
|
-
doc.write(`if (!${fnConst}(${accessor})) return INVALID;`);
|
|
3977
|
-
return accessor;
|
|
3978
|
-
}
|
|
3979
|
-
if (PATTERN_IS_COMPLETE.has(fmt) && def.pattern) {
|
|
3980
|
-
const patternConst = addConstant(ctx, def.pattern);
|
|
3981
|
-
doc.write(`${patternConst}.lastIndex = 0;`);
|
|
3982
|
-
doc.write(`if (!${patternConst}.test(${accessor})) return INVALID;`);
|
|
3983
|
-
return accessor;
|
|
3984
|
-
}
|
|
3985
|
-
const format = def.format;
|
|
3986
|
-
switch (format) {
|
|
3987
|
-
case "regex":
|
|
3988
|
-
throw new ZodCompileUnsupportedError("regex format without a pattern");
|
|
3989
|
-
case "lowercase":
|
|
3990
|
-
doc.write(`if (${accessor} !== ${accessor}.toLowerCase()) return INVALID;`);
|
|
3991
|
-
break;
|
|
3992
|
-
case "uppercase":
|
|
3993
|
-
doc.write(`if (${accessor} !== ${accessor}.toUpperCase()) return INVALID;`);
|
|
3994
|
-
break;
|
|
3995
|
-
case "includes":
|
|
3996
|
-
doc.write(`if (!${accessor}.includes(${esc(def.includes)})) return INVALID;`);
|
|
3997
|
-
break;
|
|
3998
|
-
case "starts_with": {
|
|
3999
|
-
const prefix = def.prefix;
|
|
4000
|
-
doc.write(`if (${accessor}.slice(0, ${prefix.length}) !== ${esc(prefix)}) return INVALID;`);
|
|
4001
|
-
break;
|
|
4002
|
-
}
|
|
4003
|
-
case "ends_with": {
|
|
4004
|
-
const suffix = def.suffix;
|
|
4005
|
-
doc.write(`if (${accessor}.slice(-${suffix.length}) !== ${esc(suffix)}) return INVALID;`);
|
|
4006
|
-
break;
|
|
4007
|
-
}
|
|
4008
|
-
default: {
|
|
4009
|
-
throw new ZodCompileUnsupportedError(`string format ${format}`);
|
|
4010
|
-
}
|
|
4011
|
-
}
|
|
4012
|
-
return accessor;
|
|
4013
|
-
}
|
|
4014
|
-
function generateCheck(doc, ctx, schema, accessor, needsValue = true) {
|
|
4015
|
-
const def = schema._zod.def;
|
|
4016
|
-
const type = def.type;
|
|
4017
|
-
if (def.coerce) {
|
|
4018
|
-
throw new ZodCompileUnsupportedError(`coercion (z.coerce.${type}())`);
|
|
4019
|
-
}
|
|
4020
|
-
const buildsValue = needsValue || !!def.checks?.length;
|
|
4021
|
-
let typeAccessor;
|
|
4022
|
-
switch (type) {
|
|
4023
|
-
case "string":
|
|
4024
|
-
typeAccessor = generateStringCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4025
|
-
break;
|
|
4026
|
-
case "number":
|
|
4027
|
-
typeAccessor = generateNumberCheck(doc, schema, accessor);
|
|
4028
|
-
break;
|
|
4029
|
-
case "boolean":
|
|
4030
|
-
typeAccessor = generateBooleanCheck(doc, accessor);
|
|
4031
|
-
break;
|
|
4032
|
-
case "bigint":
|
|
4033
|
-
typeAccessor = generateBigIntCheck(doc, schema, accessor);
|
|
4034
|
-
break;
|
|
4035
|
-
case "symbol":
|
|
4036
|
-
typeAccessor = generateSymbolCheck(doc, accessor);
|
|
4037
|
-
break;
|
|
4038
|
-
case "undefined":
|
|
4039
|
-
typeAccessor = generateUndefinedCheck(doc, accessor);
|
|
4040
|
-
break;
|
|
4041
|
-
case "null":
|
|
4042
|
-
typeAccessor = generateNullCheck(doc, accessor);
|
|
4043
|
-
break;
|
|
4044
|
-
case "any":
|
|
4045
|
-
case "unknown":
|
|
4046
|
-
typeAccessor = accessor;
|
|
4047
|
-
break;
|
|
4048
|
-
case "never":
|
|
4049
|
-
doc.write("return INVALID;");
|
|
4050
|
-
typeAccessor = accessor;
|
|
4051
|
-
break;
|
|
4052
|
-
case "void":
|
|
4053
|
-
typeAccessor = generateVoidCheck(doc, accessor);
|
|
4054
|
-
break;
|
|
4055
|
-
case "nan":
|
|
4056
|
-
typeAccessor = generateNaNCheck(doc, accessor);
|
|
4057
|
-
break;
|
|
4058
|
-
case "date":
|
|
4059
|
-
typeAccessor = generateDateCheck(doc, accessor);
|
|
4060
|
-
break;
|
|
4061
|
-
case "object":
|
|
4062
|
-
typeAccessor = generateObjectCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4063
|
-
break;
|
|
4064
|
-
case "optional":
|
|
4065
|
-
typeAccessor = generateOptionalCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4066
|
-
break;
|
|
4067
|
-
case "nullable":
|
|
4068
|
-
typeAccessor = generateNullableCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4069
|
-
break;
|
|
4070
|
-
case "array":
|
|
4071
|
-
typeAccessor = generateArrayCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4072
|
-
break;
|
|
4073
|
-
case "literal":
|
|
4074
|
-
typeAccessor = generateLiteralCheck(doc, ctx, schema, accessor);
|
|
4075
|
-
break;
|
|
4076
|
-
case "enum":
|
|
4077
|
-
typeAccessor = generateEnumCheck(doc, ctx, schema, accessor);
|
|
4078
|
-
break;
|
|
4079
|
-
case "readonly": {
|
|
4080
|
-
const innerOut = generateWrapperCheck(doc, ctx, schema, accessor);
|
|
4081
|
-
const frozenVar = newVar(ctx);
|
|
4082
|
-
doc.write(`const ${frozenVar} = Object.freeze(${innerOut});`);
|
|
4083
|
-
typeAccessor = frozenVar;
|
|
4084
|
-
break;
|
|
4085
|
-
}
|
|
4086
|
-
case "success":
|
|
4087
|
-
generateWrapperCheck(doc, ctx, schema, accessor);
|
|
4088
|
-
typeAccessor = "true";
|
|
4089
|
-
break;
|
|
4090
|
-
case "default":
|
|
4091
|
-
case "prefault":
|
|
4092
|
-
typeAccessor = generateDefaultCheck(doc, ctx, schema, accessor);
|
|
4093
|
-
break;
|
|
4094
|
-
case "nonoptional":
|
|
4095
|
-
typeAccessor = generateNonOptionalCheck(doc, ctx, schema, accessor);
|
|
4096
|
-
break;
|
|
4097
|
-
case "tuple":
|
|
4098
|
-
typeAccessor = generateTupleCheck(doc, ctx, schema, accessor);
|
|
4099
|
-
break;
|
|
4100
|
-
case "union":
|
|
4101
|
-
typeAccessor = generateUnionCheck(doc, ctx, schema, accessor);
|
|
4102
|
-
break;
|
|
4103
|
-
case "intersection":
|
|
4104
|
-
typeAccessor = generateIntersectionCheck(doc, ctx, schema, accessor);
|
|
4105
|
-
break;
|
|
4106
|
-
case "record":
|
|
4107
|
-
typeAccessor = generateRecordCheck(doc, ctx, schema, accessor);
|
|
4108
|
-
break;
|
|
4109
|
-
case "map":
|
|
4110
|
-
typeAccessor = generateMapCheck(doc, ctx, schema, accessor);
|
|
4111
|
-
break;
|
|
4112
|
-
case "set":
|
|
4113
|
-
typeAccessor = generateSetCheck(doc, ctx, schema, accessor);
|
|
4114
|
-
break;
|
|
4115
|
-
case "file":
|
|
4116
|
-
typeAccessor = generateFileCheck(doc, accessor);
|
|
4117
|
-
break;
|
|
4118
|
-
case "template_literal":
|
|
4119
|
-
typeAccessor = generateTemplateLiteralCheck(doc, ctx, schema, accessor);
|
|
4120
|
-
break;
|
|
4121
|
-
case "lazy":
|
|
4122
|
-
typeAccessor = generateLazyCheck(doc, ctx, schema, accessor);
|
|
4123
|
-
break;
|
|
4124
|
-
case "pipe":
|
|
4125
|
-
typeAccessor = generatePipeCheck(doc, ctx, schema, accessor);
|
|
4126
|
-
break;
|
|
4127
|
-
case "custom":
|
|
4128
|
-
typeAccessor = generateCustomCheck(doc, ctx, schema, accessor);
|
|
4129
|
-
break;
|
|
4130
|
-
case "transform":
|
|
4131
|
-
typeAccessor = generateTransformCheck(doc, ctx, schema, accessor);
|
|
4132
|
-
break;
|
|
4133
|
-
case "catch":
|
|
4134
|
-
typeAccessor = generateCatchCheck(doc, ctx, schema, accessor);
|
|
4135
|
-
break;
|
|
4136
|
-
default: {
|
|
4137
|
-
throw new ZodCompileUnsupportedError(`schema type ${type}`);
|
|
4138
|
-
}
|
|
4139
|
-
}
|
|
4140
|
-
if (typeAccessor === null)
|
|
4141
|
-
return null;
|
|
4142
|
-
return generateChecks(doc, ctx, schema, typeAccessor);
|
|
4143
|
-
}
|
|
4144
|
-
function generateStringCheck(doc, ctx, schema, accessor, needsValue = true) {
|
|
4145
|
-
doc.write(`if (typeof ${accessor} !== "string") return INVALID;`);
|
|
4146
|
-
const def = schema._zod.def;
|
|
4147
|
-
if (def.format === undefined)
|
|
4148
|
-
return accessor;
|
|
4149
|
-
return generateStringFormatCheck(doc, ctx, def, accessor, needsValue);
|
|
4150
|
-
}
|
|
4151
|
-
function generateNumberCheck(doc, schema, accessor) {
|
|
4152
|
-
doc.write(`if (typeof ${accessor} !== "number" || !Number.isFinite(${accessor})) return INVALID;`);
|
|
4153
|
-
const def = schema._zod.def;
|
|
4154
|
-
if (def.check === "number_format" && def.format) {
|
|
4155
|
-
generateNumberFormatCheck(doc, { format: def.format }, accessor);
|
|
4156
|
-
}
|
|
4157
|
-
return accessor;
|
|
4158
|
-
}
|
|
4159
|
-
function generateBooleanCheck(doc, accessor) {
|
|
4160
|
-
doc.write(`if (typeof ${accessor} !== "boolean") return INVALID;`);
|
|
4161
|
-
return accessor;
|
|
4162
|
-
}
|
|
4163
|
-
function generateBigIntCheck(doc, schema, accessor) {
|
|
4164
|
-
doc.write(`if (typeof ${accessor} !== "bigint") return INVALID;`);
|
|
4165
|
-
const def = schema._zod.def;
|
|
4166
|
-
if (def.format) {
|
|
4167
|
-
switch (def.format) {
|
|
4168
|
-
case "int64":
|
|
4169
|
-
doc.write(`if (${accessor} < -9223372036854775808n || ${accessor} > 9223372036854775807n) return INVALID;`);
|
|
4170
|
-
break;
|
|
4171
|
-
case "uint64":
|
|
4172
|
-
doc.write(`if (${accessor} < 0n || ${accessor} > 18446744073709551615n) return INVALID;`);
|
|
4173
|
-
break;
|
|
4174
|
-
}
|
|
4175
|
-
}
|
|
4176
|
-
return accessor;
|
|
4177
|
-
}
|
|
4178
|
-
function generateSymbolCheck(doc, accessor) {
|
|
4179
|
-
doc.write(`if (typeof ${accessor} !== "symbol") return INVALID;`);
|
|
4180
|
-
return accessor;
|
|
4181
|
-
}
|
|
4182
|
-
function generateUndefinedCheck(doc, accessor) {
|
|
4183
|
-
doc.write(`if (${accessor} !== undefined) return INVALID;`);
|
|
4184
|
-
return accessor;
|
|
4185
|
-
}
|
|
4186
|
-
function generateNullCheck(doc, accessor) {
|
|
4187
|
-
doc.write(`if (${accessor} !== null) return INVALID;`);
|
|
4188
|
-
return accessor;
|
|
4189
|
-
}
|
|
4190
|
-
function generateVoidCheck(doc, accessor) {
|
|
4191
|
-
doc.write(`if (${accessor} !== undefined) return INVALID;`);
|
|
4192
|
-
return accessor;
|
|
4193
|
-
}
|
|
4194
|
-
function generateNaNCheck(doc, accessor) {
|
|
4195
|
-
doc.write(`if (typeof ${accessor} !== "number" || !Number.isNaN(${accessor})) return INVALID;`);
|
|
4196
|
-
return accessor;
|
|
4197
|
-
}
|
|
4198
|
-
function generateDateCheck(doc, accessor) {
|
|
4199
|
-
doc.write(`if (!(${accessor} instanceof Date) || Number.isNaN(${accessor}.getTime())) return INVALID;`);
|
|
4200
|
-
return accessor;
|
|
4201
|
-
}
|
|
4202
|
-
function generateObjectCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
4203
|
-
const def = schema._zod.def;
|
|
4204
|
-
doc.write(`if (typeof ${accessor} !== "object" || ${accessor} === null || Array.isArray(${accessor})) return INVALID;`);
|
|
4205
|
-
const shape = def.shape;
|
|
4206
|
-
const keys = Object.keys(shape);
|
|
4207
|
-
const symbolKeys = Object.getOwnPropertySymbols(shape);
|
|
4208
|
-
const allKeys = symbolKeys.length ? [...keys, ...symbolKeys] : keys;
|
|
4209
|
-
const keyExpr = (k) => typeof k === "symbol" ? addConstant(ctx, k) : esc(k);
|
|
4210
|
-
const propKey = (k) => typeof k === "symbol" ? `[${keyExpr(k)}]` : esc(k);
|
|
4211
|
-
const propShape = shape;
|
|
4212
|
-
if (keys.includes("__proto__")) {
|
|
4213
|
-
throw new ZodCompileUnsupportedError('object shape key "__proto__"');
|
|
4214
|
-
}
|
|
4215
|
-
const propOutputs = new Map;
|
|
4216
|
-
for (const key of allKeys) {
|
|
4217
|
-
const propSchema = propShape[key];
|
|
4218
|
-
const kx = keyExpr(key);
|
|
4219
|
-
const inputVar = newVar(ctx);
|
|
4220
|
-
doc.write(`const ${inputVar} = ${accessor}[${kx}];`);
|
|
4221
|
-
if (propSchema._zod.optin !== undefined) {
|
|
4222
|
-
const outputVar = newVar(ctx);
|
|
4223
|
-
doc.write(`let ${outputVar} = (() => {`);
|
|
4224
|
-
doc.indented((d) => {
|
|
4225
|
-
const outputAccessor = compileChild(d, ctx, propSchema, inputVar);
|
|
4226
|
-
d.write(`return ${outputAccessor};`);
|
|
4227
|
-
});
|
|
4228
|
-
doc.write(`})();`);
|
|
4229
|
-
if (propSchema._zod.optout === "optional") {
|
|
4230
|
-
doc.write(`if (${outputVar} === INVALID) {`);
|
|
4231
|
-
doc.indented((d) => {
|
|
4232
|
-
d.write(`if (${kx} in ${accessor}) return INVALID;`);
|
|
4233
|
-
d.write(`${outputVar} = undefined;`);
|
|
4234
|
-
});
|
|
4235
|
-
doc.write(`}`);
|
|
4236
|
-
} else {
|
|
4237
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
4238
|
-
}
|
|
4239
|
-
propOutputs.set(key, outputVar);
|
|
4240
|
-
} else {
|
|
4241
|
-
if (requiresPresenceCheck(propSchema)) {
|
|
4242
|
-
doc.write(`if (!(${kx} in ${accessor})) return INVALID;`);
|
|
4243
|
-
}
|
|
4244
|
-
const outputAccessor = compileChild(doc, ctx, propSchema, inputVar, buildsValue);
|
|
4245
|
-
if (outputAccessor !== null)
|
|
4246
|
-
propOutputs.set(key, outputAccessor);
|
|
4247
|
-
}
|
|
4248
|
-
}
|
|
4249
|
-
const catchall = def.catchall;
|
|
4250
|
-
let unknownKeysMode = "none";
|
|
4251
|
-
if (catchall) {
|
|
4252
|
-
const catchallType = catchall._zod.def.type;
|
|
4253
|
-
if (catchallType === "never") {
|
|
4254
|
-
const condition = keys.map((k) => `k !== ${esc(k)}`).join(" && ") || "true";
|
|
4255
|
-
doc.write(`for (const k in ${accessor}) {`);
|
|
4256
|
-
doc.indented((d) => {
|
|
4257
|
-
d.write(`if (${condition}) return INVALID;`);
|
|
4258
|
-
});
|
|
4259
|
-
doc.write(`}`);
|
|
4260
|
-
} else if ((catchallType === "unknown" || catchallType === "any") && !catchall._zod.def.checks?.length) {
|
|
4261
|
-
unknownKeysMode = "passthrough";
|
|
4262
|
-
} else {
|
|
4263
|
-
unknownKeysMode = "schema";
|
|
4264
|
-
}
|
|
4265
|
-
}
|
|
4266
|
-
const outputVar = newVar(ctx);
|
|
4267
|
-
const hasConditionalKeys = allKeys.some((k) => mayOmitUndefined(propShape[k]) || dropsWhenAbsent(propShape[k]));
|
|
4268
|
-
if (!buildsValue) {
|
|
4269
|
-
if (unknownKeysMode === "schema") {
|
|
4270
|
-
const knownSet = keys.length > 0 ? addConstant(ctx, new Set(keys)) : null;
|
|
4271
|
-
doc.write(`for (const k in ${accessor}) {`);
|
|
4272
|
-
doc.indented((d) => {
|
|
4273
|
-
d.write(`if (k === "__proto__") continue;`);
|
|
4274
|
-
if (knownSet)
|
|
4275
|
-
d.write(`if (${knownSet}.has(k)) continue;`);
|
|
4276
|
-
const valVar = newVar(ctx);
|
|
4277
|
-
d.write(`const ${valVar} = ${accessor}[k];`);
|
|
4278
|
-
compileChild(d, ctx, catchall, valVar, false);
|
|
4279
|
-
});
|
|
4280
|
-
doc.write(`}`);
|
|
4281
|
-
}
|
|
4282
|
-
return null;
|
|
4283
|
-
}
|
|
4284
|
-
if (!hasConditionalKeys) {
|
|
4285
|
-
const propLiterals = allKeys.map((k) => `${propKey(k)}: ${propOutputs.get(k)}`).join(", ");
|
|
4286
|
-
doc.write(`const ${outputVar} = { ${propLiterals} };`);
|
|
4287
|
-
} else {
|
|
4288
|
-
doc.write(`const ${outputVar} = {};`);
|
|
4289
|
-
for (const k of allKeys) {
|
|
4290
|
-
const kx = keyExpr(k);
|
|
4291
|
-
const out = propOutputs.get(k);
|
|
4292
|
-
if (dropsWhenAbsent(propShape[k])) {
|
|
4293
|
-
doc.write(`if (${kx} in ${accessor}) ${outputVar}[${kx}] = ${out};`);
|
|
4294
|
-
} else if (mayOmitUndefined(propShape[k])) {
|
|
4295
|
-
doc.write(`if (${out} !== undefined || ${kx} in ${accessor}) ${outputVar}[${kx}] = ${out};`);
|
|
4296
|
-
} else {
|
|
4297
|
-
doc.write(`${outputVar}[${kx}] = ${out};`);
|
|
4298
|
-
}
|
|
4299
|
-
}
|
|
4300
|
-
}
|
|
4301
|
-
if (unknownKeysMode !== "none") {
|
|
4302
|
-
const knownSet = keys.length > 0 ? addConstant(ctx, new Set(keys)) : null;
|
|
4303
|
-
doc.write(`for (const k in ${accessor}) {`);
|
|
4304
|
-
doc.indented((d) => {
|
|
4305
|
-
d.write(`if (k === "__proto__") continue;`);
|
|
4306
|
-
if (knownSet)
|
|
4307
|
-
d.write(`if (${knownSet}.has(k)) continue;`);
|
|
4308
|
-
if (unknownKeysMode === "passthrough") {
|
|
4309
|
-
d.write(`${outputVar}[k] = ${accessor}[k];`);
|
|
4310
|
-
} else {
|
|
4311
|
-
const valVar = newVar(ctx);
|
|
4312
|
-
d.write(`const ${valVar} = ${accessor}[k];`);
|
|
4313
|
-
const catchallOut = compileChild(d, ctx, catchall, valVar);
|
|
4314
|
-
d.write(`${outputVar}[k] = ${catchallOut};`);
|
|
4315
|
-
}
|
|
4316
|
-
});
|
|
4317
|
-
doc.write(`}`);
|
|
4318
|
-
}
|
|
4319
|
-
return outputVar;
|
|
4320
|
-
}
|
|
4321
|
-
function generateOptionalCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
4322
|
-
const def = schema._zod.def;
|
|
4323
|
-
if (isExactOptional(schema)) {
|
|
4324
|
-
return generateCheck(doc, ctx, def.innerType, accessor, buildsValue);
|
|
4325
|
-
}
|
|
4326
|
-
if (def.innerType._zod.optin === "defaulted") {
|
|
4327
|
-
const outputVar = newVar(ctx);
|
|
4328
|
-
const branchVar = newVar(ctx);
|
|
4329
|
-
doc.write(`let ${outputVar};`);
|
|
4330
|
-
doc.write(`if (${accessor} === undefined) {`);
|
|
4331
|
-
doc.indented((d) => {
|
|
4332
|
-
d.write(`const ${branchVar} = (() => {`);
|
|
4333
|
-
d.indented((d2) => {
|
|
4334
|
-
const innerOutput = generateCheck(d2, ctx, def.innerType, accessor);
|
|
4335
|
-
d2.write(`return ${innerOutput};`);
|
|
4336
|
-
});
|
|
4337
|
-
d.write(`})();`);
|
|
4338
|
-
d.write(`if (${branchVar} !== INVALID) ${outputVar} = ${branchVar};`);
|
|
4339
|
-
});
|
|
4340
|
-
doc.write(`} else {`);
|
|
4341
|
-
doc.indented((d) => {
|
|
4342
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor);
|
|
4343
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
4344
|
-
});
|
|
4345
|
-
doc.write(`}`);
|
|
4346
|
-
return outputVar;
|
|
4347
|
-
}
|
|
4348
|
-
const outputVar = buildsValue ? newVar(ctx) : null;
|
|
4349
|
-
if (outputVar)
|
|
4350
|
-
doc.write(`let ${outputVar};`);
|
|
4351
|
-
doc.write(`if (${accessor} !== undefined) {`);
|
|
4352
|
-
doc.indented((d) => {
|
|
4353
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor, buildsValue);
|
|
4354
|
-
if (outputVar && innerOutput !== null)
|
|
4355
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
4356
|
-
});
|
|
4357
|
-
doc.write(`}`);
|
|
4358
|
-
return outputVar;
|
|
4359
|
-
}
|
|
4360
|
-
function isExactOptional(schema) {
|
|
4361
|
-
return schema._zod.traits?.has("$ZodExactOptional") === true;
|
|
4362
|
-
}
|
|
4363
|
-
function requiresPresenceCheck(schema) {
|
|
4364
|
-
return schema._zod.optin === undefined && fastPathAcceptsAbsence(schema);
|
|
4365
|
-
}
|
|
4366
|
-
function fastPathAcceptsAbsence(schema) {
|
|
4367
|
-
if (schema._zod.def.coerce)
|
|
4368
|
-
return true;
|
|
4369
|
-
const def = schema._zod.def;
|
|
4370
|
-
switch (def.type) {
|
|
4371
|
-
case "any":
|
|
4372
|
-
case "unknown":
|
|
4373
|
-
case "undefined":
|
|
4374
|
-
case "void":
|
|
4375
|
-
case "default":
|
|
4376
|
-
case "prefault":
|
|
4377
|
-
case "transform":
|
|
4378
|
-
case "custom":
|
|
4379
|
-
case "lazy":
|
|
4380
|
-
return true;
|
|
4381
|
-
case "string":
|
|
4382
|
-
case "number":
|
|
4383
|
-
case "boolean":
|
|
4384
|
-
case "bigint":
|
|
4385
|
-
case "symbol":
|
|
4386
|
-
case "null":
|
|
4387
|
-
case "never":
|
|
4388
|
-
case "nan":
|
|
4389
|
-
case "date":
|
|
4390
|
-
case "object":
|
|
4391
|
-
case "array":
|
|
4392
|
-
case "tuple":
|
|
4393
|
-
case "record":
|
|
4394
|
-
case "map":
|
|
4395
|
-
case "set":
|
|
4396
|
-
case "file":
|
|
4397
|
-
case "template_literal":
|
|
4398
|
-
return false;
|
|
4399
|
-
case "nonoptional":
|
|
4400
|
-
return def.innerType ? fastPathAcceptsAbsence(def.innerType) : false;
|
|
4401
|
-
case "literal":
|
|
4402
|
-
return !!def.values?.includes(undefined);
|
|
4403
|
-
case "enum":
|
|
4404
|
-
return !!schema._zod.values?.has(undefined);
|
|
4405
|
-
case "optional":
|
|
4406
|
-
case "nullable":
|
|
4407
|
-
case "readonly":
|
|
4408
|
-
case "success":
|
|
4409
|
-
return def.innerType ? fastPathAcceptsAbsence(def.innerType) : true;
|
|
4410
|
-
case "catch":
|
|
4411
|
-
return true;
|
|
4412
|
-
case "union":
|
|
4413
|
-
return def.options ? def.options.some(fastPathAcceptsAbsence) : true;
|
|
4414
|
-
case "intersection":
|
|
4415
|
-
if (!def.left || !def.right)
|
|
4416
|
-
return true;
|
|
4417
|
-
return fastPathAcceptsAbsence(def.left) && fastPathAcceptsAbsence(def.right);
|
|
4418
|
-
case "pipe":
|
|
4419
|
-
return def.in ? fastPathAcceptsAbsence(def.in) : true;
|
|
4420
|
-
default:
|
|
4421
|
-
return true;
|
|
4422
|
-
}
|
|
4423
|
-
}
|
|
4424
|
-
function dropsWhenAbsent(schema) {
|
|
4425
|
-
return schema._zod.optin === "optional" && schema._zod.optout === "optional";
|
|
4426
|
-
}
|
|
4427
|
-
function mayOmitUndefined(schema) {
|
|
4428
|
-
return (schema._zod.optin !== "defaulted" || schema._zod.optout === "optional") && mayOutputUndefined(schema);
|
|
4429
|
-
}
|
|
4430
|
-
function mayOutputUndefined(schema) {
|
|
4431
|
-
const def = schema._zod.def;
|
|
4432
|
-
switch (def.type) {
|
|
4433
|
-
case "string":
|
|
4434
|
-
case "number":
|
|
4435
|
-
case "boolean":
|
|
4436
|
-
case "bigint":
|
|
4437
|
-
case "symbol":
|
|
4438
|
-
case "null":
|
|
4439
|
-
case "nan":
|
|
4440
|
-
case "date":
|
|
4441
|
-
case "object":
|
|
4442
|
-
case "array":
|
|
4443
|
-
case "tuple":
|
|
4444
|
-
case "record":
|
|
4445
|
-
case "map":
|
|
4446
|
-
case "set":
|
|
4447
|
-
case "file":
|
|
4448
|
-
case "template_literal":
|
|
4449
|
-
case "never":
|
|
4450
|
-
case "success":
|
|
4451
|
-
return false;
|
|
4452
|
-
case "literal":
|
|
4453
|
-
return !!def.values?.includes(undefined);
|
|
4454
|
-
case "enum":
|
|
4455
|
-
return !!schema._zod.values?.has(undefined);
|
|
4456
|
-
case "optional":
|
|
4457
|
-
return true;
|
|
4458
|
-
case "nullable":
|
|
4459
|
-
case "readonly":
|
|
4460
|
-
case "nonoptional":
|
|
4461
|
-
return def.innerType ? mayOutputUndefined(def.innerType) : true;
|
|
4462
|
-
case "union":
|
|
4463
|
-
return def.options ? def.options.some(mayOutputUndefined) : true;
|
|
4464
|
-
case "intersection":
|
|
4465
|
-
return !def.left || !def.right || mayOutputUndefined(def.left) || mayOutputUndefined(def.right);
|
|
4466
|
-
case "pipe":
|
|
4467
|
-
return def.out ? mayOutputUndefined(def.out) : true;
|
|
4468
|
-
default:
|
|
4469
|
-
return true;
|
|
4470
|
-
}
|
|
4471
|
-
}
|
|
4472
|
-
function generateNullableCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
4473
|
-
const def = schema._zod.def;
|
|
4474
|
-
const outputVar = buildsValue ? newVar(ctx) : null;
|
|
4475
|
-
if (outputVar)
|
|
4476
|
-
doc.write(`let ${outputVar} = null;`);
|
|
4477
|
-
doc.write(`if (${accessor} !== null) {`);
|
|
4478
|
-
doc.indented((d) => {
|
|
4479
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor, buildsValue);
|
|
4480
|
-
if (outputVar && innerOutput !== null)
|
|
4481
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
4482
|
-
});
|
|
4483
|
-
doc.write(`}`);
|
|
4484
|
-
return outputVar;
|
|
4485
|
-
}
|
|
4486
|
-
function generateArrayCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
4487
|
-
const def = schema._zod.def;
|
|
4488
|
-
doc.write(`if (!Array.isArray(${accessor})) return INVALID;`);
|
|
4489
|
-
const outputVar = buildsValue ? newVar(ctx) : null;
|
|
4490
|
-
const iVar = newVar(ctx);
|
|
4491
|
-
const elemVar = newVar(ctx);
|
|
4492
|
-
if (outputVar)
|
|
4493
|
-
doc.write(`const ${outputVar} = new Array(${accessor}.length);`);
|
|
4494
|
-
doc.write(`for (let ${iVar} = 0; ${iVar} < ${accessor}.length; ${iVar}++) {`);
|
|
4495
|
-
doc.indented((d) => {
|
|
4496
|
-
d.write(`const ${elemVar} = ${accessor}[${iVar}];`);
|
|
4497
|
-
const elemOutput = compileChild(d, ctx, def.element, elemVar, buildsValue);
|
|
4498
|
-
if (outputVar && elemOutput !== null)
|
|
4499
|
-
d.write(`${outputVar}[${iVar}] = ${elemOutput};`);
|
|
4500
|
-
});
|
|
4501
|
-
doc.write(`}`);
|
|
4502
|
-
return outputVar;
|
|
4503
|
-
}
|
|
4504
|
-
function generateLiteralCheck(doc, ctx, schema, accessor) {
|
|
4505
|
-
const def = schema._zod.def;
|
|
4506
|
-
const values = def.values;
|
|
4507
|
-
if (values.length !== 1) {
|
|
4508
|
-
const literalSet = addConstant(ctx, new Set(values));
|
|
4509
|
-
doc.write(`if (!${literalSet}.has(${accessor})) return INVALID;`);
|
|
4510
|
-
return accessor;
|
|
4511
|
-
}
|
|
4512
|
-
const value = values[0];
|
|
4513
|
-
if (typeof value === "number" && Number.isNaN(value)) {
|
|
4514
|
-
const literalSet = addConstant(ctx, new Set(values));
|
|
4515
|
-
doc.write(`if (!${literalSet}.has(${accessor})) return INVALID;`);
|
|
4516
|
-
return accessor;
|
|
4517
|
-
}
|
|
4518
|
-
if (typeof value === "string") {
|
|
4519
|
-
doc.write(`if (${accessor} !== ${esc(value)}) return INVALID;`);
|
|
4520
|
-
} else if (typeof value === "number" || typeof value === "boolean") {
|
|
4521
|
-
doc.write(`if (${accessor} !== ${value}) return INVALID;`);
|
|
4522
|
-
} else if (value === null) {
|
|
4523
|
-
doc.write(`if (${accessor} !== null) return INVALID;`);
|
|
4524
|
-
} else if (value === undefined) {
|
|
4525
|
-
doc.write(`if (${accessor} !== undefined) return INVALID;`);
|
|
4526
|
-
} else if (typeof value === "bigint") {
|
|
4527
|
-
doc.write(`if (${accessor} !== ${value}n) return INVALID;`);
|
|
4528
|
-
} else {
|
|
4529
|
-
throw new ZodCompileUnsupportedError(`literal type ${typeof value}`);
|
|
4530
|
-
}
|
|
4531
|
-
return accessor;
|
|
4532
|
-
}
|
|
4533
|
-
function generateEnumCheck(doc, ctx, schema, accessor) {
|
|
4534
|
-
const values = schema._zod.values;
|
|
4535
|
-
if (!values) {
|
|
4536
|
-
throw new ZodCompileUnsupportedError("enum schema without enumerated values");
|
|
4537
|
-
}
|
|
4538
|
-
const enumSet = addConstant(ctx, values);
|
|
4539
|
-
doc.write(`if (!${enumSet}.has(${accessor})) return INVALID;`);
|
|
4540
|
-
return accessor;
|
|
4541
|
-
}
|
|
4542
|
-
function generateWrapperCheck(doc, ctx, schema, accessor) {
|
|
4543
|
-
const def = schema._zod.def;
|
|
4544
|
-
return generateCheck(doc, ctx, def.innerType, accessor);
|
|
4545
|
-
}
|
|
4546
|
-
function generateDefaultCheck(doc, ctx, schema, accessor) {
|
|
4547
|
-
const def = schema._zod.def;
|
|
4548
|
-
const descriptor = Object.getOwnPropertyDescriptor(schema._zod.def, "defaultValue");
|
|
4549
|
-
const defaultGetter = descriptor ? () => schema._zod.def.defaultValue : undefined;
|
|
4550
|
-
if (schema._zod.def.type === "prefault") {
|
|
4551
|
-
if (!defaultGetter) {
|
|
4552
|
-
return generateCheck(doc, ctx, def.innerType, accessor);
|
|
4553
|
-
}
|
|
4554
|
-
const defaultFn = addConstant(ctx, defaultGetter);
|
|
4555
|
-
const inputVar = newVar(ctx);
|
|
4556
|
-
doc.write(`let ${inputVar} = ${accessor};`);
|
|
4557
|
-
doc.write(`if (${accessor} === undefined) ${inputVar} = ${defaultFn}();`);
|
|
4558
|
-
return generateCheck(doc, ctx, def.innerType, inputVar);
|
|
4559
|
-
}
|
|
4560
|
-
const outputVar = newVar(ctx);
|
|
4561
|
-
if (defaultGetter) {
|
|
4562
|
-
const defaultFn = addConstant(ctx, defaultGetter);
|
|
4563
|
-
const cloneFn = addConstant(ctx, shallowClone);
|
|
4564
|
-
doc.write(`let ${outputVar};`);
|
|
4565
|
-
doc.write(`if (${accessor} === undefined) {`);
|
|
4566
|
-
doc.indented((d) => {
|
|
4567
|
-
d.write(`${outputVar} = ${cloneFn}(${defaultFn}());`);
|
|
4568
|
-
});
|
|
4569
|
-
doc.write(`} else {`);
|
|
4570
|
-
doc.indented((d) => {
|
|
4571
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor);
|
|
4572
|
-
d.write(`${outputVar} = ${innerOutput} === undefined ? ${cloneFn}(${defaultFn}()) : ${innerOutput};`);
|
|
4573
|
-
});
|
|
4574
|
-
doc.write(`}`);
|
|
4575
|
-
} else {
|
|
4576
|
-
doc.write(`let ${outputVar};`);
|
|
4577
|
-
doc.write(`if (${accessor} !== undefined) {`);
|
|
4578
|
-
doc.indented((d) => {
|
|
4579
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor);
|
|
4580
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
4581
|
-
});
|
|
4582
|
-
doc.write(`}`);
|
|
4583
|
-
}
|
|
4584
|
-
return outputVar;
|
|
4585
|
-
}
|
|
4586
|
-
function generateNonOptionalCheck(doc, ctx, schema, accessor) {
|
|
4587
|
-
const def = schema._zod.def;
|
|
4588
|
-
const innerOutput = generateCheck(doc, ctx, def.innerType, accessor);
|
|
4589
|
-
const outputVar = newVar(ctx);
|
|
4590
|
-
doc.write(`const ${outputVar} = ${innerOutput};`);
|
|
4591
|
-
doc.write(`if (${outputVar} === undefined) return INVALID;`);
|
|
4592
|
-
return outputVar;
|
|
4593
|
-
}
|
|
4594
|
-
function generateTupleCheck(doc, ctx, schema, accessor) {
|
|
4595
|
-
const def = schema._zod.def;
|
|
4596
|
-
const items = def.items;
|
|
4597
|
-
const rest = def.rest;
|
|
4598
|
-
doc.write(`if (!Array.isArray(${accessor})) return INVALID;`);
|
|
4599
|
-
const optinStart = getTupleOptStart(items, "optin");
|
|
4600
|
-
const optoutStart = getTupleOptStart(items, "optout");
|
|
4601
|
-
if (rest) {
|
|
4602
|
-
doc.write(`if (${accessor}.length < ${optinStart}) return INVALID;`);
|
|
4603
|
-
} else {
|
|
4604
|
-
doc.write(`if (${accessor}.length < ${optinStart} || ${accessor}.length > ${items.length}) return INVALID;`);
|
|
4605
|
-
}
|
|
4606
|
-
const outputVar = newVar(ctx);
|
|
4607
|
-
doc.write(`const ${outputVar} = [];`);
|
|
4608
|
-
for (let i = 0;i < items.length; i++) {
|
|
4609
|
-
const itemSchema = items[i];
|
|
4610
|
-
if (i >= optoutStart) {
|
|
4611
|
-
doc.write(`if (${outputVar}.length === ${i}) {`);
|
|
4612
|
-
doc.indented((d) => {
|
|
4613
|
-
d.write(`if (${i} < ${accessor}.length) {`);
|
|
4614
|
-
d.indented((d2) => {
|
|
4615
|
-
const elemVar = newVar(ctx);
|
|
4616
|
-
d2.write(`const ${elemVar} = ${accessor}[${i}];`);
|
|
4617
|
-
const elemOutput = compileChild(d2, ctx, itemSchema, elemVar);
|
|
4618
|
-
d2.write(`${outputVar}[${i}] = ${elemOutput};`);
|
|
4619
|
-
});
|
|
4620
|
-
d.write(`} else {`);
|
|
4621
|
-
d.indented((d2) => {
|
|
4622
|
-
if (dropsWhenAbsent(itemSchema)) {
|
|
4623
|
-
d2.write(`${outputVar}.length = ${i};`);
|
|
4624
|
-
return;
|
|
4625
|
-
}
|
|
4626
|
-
const elemVar = newVar(ctx);
|
|
4627
|
-
const branchVar = newVar(ctx);
|
|
4628
|
-
d2.write(`const ${elemVar} = undefined;`);
|
|
4629
|
-
d2.write(`const ${branchVar} = (() => {`);
|
|
4630
|
-
d2.indented((d3) => {
|
|
4631
|
-
const elemOutput = compileChild(d3, ctx, itemSchema, elemVar);
|
|
4632
|
-
d3.write(`return ${elemOutput};`);
|
|
4633
|
-
});
|
|
4634
|
-
d2.write(`})();`);
|
|
4635
|
-
d2.write(`if (${branchVar} === INVALID || ${branchVar} === undefined) ${outputVar}.length = ${i};`);
|
|
4636
|
-
d2.write(`else ${outputVar}[${i}] = ${branchVar};`);
|
|
4637
|
-
});
|
|
4638
|
-
d.write(`}`);
|
|
4639
|
-
});
|
|
4640
|
-
doc.write(`}`);
|
|
4641
|
-
} else {
|
|
4642
|
-
const elemVar = newVar(ctx);
|
|
4643
|
-
doc.write(`const ${elemVar} = ${accessor}[${i}];`);
|
|
4644
|
-
const elemOutput = compileChild(doc, ctx, itemSchema, elemVar);
|
|
4645
|
-
doc.write(`${outputVar}[${i}] = ${elemOutput};`);
|
|
4646
|
-
}
|
|
4647
|
-
}
|
|
4648
|
-
if (rest) {
|
|
4649
|
-
const iVar = newVar(ctx);
|
|
4650
|
-
const elemVar = newVar(ctx);
|
|
4651
|
-
doc.write(`for (let ${iVar} = ${items.length}; ${iVar} < ${accessor}.length; ${iVar}++) {`);
|
|
4652
|
-
doc.indented((d) => {
|
|
4653
|
-
d.write(`const ${elemVar} = ${accessor}[${iVar}];`);
|
|
4654
|
-
const elemOutput = compileChild(d, ctx, rest, elemVar);
|
|
4655
|
-
d.write(`${outputVar}[${iVar}] = ${elemOutput};`);
|
|
4656
|
-
});
|
|
4657
|
-
doc.write(`}`);
|
|
4658
|
-
}
|
|
4659
|
-
return outputVar;
|
|
4660
|
-
}
|
|
4661
|
-
function getTupleOptStart(items, key) {
|
|
4662
|
-
for (let i = items.length - 1;i >= 0; i--) {
|
|
4663
|
-
const omittable = key === "optin" ? items[i]._zod.optin !== undefined : items[i]._zod.optout === "optional";
|
|
4664
|
-
if (!omittable)
|
|
4665
|
-
return i + 1;
|
|
4666
|
-
}
|
|
4667
|
-
return 0;
|
|
4668
|
-
}
|
|
4669
|
-
function generateUnionCheck(doc, ctx, schema, accessor) {
|
|
4670
|
-
const def = schema._zod.def;
|
|
4671
|
-
const options = def.options;
|
|
4672
|
-
if (def.discriminator) {
|
|
4673
|
-
return generateDiscriminatedUnionCheck(doc, ctx, def, accessor);
|
|
4674
|
-
}
|
|
4675
|
-
if (def.inclusive === false) {
|
|
4676
|
-
throw new ZodCompileUnsupportedError("exclusive unions (z.xor)");
|
|
4677
|
-
}
|
|
4678
|
-
if (options.length === 0) {
|
|
4679
|
-
doc.write("return INVALID;");
|
|
4680
|
-
return accessor;
|
|
4681
|
-
}
|
|
4682
|
-
if (options.length === 1) {
|
|
4683
|
-
return generateCheck(doc, ctx, options[0], accessor);
|
|
4684
|
-
}
|
|
4685
|
-
const allLiterals = options.every((opt) => opt._zod.def.type === "literal" && !opt._zod.def.checks?.length);
|
|
4686
|
-
if (allLiterals) {
|
|
4687
|
-
const values = new Set(options.flatMap((opt) => opt._zod.def.values));
|
|
4688
|
-
const valuesConst = addConstant(ctx, values);
|
|
4689
|
-
doc.write(`if (!${valuesConst}.has(${accessor})) return INVALID;`);
|
|
4690
|
-
return accessor;
|
|
4691
|
-
}
|
|
4692
|
-
const outputVar = newVar(ctx);
|
|
4693
|
-
doc.write(`let ${outputVar};`);
|
|
4694
|
-
for (let i = 0;i < options.length; i++) {
|
|
4695
|
-
const opt = options[i];
|
|
4696
|
-
if (i === 0) {
|
|
4697
|
-
doc.write(`${outputVar} = (() => {`);
|
|
4698
|
-
} else {
|
|
4699
|
-
doc.write(`if (${outputVar} === INVALID) ${outputVar} = (() => {`);
|
|
4700
|
-
}
|
|
4701
|
-
doc.indented((d) => {
|
|
4702
|
-
const branchOutput = generateCheck(d, ctx, opt, accessor);
|
|
4703
|
-
d.write(`return ${branchOutput};`);
|
|
4704
|
-
});
|
|
4705
|
-
doc.write(`})();`);
|
|
4706
|
-
}
|
|
4707
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
4708
|
-
return outputVar;
|
|
4709
|
-
}
|
|
4710
|
-
function generateDiscriminatedUnionCheck(doc, ctx, def, accessor) {
|
|
4711
|
-
if (def.unionFallback) {
|
|
4712
|
-
throw new ZodCompileUnsupportedError("discriminated union with unionFallback");
|
|
4713
|
-
}
|
|
4714
|
-
if (def.options.length === 0) {
|
|
4715
|
-
doc.write("return INVALID;");
|
|
4716
|
-
return accessor;
|
|
4717
|
-
}
|
|
4718
|
-
const discVar = newVar(ctx);
|
|
4719
|
-
const outputVar = newVar(ctx);
|
|
4720
|
-
doc.write(`const ${discVar} = ${accessor}?.[${esc(def.discriminator)}];`);
|
|
4721
|
-
doc.write(`let ${outputVar};`);
|
|
4722
|
-
let firstBranch = true;
|
|
4723
|
-
const claimed = new Set;
|
|
4724
|
-
for (const option of def.options) {
|
|
4725
|
-
const values = option._zod.propValues?.[def.discriminator];
|
|
4726
|
-
if (!values || values.size === 0) {
|
|
4727
|
-
throw new ZodCompileUnsupportedError("discriminated union option without static discriminator values");
|
|
4728
|
-
}
|
|
4729
|
-
for (const value of values) {
|
|
4730
|
-
if (claimed.has(value)) {
|
|
4731
|
-
throw new ZodCompileUnsupportedError(`duplicate discriminator value ${String(value)}`);
|
|
4732
|
-
}
|
|
4733
|
-
claimed.add(value);
|
|
4734
|
-
}
|
|
4735
|
-
const conditions = Array.from(values, (value) => literalEquality(ctx, discVar, value));
|
|
4736
|
-
const prefix = firstBranch ? "if" : "else if";
|
|
4737
|
-
doc.write(`${prefix} (${conditions.join(" || ")}) {`);
|
|
4738
|
-
doc.indented((d) => {
|
|
4739
|
-
const branchOutput = generateCheck(d, ctx, option, accessor);
|
|
4740
|
-
d.write(`${outputVar} = ${branchOutput};`);
|
|
4741
|
-
});
|
|
4742
|
-
doc.write(`}`);
|
|
4743
|
-
firstBranch = false;
|
|
4744
|
-
}
|
|
4745
|
-
doc.write(`else { return INVALID; }`);
|
|
4746
|
-
return outputVar;
|
|
4747
|
-
}
|
|
4748
|
-
function literalEquality(ctx, accessor, value) {
|
|
4749
|
-
if (typeof value === "string")
|
|
4750
|
-
return `${accessor} === ${esc(value)}`;
|
|
4751
|
-
if (typeof value === "number") {
|
|
4752
|
-
if (Number.isNaN(value))
|
|
4753
|
-
return `Number.isNaN(${accessor})`;
|
|
4754
|
-
return `${accessor} === ${value}`;
|
|
4755
|
-
}
|
|
4756
|
-
if (typeof value === "boolean")
|
|
4757
|
-
return `${accessor} === ${value}`;
|
|
4758
|
-
if (value === null)
|
|
4759
|
-
return `${accessor} === null`;
|
|
4760
|
-
if (value === undefined)
|
|
4761
|
-
return `${accessor} === undefined`;
|
|
4762
|
-
if (typeof value === "bigint")
|
|
4763
|
-
return `${accessor} === ${value}n`;
|
|
4764
|
-
if (typeof value === "symbol") {
|
|
4765
|
-
const symbolConst = addConstant(ctx, value);
|
|
4766
|
-
return `${accessor} === ${symbolConst}`;
|
|
4767
|
-
}
|
|
4768
|
-
throw new ZodCompileUnsupportedError(`literal discriminator value ${String(value)}`);
|
|
4769
|
-
}
|
|
4770
|
-
function generateIntersectionCheck(doc, ctx, schema, accessor) {
|
|
4771
|
-
const def = schema._zod.def;
|
|
4772
|
-
ctx.definite = false;
|
|
4773
|
-
const leftOutput = compileChild(doc, ctx, def.left, accessor);
|
|
4774
|
-
const rightOutput = compileChild(doc, ctx, def.right, accessor);
|
|
4775
|
-
const mergeConst = addConstant(ctx, mergeValues);
|
|
4776
|
-
const mergedVar = newVar(ctx);
|
|
4777
|
-
doc.write(`const ${mergedVar} = ${mergeConst}(${leftOutput}, ${rightOutput});`);
|
|
4778
|
-
doc.write(`if (!${mergedVar}.valid) return INVALID;`);
|
|
4779
|
-
return `${mergedVar}.data`;
|
|
4780
|
-
}
|
|
4781
|
-
function generateRecordCheck(doc, ctx, schema, accessor) {
|
|
4782
|
-
const def = schema._zod.def;
|
|
4783
|
-
const isPlainObjectConst = addConstant(ctx, isPlainObject);
|
|
4784
|
-
doc.write(`if (!${isPlainObjectConst}(${accessor})) return INVALID;`);
|
|
4785
|
-
const outputVar = newVar(ctx);
|
|
4786
|
-
const kVar = newVar(ctx);
|
|
4787
|
-
const valVar = newVar(ctx);
|
|
4788
|
-
doc.write(`const ${outputVar} = {};`);
|
|
4789
|
-
const recordDef = def;
|
|
4790
|
-
const keyValues = recordDef.partial ? undefined : def.keyType._zod.values;
|
|
4791
|
-
if (keyValues) {
|
|
4792
|
-
const inputKeys = [];
|
|
4793
|
-
for (const key of keyValues) {
|
|
4794
|
-
if (!(typeof key === "string" || typeof key === "number" || typeof key === "symbol")) {
|
|
4795
|
-
throw new ZodCompileUnsupportedError(`record key value ${String(key)}`);
|
|
4796
|
-
}
|
|
4797
|
-
const inputKey = typeof key === "number" ? key.toString() : key;
|
|
4798
|
-
if (inputKey === "__proto__") {
|
|
4799
|
-
throw new ZodCompileUnsupportedError('record key "__proto__"');
|
|
4800
|
-
}
|
|
4801
|
-
inputKeys.push(inputKey);
|
|
4802
|
-
const keyConst = addConstant(ctx, key);
|
|
4803
|
-
const outKey = generateCheck(doc, ctx, def.keyType, keyConst);
|
|
4804
|
-
const valueVar = newVar(ctx);
|
|
4805
|
-
doc.write(`const ${valueVar} = ${accessor}[${literalPropertyKey(ctx, inputKey)}];`);
|
|
4806
|
-
const valOutput = compileChild(doc, ctx, def.valueType, valueVar);
|
|
4807
|
-
doc.write(`${outputVar}[${outKey}] = ${valOutput};`);
|
|
4808
|
-
}
|
|
4809
|
-
const knownKeysConst = addConstant(ctx, new Set(inputKeys));
|
|
4810
|
-
doc.write(`for (const ${kVar} in ${accessor}) {`);
|
|
4811
|
-
doc.indented((d) => {
|
|
4812
|
-
d.write(`if (${knownKeysConst}.has(${kVar})) continue;`);
|
|
4813
|
-
if (recordDef.mode === "loose") {
|
|
4814
|
-
d.write(`if (${kVar} !== "__proto__") ${outputVar}[${kVar}] = ${accessor}[${kVar}];`);
|
|
4815
|
-
} else {
|
|
4816
|
-
d.write(`return INVALID;`);
|
|
4817
|
-
}
|
|
4818
|
-
});
|
|
4819
|
-
doc.write(`}`);
|
|
4820
|
-
return outputVar;
|
|
4821
|
-
}
|
|
4822
|
-
const keyDef = def.keyType._zod.def;
|
|
4823
|
-
const keyIsBareString = keyDef.type === "string" && keyDef.format === undefined && !keyDef.coerce && (keyDef.checks?.length ?? 0) === 0;
|
|
4824
|
-
if (!keyIsBareString) {
|
|
4825
|
-
const isLoose = def.mode === "loose";
|
|
4826
|
-
const keyFn = compileFn(def.keyType);
|
|
4827
|
-
if (keyFn.definite === false)
|
|
4828
|
-
ctx.definite = false;
|
|
4829
|
-
const keyFast = addConstant(ctx, keyFn);
|
|
4830
|
-
const numericConst = addConstant(ctx, number);
|
|
4831
|
-
const outKeyVar = newVar(ctx);
|
|
4832
|
-
emitOwnKeys(doc, ctx, accessor, kVar, (d) => {
|
|
4833
|
-
d.write(`let ${outKeyVar} = ${keyFast}(${kVar});`);
|
|
4834
|
-
d.write(`if (${outKeyVar} === INVALID && typeof ${kVar} === "string" && ${numericConst}.test(${kVar})) ${outKeyVar} = ${keyFast}(Number(${kVar}));`);
|
|
4835
|
-
if (isLoose) {
|
|
4836
|
-
d.write(`if (${outKeyVar} === INVALID) { ${outputVar}[${kVar}] = ${accessor}[${kVar}]; continue; }`);
|
|
4837
|
-
} else {
|
|
4838
|
-
d.write(`if (${outKeyVar} === INVALID) return INVALID;`);
|
|
4839
|
-
}
|
|
4840
|
-
d.write(`if (${outKeyVar} === "__proto__") continue;`);
|
|
4841
|
-
const valueVar = newVar(ctx);
|
|
4842
|
-
d.write(`const ${valueVar} = ${accessor}[${kVar}];`);
|
|
4843
|
-
const valOutput = compileChild(d, ctx, def.valueType, valueVar);
|
|
4844
|
-
d.write(`${outputVar}[${outKeyVar}] = ${valOutput};`);
|
|
4845
|
-
});
|
|
4846
|
-
return outputVar;
|
|
4847
|
-
}
|
|
4848
|
-
emitOwnKeys(doc, ctx, accessor, kVar, (d) => {
|
|
4849
|
-
d.write(`const ${valVar} = ${accessor}[${kVar}];`);
|
|
4850
|
-
const valOutput = compileChild(d, ctx, def.valueType, valVar);
|
|
4851
|
-
d.write(`${outputVar}[${kVar}] = ${valOutput};`);
|
|
4852
|
-
}, `return INVALID;`);
|
|
4853
|
-
return outputVar;
|
|
4854
|
-
}
|
|
4855
|
-
function emitOwnKeys(doc, ctx, accessor, kVar, body, onSymbol) {
|
|
4856
|
-
const propIsEnumerableConst = addConstant(ctx, Object.prototype.propertyIsEnumerable);
|
|
4857
|
-
const symsVar = newVar(ctx);
|
|
4858
|
-
const keysVar = newVar(ctx);
|
|
4859
|
-
const iVar = newVar(ctx);
|
|
4860
|
-
doc.write(`const ${symsVar} = Object.getOwnPropertySymbols(${accessor});`);
|
|
4861
|
-
doc.write(`const ${keysVar} = Object.getOwnPropertyNames(${accessor});`);
|
|
4862
|
-
doc.write(`for (let ${iVar} = 0; ${iVar} < ${keysVar}.length; ${iVar}++) {`);
|
|
4863
|
-
doc.indented((d) => {
|
|
4864
|
-
d.write(`const ${kVar} = ${keysVar}[${iVar}];`);
|
|
4865
|
-
d.write(`if (${kVar} === "__proto__" || !${propIsEnumerableConst}.call(${accessor}, ${kVar})) continue;`);
|
|
4866
|
-
body(d);
|
|
4867
|
-
});
|
|
4868
|
-
doc.write(`}`);
|
|
4869
|
-
doc.write(`for (let ${iVar} = 0; ${iVar} < ${symsVar}.length; ${iVar}++) {`);
|
|
4870
|
-
doc.indented((d) => {
|
|
4871
|
-
d.write(`const ${kVar} = ${symsVar}[${iVar}];`);
|
|
4872
|
-
d.write(`if (!${propIsEnumerableConst}.call(${accessor}, ${kVar})) continue;`);
|
|
4873
|
-
if (onSymbol)
|
|
4874
|
-
d.write(onSymbol);
|
|
4875
|
-
else
|
|
4876
|
-
body(d);
|
|
4877
|
-
});
|
|
4878
|
-
doc.write(`}`);
|
|
4879
|
-
}
|
|
4880
|
-
function literalPropertyKey(ctx, key) {
|
|
4881
|
-
if (typeof key === "string")
|
|
4882
|
-
return esc(key);
|
|
4883
|
-
return addConstant(ctx, key);
|
|
4884
|
-
}
|
|
4885
|
-
function generateMapCheck(doc, ctx, schema, accessor) {
|
|
4886
|
-
const def = schema._zod.def;
|
|
4887
|
-
doc.write(`if (!(${accessor} instanceof Map)) return INVALID;`);
|
|
4888
|
-
const outputVar = newVar(ctx);
|
|
4889
|
-
const kVar = newVar(ctx);
|
|
4890
|
-
const valVar = newVar(ctx);
|
|
4891
|
-
doc.write(`const ${outputVar} = new Map();`);
|
|
4892
|
-
doc.write(`for (const [${kVar}, ${valVar}] of ${accessor}) {`);
|
|
4893
|
-
doc.indented((d) => {
|
|
4894
|
-
const keyOutput = generateCheck(d, ctx, def.keyType, kVar);
|
|
4895
|
-
const valOutput = generateCheck(d, ctx, def.valueType, valVar);
|
|
4896
|
-
d.write(`${outputVar}.set(${keyOutput}, ${valOutput});`);
|
|
4897
|
-
});
|
|
4898
|
-
doc.write(`}`);
|
|
4899
|
-
return outputVar;
|
|
4900
|
-
}
|
|
4901
|
-
function generateSetCheck(doc, ctx, schema, accessor) {
|
|
4902
|
-
const def = schema._zod.def;
|
|
4903
|
-
doc.write(`if (!(${accessor} instanceof Set)) return INVALID;`);
|
|
4904
|
-
const outputVar = newVar(ctx);
|
|
4905
|
-
const valVar = newVar(ctx);
|
|
4906
|
-
doc.write(`const ${outputVar} = new Set();`);
|
|
4907
|
-
doc.write(`for (const ${valVar} of ${accessor}) {`);
|
|
4908
|
-
doc.indented((d) => {
|
|
4909
|
-
const valOutput = generateCheck(d, ctx, def.valueType, valVar);
|
|
4910
|
-
d.write(`${outputVar}.add(${valOutput});`);
|
|
4911
|
-
});
|
|
4912
|
-
doc.write(`}`);
|
|
4913
|
-
return outputVar;
|
|
4914
|
-
}
|
|
4915
|
-
function generateFileCheck(doc, accessor) {
|
|
4916
|
-
doc.write(`if (!(${accessor} instanceof File)) return INVALID;`);
|
|
4917
|
-
return accessor;
|
|
4918
|
-
}
|
|
4919
|
-
function generateTemplateLiteralCheck(doc, ctx, schema, accessor) {
|
|
4920
|
-
doc.write(`if (typeof ${accessor} !== "string") return INVALID;`);
|
|
4921
|
-
const pattern = schema._zod.pattern;
|
|
4922
|
-
if (pattern) {
|
|
4923
|
-
const patternConst = addConstant(ctx, pattern);
|
|
4924
|
-
doc.write(`${patternConst}.lastIndex = 0;`);
|
|
4925
|
-
doc.write(`if (!${patternConst}.test(${accessor})) return INVALID;`);
|
|
4926
|
-
}
|
|
4927
|
-
return accessor;
|
|
4928
|
-
}
|
|
4929
|
-
function generateLazyCheck(doc, ctx, schema, accessor) {
|
|
4930
|
-
const def = schema._zod.def;
|
|
4931
|
-
const getterConst = addUserConstant(ctx, def.getter);
|
|
4932
|
-
const cacheConst = addConstant(ctx, { parser: null });
|
|
4933
|
-
doc.write(`if (!${cacheConst}.parser) {`);
|
|
4934
|
-
doc.indented((d) => {
|
|
4935
|
-
d.write(`const inner = ${getterConst}();`);
|
|
4936
|
-
d.write(`${cacheConst}.parser = function(input) {`);
|
|
4937
|
-
d.indented((d2) => {
|
|
4938
|
-
d2.write(`const result = inner._zod.run({ value: input, issues: [] }, {});`);
|
|
4939
|
-
d2.write(`return result.issues.length === 0 ? result.value : INVALID;`);
|
|
4940
|
-
});
|
|
4941
|
-
d.write(`};`);
|
|
4942
|
-
});
|
|
4943
|
-
doc.write(`}`);
|
|
4944
|
-
const outputVar = newVar(ctx);
|
|
4945
|
-
doc.write(`const ${outputVar} = ${cacheConst}.parser(${accessor});`);
|
|
4946
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
4947
|
-
return outputVar;
|
|
4948
|
-
}
|
|
4949
|
-
function generatePipeCheck(doc, ctx, schema, accessor) {
|
|
4950
|
-
const def = schema._zod.def;
|
|
4951
|
-
const inputOutput = generateCheck(doc, ctx, def.in, accessor);
|
|
4952
|
-
if (def.transform) {
|
|
4953
|
-
if (isAsyncFunction(def.transform)) {
|
|
4954
|
-
throw new ZodCompileAsyncError("z.compile: async transforms in pipes are not supported");
|
|
4955
|
-
}
|
|
4956
|
-
const transformFn = def.transform;
|
|
4957
|
-
const helperFn = (value) => {
|
|
4958
|
-
const fakePayload = { value, issues: [], addIssue: pushIssue };
|
|
4959
|
-
const result = transformFn(value, fakePayload);
|
|
4960
|
-
if (result instanceof Promise)
|
|
4961
|
-
return INVALID;
|
|
4962
|
-
return fakePayload.issues.length === 0 ? result : INVALID;
|
|
4963
|
-
};
|
|
4964
|
-
const helperConst = addUserConstant(ctx, helperFn);
|
|
4965
|
-
const transformedVar = newVar(ctx);
|
|
4966
|
-
doc.write(`const ${transformedVar} = ${helperConst}(${inputOutput});`);
|
|
4967
|
-
doc.write(`if (${transformedVar} === INVALID) return INVALID;`);
|
|
4968
|
-
return generateCheck(doc, ctx, def.out, transformedVar);
|
|
4969
|
-
} else {
|
|
4970
|
-
return generateCheck(doc, ctx, def.out, inputOutput);
|
|
4971
|
-
}
|
|
4972
|
-
}
|
|
4973
|
-
function isAsyncFunction(fn) {
|
|
4974
|
-
return typeof fn === "function" && (fn.constructor.name === "AsyncFunction" || fn[Symbol.toStringTag] === "AsyncFunction");
|
|
4975
|
-
}
|
|
4976
|
-
function generateCustomCheck(doc, ctx, schema, accessor) {
|
|
4977
|
-
const def = schema._zod.def;
|
|
4978
|
-
if (def.fn) {
|
|
4979
|
-
if (isAsyncFunction(def.fn)) {
|
|
4980
|
-
throw new ZodCompileAsyncError("z.compile: async custom predicates are not supported");
|
|
4981
|
-
}
|
|
4982
|
-
const fnConst = addUserConstant(ctx, def.fn);
|
|
4983
|
-
const throwAsyncConst = addConstant(ctx, throwAsync);
|
|
4984
|
-
const resVar = newVar(ctx);
|
|
4985
|
-
doc.write(`const ${resVar} = ${fnConst}(${accessor});`);
|
|
4986
|
-
doc.write(`if (${resVar} instanceof Promise) ${throwAsyncConst}();`);
|
|
4987
|
-
doc.write(`if (!${resVar}) return INVALID;`);
|
|
4988
|
-
} else {
|
|
4989
|
-
throw new ZodCompileUnsupportedError("custom schema without a predicate function");
|
|
4990
|
-
}
|
|
4991
|
-
return accessor;
|
|
4992
|
-
}
|
|
4993
|
-
function runtimeCatch(innerSchema, catchValue, value) {
|
|
4994
|
-
const result = innerSchema._zod.run({ value, issues: [] }, {});
|
|
4995
|
-
if (result && typeof result.then === "function")
|
|
4996
|
-
return INVALID;
|
|
4997
|
-
const r = result;
|
|
4998
|
-
if (r.issues.length === 0)
|
|
4999
|
-
return r.value;
|
|
5000
|
-
return catchValue();
|
|
5001
|
-
}
|
|
5002
|
-
function generateCatchCheck(doc, ctx, schema, accessor) {
|
|
5003
|
-
const def = schema._zod.def;
|
|
5004
|
-
if (!def.catchValue[CONSTANT_CATCH]) {
|
|
5005
|
-
throw new ZodCompileUnsupportedError("catch with a callback (only a constant catch value compiles)", false);
|
|
5006
|
-
}
|
|
5007
|
-
const outputVar = newVar(ctx);
|
|
5008
|
-
doc.write(`let ${outputVar} = (() => {`);
|
|
5009
|
-
doc.indented((d) => {
|
|
5010
|
-
const innerOut = compileChild(d, ctx, def.innerType, accessor);
|
|
5011
|
-
d.write(`return ${innerOut};`);
|
|
5012
|
-
});
|
|
5013
|
-
doc.write(`})();`);
|
|
5014
|
-
const innerConst = addConstant(ctx, def.innerType);
|
|
5015
|
-
const catchConst = addUserConstant(ctx, def.catchValue);
|
|
5016
|
-
const catchHelperConst = addConstant(ctx, runtimeCatch);
|
|
5017
|
-
doc.write(`if (${outputVar} === INVALID) {`);
|
|
5018
|
-
doc.indented((d) => {
|
|
5019
|
-
d.write(`${outputVar} = ${catchHelperConst}(${innerConst}, ${catchConst}, ${accessor});`);
|
|
5020
|
-
d.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
5021
|
-
});
|
|
5022
|
-
doc.write(`}`);
|
|
5023
|
-
return outputVar;
|
|
5024
|
-
}
|
|
5025
|
-
function generateTransformCheck(doc, ctx, schema, accessor) {
|
|
5026
|
-
const def = schema._zod.def;
|
|
5027
|
-
if (def.transform) {
|
|
5028
|
-
if (isAsyncFunction(def.transform)) {
|
|
5029
|
-
throw new ZodCompileAsyncError("z.compile: async transforms are not supported");
|
|
5030
|
-
}
|
|
5031
|
-
const transformFn = def.transform;
|
|
5032
|
-
const helperFn = (value) => {
|
|
5033
|
-
const fakePayload = { value, issues: [], addIssue: pushIssue };
|
|
5034
|
-
const result = transformFn(value, fakePayload);
|
|
5035
|
-
if (result instanceof Promise)
|
|
5036
|
-
return INVALID;
|
|
5037
|
-
return fakePayload.issues.length === 0 ? result : INVALID;
|
|
5038
|
-
};
|
|
5039
|
-
const helperConst = addUserConstant(ctx, helperFn);
|
|
5040
|
-
const outputVar = newVar(ctx);
|
|
5041
|
-
doc.write(`const ${outputVar} = ${helperConst}(${accessor});`);
|
|
5042
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
5043
|
-
return outputVar;
|
|
5044
|
-
}
|
|
5045
|
-
return accessor;
|
|
5046
|
-
}
|
|
5047
3409
|
// node_modules/zod/v4/core/api.js
|
|
5048
3410
|
function snapshotChecks(def) {
|
|
5049
3411
|
if (def.checks)
|
|
@@ -8486,7 +6848,6 @@ var advertisedOperationSchemas = {
|
|
|
8486
6848
|
worker: advertised(workerOperations),
|
|
8487
6849
|
state: advertised(stateOperations)
|
|
8488
6850
|
};
|
|
8489
|
-
var compiledOperationSchemas = Object.fromEntries(OPERATION_FAMILIES.map((family) => [family, compile(operationSchemas[family])]));
|
|
8490
6851
|
var success = (revisionValue, workspaceRevisionValue, data) => ({
|
|
8491
6852
|
ok: true,
|
|
8492
6853
|
schemaVersion: 1,
|
|
@@ -8508,8 +6869,7 @@ function resolveIdRef(kind, ref, ids) {
|
|
|
8508
6869
|
function parseOperation(family, input) {
|
|
8509
6870
|
if (typeof input === "object" && input !== null && "schemaVersion" in input && input.schemaVersion !== SCHEMA_VERSION)
|
|
8510
6871
|
return failure2("unsupported_version", "unsupported schema version", { operation: family });
|
|
8511
|
-
const
|
|
8512
|
-
const parsed = compiled.success ? operationSchemas[family].safeParse(input) : compiled;
|
|
6872
|
+
const parsed = operationSchemas[family].safeParse(input);
|
|
8513
6873
|
if (parsed.success)
|
|
8514
6874
|
return success(null, null, parsed.data);
|
|
8515
6875
|
const fields = parsed.error.issues.map((issue) => ({
|
|
@@ -16744,9 +15104,17 @@ var sessionMarkerFile = (input, session, store) => {
|
|
|
16744
15104
|
return path18.join(dir, "hooks", `skills-${name}.json`);
|
|
16745
15105
|
};
|
|
16746
15106
|
var list2 = (value) => Array.isArray(value) ? value.filter((item) => typeof item === "string") : [];
|
|
16747
|
-
var
|
|
15107
|
+
var EMPTY = { nudged: [], loaded: [], raw: [], stops: [], judges: [] };
|
|
15108
|
+
var readRaw = (file) => {
|
|
15109
|
+
try {
|
|
15110
|
+
return fs15.readFileSync(file, "utf8");
|
|
15111
|
+
} catch {
|
|
15112
|
+
return "";
|
|
15113
|
+
}
|
|
15114
|
+
};
|
|
15115
|
+
var parseMarker = (text) => {
|
|
16748
15116
|
try {
|
|
16749
|
-
const value = JSON.parse(
|
|
15117
|
+
const value = JSON.parse(text);
|
|
16750
15118
|
return {
|
|
16751
15119
|
nudged: list2(value.nudged),
|
|
16752
15120
|
loaded: list2(value.loaded),
|
|
@@ -16755,13 +15123,36 @@ var readSessionMarker = (file) => {
|
|
|
16755
15123
|
judges: list2(value.judges)
|
|
16756
15124
|
};
|
|
16757
15125
|
} catch {
|
|
16758
|
-
return {
|
|
15126
|
+
return { ...EMPTY };
|
|
15127
|
+
}
|
|
15128
|
+
};
|
|
15129
|
+
var readSessionMarker = (file) => parseMarker(readRaw(file));
|
|
15130
|
+
var writeAtomic = (file, marker) => {
|
|
15131
|
+
fs15.mkdirSync(path18.dirname(file), { recursive: true });
|
|
15132
|
+
const temp = `${file}.${process.pid}.${Math.random().toString(36).slice(2)}.tmp`;
|
|
15133
|
+
try {
|
|
15134
|
+
fs15.writeFileSync(temp, JSON.stringify(marker));
|
|
15135
|
+
fs15.renameSync(temp, file);
|
|
15136
|
+
} catch (error) {
|
|
15137
|
+
fs15.rmSync(temp, { force: true });
|
|
15138
|
+
throw error;
|
|
16759
15139
|
}
|
|
16760
15140
|
};
|
|
16761
15141
|
var writeSessionMarker = (file, marker) => {
|
|
16762
15142
|
try {
|
|
16763
|
-
|
|
16764
|
-
|
|
15143
|
+
writeAtomic(file, marker);
|
|
15144
|
+
} catch {}
|
|
15145
|
+
};
|
|
15146
|
+
var updateSessionMarker = (file, change) => {
|
|
15147
|
+
try {
|
|
15148
|
+
for (let attempt = 0;attempt < 2; attempt++) {
|
|
15149
|
+
const before = readRaw(file);
|
|
15150
|
+
const next = change(parseMarker(before));
|
|
15151
|
+
if (attempt === 0 && readRaw(file) !== before)
|
|
15152
|
+
continue;
|
|
15153
|
+
writeAtomic(file, next);
|
|
15154
|
+
return;
|
|
15155
|
+
}
|
|
16765
15156
|
} catch {}
|
|
16766
15157
|
};
|
|
16767
15158
|
var trackJudge = (input, agentId, running) => {
|
|
@@ -16769,9 +15160,10 @@ var trackJudge = (input, agentId, running) => {
|
|
|
16769
15160
|
const file = session && agentId ? sessionMarkerFile(input, session) : null;
|
|
16770
15161
|
if (!file || !agentId)
|
|
16771
15162
|
return;
|
|
16772
|
-
|
|
16773
|
-
|
|
16774
|
-
|
|
15163
|
+
updateSessionMarker(file, (marker) => {
|
|
15164
|
+
const rest = marker.judges.filter((id) => id !== agentId);
|
|
15165
|
+
return { ...marker, judges: running ? [...rest, agentId] : rest };
|
|
15166
|
+
});
|
|
16775
15167
|
};
|
|
16776
15168
|
|
|
16777
15169
|
// packages/workit-core/src/hooks/shell-words.ts
|
|
@@ -18420,8 +16812,8 @@ var offPolicyPush = (action, repo, facts, remote) => {
|
|
|
18420
16812
|
var nudgeFor = (action, host, session) => {
|
|
18421
16813
|
switch (action.kind) {
|
|
18422
16814
|
case "commit": {
|
|
18423
|
-
const
|
|
18424
|
-
return action.amend ? `workit: raw ${quote("git commit --amend")} is recorded for this session; for new commits prefer ${quote(`${
|
|
16815
|
+
const workit = host === "cursor" && session ? `workit --session ${session}` : "workit";
|
|
16816
|
+
return action.amend ? `workit: raw ${quote("git commit --amend")} is recorded for this session; for new commits prefer ${quote(`${workit} git commit -m '<msg>'`)} (adds the Workit-Session trailer and checks branch and commit policy).` : `workit: prefer ${quote(`${workit} git commit -m '<msg>' [--all | -- <paths>]`)}: it adds the Workit-Session trailer and checks branch and commit policy.`;
|
|
18425
16817
|
}
|
|
18426
16818
|
case "push":
|
|
18427
16819
|
return action.blindForce ? `workit: ${quote("git push --force")} overwrites without a lease; use ${quote("workit git push --force-with-lease")}.` : `workit: prefer ${quote("workit git push")}: it refuses protected branches and verifies the remote tip.`;
|
|
@@ -22173,7 +20565,7 @@ var shellPolicy = (cwd, command, dialect = "posix") => {
|
|
|
22173
20565
|
import fs20 from "node:fs";
|
|
22174
20566
|
import os6 from "node:os";
|
|
22175
20567
|
import path27 from "node:path";
|
|
22176
|
-
var
|
|
20568
|
+
var EMPTY2 = () => ({ deny: [], allow: [] });
|
|
22177
20569
|
var readText2 = (file) => {
|
|
22178
20570
|
try {
|
|
22179
20571
|
return fs20.readFileSync(file, "utf8");
|
|
@@ -22250,7 +20642,7 @@ var bashPattern = (entry) => {
|
|
|
22250
20642
|
return match ? match[1] ?? "" : null;
|
|
22251
20643
|
};
|
|
22252
20644
|
function claudeRules(cwd, env = process.env) {
|
|
22253
|
-
const rules =
|
|
20645
|
+
const rules = EMPTY2();
|
|
22254
20646
|
try {
|
|
22255
20647
|
for (const file of claudeSettingsFiles(cwd, env)) {
|
|
22256
20648
|
const text = readText2(file);
|
|
@@ -22330,7 +20722,7 @@ var opencodeConfig = (value, rules) => {
|
|
|
22330
20722
|
return Object.values(value.agent).every((agent) => isRecord4(agent) && opencodePermission(agent.permission, rules));
|
|
22331
20723
|
};
|
|
22332
20724
|
function opencodeRules(cwd, env = process.env) {
|
|
22333
|
-
const rules =
|
|
20725
|
+
const rules = EMPTY2();
|
|
22334
20726
|
try {
|
|
22335
20727
|
const config = env.XDG_CONFIG_HOME || path27.join(homeOf(env), ".config");
|
|
22336
20728
|
const files = [
|
|
@@ -22377,7 +20769,7 @@ var codexRule = (body) => {
|
|
|
22377
20769
|
return { pattern: words.join(" "), decision };
|
|
22378
20770
|
};
|
|
22379
20771
|
function codexRules(cwd, env = process.env) {
|
|
22380
|
-
const rules =
|
|
20772
|
+
const rules = EMPTY2();
|
|
22381
20773
|
try {
|
|
22382
20774
|
const home = env.CODEX_HOME || path27.join(homeOf(env), ".codex");
|
|
22383
20775
|
const top = gitTop(cwd) ?? cwd;
|
|
@@ -22406,7 +20798,7 @@ function codexRules(cwd, env = process.env) {
|
|
|
22406
20798
|
return rules;
|
|
22407
20799
|
}
|
|
22408
20800
|
function cursorRules(cwd, env = process.env) {
|
|
22409
|
-
const rules =
|
|
20801
|
+
const rules = EMPTY2();
|
|
22410
20802
|
try {
|
|
22411
20803
|
const top = gitTop(cwd) ?? cwd;
|
|
22412
20804
|
for (const file of [
|
|
@@ -23734,25 +22126,29 @@ var redChecks = (rows, pr, head, endpoint) => {
|
|
|
23734
22126
|
reason: `PR #${pr}'s checks were last seen ${state} at ${short(head)}, and this workspace's endpoint is \`${endpoint}\`: run \`workit ci wait --pr ${pr}\`${state === "failing" ? " after fixing the failures `workit pr status` lists (then push)" : ""}.`
|
|
23735
22127
|
};
|
|
23736
22128
|
};
|
|
23737
|
-
var
|
|
22129
|
+
var taskJudgment = (cwd) => {
|
|
23738
22130
|
try {
|
|
23739
22131
|
const store = new TaskStore(cwd);
|
|
23740
22132
|
const listed = store.listTaskIndex();
|
|
23741
22133
|
const key = store.currentKey();
|
|
23742
22134
|
if (!listed.ok || !key.ok)
|
|
23743
|
-
return
|
|
22135
|
+
return null;
|
|
23744
22136
|
const entry = listed.data.filter((item) => item.key === key.data.key && item.status !== "closed").toSorted((left, right) => right.updatedAt.localeCompare(left.updatedAt))[0];
|
|
23745
22137
|
const task = entry ? store.readTask(entry.id) : null;
|
|
23746
|
-
return task?.ok === true
|
|
22138
|
+
return task?.ok === true ? latestJudgment(task.data) : null;
|
|
23747
22139
|
} catch {
|
|
23748
|
-
return
|
|
22140
|
+
return null;
|
|
23749
22141
|
}
|
|
23750
22142
|
};
|
|
23751
22143
|
var unverified = (cwd, branch, all, descriptor, verification) => {
|
|
23752
22144
|
const check = checkVerdicts(cwd, branch, all);
|
|
23753
22145
|
if (check.accepted.accepted || check.accepted.reasons.includes("failing_verdict"))
|
|
23754
22146
|
return null;
|
|
23755
|
-
const
|
|
22147
|
+
const judgment = taskJudgment(cwd);
|
|
22148
|
+
const high = judgment?.riskTier === "high";
|
|
22149
|
+
if (judgment && !judgment.behaviorChange && !high)
|
|
22150
|
+
return null;
|
|
22151
|
+
const independent = verification === "independent" || high;
|
|
23756
22152
|
if (!independent && check.review === "self-reviewed")
|
|
23757
22153
|
return null;
|
|
23758
22154
|
const has = check.review === "self-reviewed" ? "only a self verdict" : "no current verdict";
|
|
@@ -23823,7 +22219,7 @@ function stopDecision(input, descriptor) {
|
|
|
23823
22219
|
if (file && marker) {
|
|
23824
22220
|
if (marker.stops.includes(key))
|
|
23825
22221
|
return NONE4;
|
|
23826
|
-
|
|
22222
|
+
updateSessionMarker(file, (fresh) => ({ ...fresh, stops: [...fresh.stops, key] }));
|
|
23827
22223
|
}
|
|
23828
22224
|
return {
|
|
23829
22225
|
kind: "continue",
|
|
@@ -25661,7 +24057,16 @@ ${nudge}` : content) };
|
|
|
25661
24057
|
pi.on("session_compact_failed", () => {
|
|
25662
24058
|
return;
|
|
25663
24059
|
});
|
|
25664
|
-
pi.on("tool_call", (event, ctx) =>
|
|
24060
|
+
pi.on("tool_call", (event, ctx) => {
|
|
24061
|
+
try {
|
|
24062
|
+
return enforceToolPolicy(event, ctx);
|
|
24063
|
+
} catch (error) {
|
|
24064
|
+
const message = `hook failure: ${String(error)}`;
|
|
24065
|
+
process.stderr.write(`[workit] tool_call gate failed: ${message}
|
|
24066
|
+
`);
|
|
24067
|
+
return PI_DESCRIPTOR.shellPolicy.failClosed ? { block: true, reason: message } : undefined;
|
|
24068
|
+
}
|
|
24069
|
+
});
|
|
25665
24070
|
pi.on("tool_result", (event, ctx) => {
|
|
25666
24071
|
const observed = observeResult(event, ctx);
|
|
25667
24072
|
let standing = null;
|