@brainervirus/workit-codex 8.22.2 → 8.23.0
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/.codex-plugin/plugin.json +1 -1
- package/agents/workit-implementer.toml +27 -19
- package/dist/launch-mcp.js +33 -1656
- package/dist/{workit-hook-aq4zdwdp.js → workit-hook-hegafjtc.js} +505 -1749
- package/dist/workit-hook.js +3 -1
- package/package.json +3 -3
- package/skills/workit-fanout/SKILL.md +4 -4
- package/skills/workit-fanout/references/brief.md +17 -11
- package/skills/workit-fanout/references/orchestration.md +6 -6
- package/skills/workit-implement/SKILL.md +2 -0
package/dist/launch-mcp.js
CHANGED
|
@@ -6622,7 +6622,7 @@ var require_dist = __commonJS(function(exports, module) {
|
|
|
6622
6622
|
|
|
6623
6623
|
// packages/workit-codex/scripts/launch-mcp.ts
|
|
6624
6624
|
import { existsSync as existsSync10, realpathSync as realpathSync7, statSync as statSync3 } from "node:fs";
|
|
6625
|
-
import
|
|
6625
|
+
import path25 from "node:path";
|
|
6626
6626
|
import { fileURLToPath as fileURLToPath3 } from "node:url";
|
|
6627
6627
|
|
|
6628
6628
|
// packages/workit-core/src/core.ts
|
|
@@ -7587,8 +7587,6 @@ var base64 = /^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/
|
|
|
7587
7587
|
var base64url = /^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2,3})?$/;
|
|
7588
7588
|
var httpProtocol = /^https?$/;
|
|
7589
7589
|
var e164 = /^\+[1-9]\d{6,14}$/;
|
|
7590
|
-
var creditCard = /^\d(?:[ -]?\d){11,18}$/;
|
|
7591
|
-
var iban = /^[A-Z]{2}(?!00|01|99)\d{2}[A-Z0-9]{11,30}$/;
|
|
7592
7590
|
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])))`;
|
|
7593
7591
|
function anchor(source) {
|
|
7594
7592
|
return new RegExp(`^${source}$`);
|
|
@@ -8435,41 +8433,6 @@ var $ZodE164 = /* @__PURE__ */ $constructor("$ZodE164", (inst, def) => {
|
|
|
8435
8433
|
def.pattern ?? (def.pattern = e164);
|
|
8436
8434
|
$ZodStringFormat.init(inst, def);
|
|
8437
8435
|
});
|
|
8438
|
-
var CC_SANITIZE = /[- ]/g;
|
|
8439
|
-
function isLuhnAlgo(digits) {
|
|
8440
|
-
let length = digits.length;
|
|
8441
|
-
let bit = 1;
|
|
8442
|
-
let sum = 0;
|
|
8443
|
-
while (length) {
|
|
8444
|
-
const value = digits.charCodeAt(--length) - 48;
|
|
8445
|
-
bit ^= 1;
|
|
8446
|
-
sum += bit ? [0, 2, 4, 6, 8, 1, 3, 5, 7, 9][value] : value;
|
|
8447
|
-
}
|
|
8448
|
-
return sum % 10 === 0;
|
|
8449
|
-
}
|
|
8450
|
-
function isValidCreditCard(input) {
|
|
8451
|
-
if (!creditCard.test(input))
|
|
8452
|
-
return false;
|
|
8453
|
-
return isLuhnAlgo(input.replace(CC_SANITIZE, ""));
|
|
8454
|
-
}
|
|
8455
|
-
function isIso7064Mod97(iban2) {
|
|
8456
|
-
let remainder = 0;
|
|
8457
|
-
const len = iban2.length;
|
|
8458
|
-
for (let i = 4;i < len; i++) {
|
|
8459
|
-
const code = iban2.charCodeAt(i);
|
|
8460
|
-
remainder = (code >= 65 ? remainder * 100 + (code - 55) : remainder * 10 + (code - 48)) % 97;
|
|
8461
|
-
}
|
|
8462
|
-
for (let i = 0;i < 4; i++) {
|
|
8463
|
-
const code = iban2.charCodeAt(i);
|
|
8464
|
-
remainder = (code >= 65 ? remainder * 100 + (code - 55) : remainder * 10 + (code - 48)) % 97;
|
|
8465
|
-
}
|
|
8466
|
-
return remainder === 1;
|
|
8467
|
-
}
|
|
8468
|
-
function isValidIBAN(input) {
|
|
8469
|
-
if (!iban.test(input))
|
|
8470
|
-
return false;
|
|
8471
|
-
return isIso7064Mod97(input);
|
|
8472
|
-
}
|
|
8473
8436
|
function isValidJWT(token, algorithm = null) {
|
|
8474
8437
|
try {
|
|
8475
8438
|
const tokensParts = token.split(".");
|
|
@@ -9802,9 +9765,6 @@ function settle(inst, answer) {
|
|
|
9802
9765
|
recursive.set(inst, answer === PROVEN);
|
|
9803
9766
|
return answer;
|
|
9804
9767
|
}
|
|
9805
|
-
function isRecursiveSchema(inst) {
|
|
9806
|
-
return isRecursive(inst, new Set, true) !== NONE;
|
|
9807
|
-
}
|
|
9808
9768
|
function bucketFor(state, inst) {
|
|
9809
9769
|
let bucket = state.buckets.get(inst);
|
|
9810
9770
|
if (!bucket) {
|
|
@@ -10091,1604 +10051,6 @@ function registry() {
|
|
|
10091
10051
|
}
|
|
10092
10052
|
(_a2 = globalThis).__zod_globalRegistry ?? (_a2.__zod_globalRegistry = registry());
|
|
10093
10053
|
var globalRegistry = globalThis.__zod_globalRegistry;
|
|
10094
|
-
// node_modules/zod/v4/core/compile.js
|
|
10095
|
-
var INVALID = Symbol.for("zod.compile.invalid");
|
|
10096
|
-
var FALLBACK_FLAG = Symbol.for("zod.compile.fallback");
|
|
10097
|
-
|
|
10098
|
-
class ZodCompileAsyncError extends Error {
|
|
10099
|
-
constructor(message = "z.compile does not support async refinements, transforms, or checks") {
|
|
10100
|
-
super(message);
|
|
10101
|
-
this.name = "ZodCompileAsyncError";
|
|
10102
|
-
}
|
|
10103
|
-
}
|
|
10104
|
-
|
|
10105
|
-
class ZodCompileUnsupportedError extends Error {
|
|
10106
|
-
constructor(feature, islandable = true) {
|
|
10107
|
-
super(`z.compile does not support ${feature}; this schema must use the runtime parser`);
|
|
10108
|
-
this.name = "ZodCompileUnsupportedError";
|
|
10109
|
-
this.islandable = islandable;
|
|
10110
|
-
}
|
|
10111
|
-
}
|
|
10112
|
-
function compileValidator(schema, parser) {
|
|
10113
|
-
try {
|
|
10114
|
-
return compileFn(schema, { assertOnly: true });
|
|
10115
|
-
} catch {
|
|
10116
|
-
return parser;
|
|
10117
|
-
}
|
|
10118
|
-
}
|
|
10119
|
-
function compile(schema, options) {
|
|
10120
|
-
try {
|
|
10121
|
-
const parser = compileFn(schema);
|
|
10122
|
-
const clone = withParser(schema, parser);
|
|
10123
|
-
clone._zod.bag.validator = compileValidator(schema, parser);
|
|
10124
|
-
return clone;
|
|
10125
|
-
} catch (err) {
|
|
10126
|
-
if (options?.strict)
|
|
10127
|
-
throw err;
|
|
10128
|
-
return schema;
|
|
10129
|
-
}
|
|
10130
|
-
}
|
|
10131
|
-
function withParser(schema, parser) {
|
|
10132
|
-
if (isRecursiveSchema(schema)) {
|
|
10133
|
-
throw new ZodCompileUnsupportedError("a schema whose subtree contains a reference cycle");
|
|
10134
|
-
}
|
|
10135
|
-
const clone2 = clone(schema);
|
|
10136
|
-
const liveRun = schema._zod.run;
|
|
10137
|
-
const originalRun = liveRun.__originalRun ?? liveRun;
|
|
10138
|
-
const wrapped = (payload, ctx) => {
|
|
10139
|
-
if (ctx?.async || ctx?.direction === "backward" || ctx?.skipChecks || ctx?.[FALLBACK_FLAG]) {
|
|
10140
|
-
return originalRun(payload, ctx);
|
|
10141
|
-
}
|
|
10142
|
-
if (ctx && isBackEdge(ctx, payload.value)) {
|
|
10143
|
-
return originalRun(payload, ctx);
|
|
10144
|
-
}
|
|
10145
|
-
const out = parser(payload.value);
|
|
10146
|
-
if (out !== INVALID) {
|
|
10147
|
-
payload.value = out;
|
|
10148
|
-
return payload;
|
|
10149
|
-
}
|
|
10150
|
-
if (ctx)
|
|
10151
|
-
ctx[FALLBACK_FLAG] = true;
|
|
10152
|
-
return originalRun(payload, ctx);
|
|
10153
|
-
};
|
|
10154
|
-
wrapped.__originalRun = originalRun;
|
|
10155
|
-
clone2._zod.bag.fallbackRun = originalRun;
|
|
10156
|
-
clone2._zod.bag.validator = parser;
|
|
10157
|
-
clone2._zod.run = wrapped;
|
|
10158
|
-
if (!liveRun.__originalRun)
|
|
10159
|
-
installCompiledUserMethods(clone2, schema, parser);
|
|
10160
|
-
return clone2;
|
|
10161
|
-
}
|
|
10162
|
-
function installCompiledUserMethods(target, source, parser) {
|
|
10163
|
-
const targetAny = target;
|
|
10164
|
-
const sourceAny = source;
|
|
10165
|
-
if (typeof sourceAny.safeParse === "function") {
|
|
10166
|
-
const originalSafeParse = sourceAny.safeParse;
|
|
10167
|
-
targetAny.safeParse = (data, params) => {
|
|
10168
|
-
const out = parser(data);
|
|
10169
|
-
if (out !== INVALID) {
|
|
10170
|
-
return { success: true, data: out };
|
|
10171
|
-
}
|
|
10172
|
-
return originalSafeParse(data, params);
|
|
10173
|
-
};
|
|
10174
|
-
}
|
|
10175
|
-
if (typeof sourceAny.parse === "function") {
|
|
10176
|
-
const originalParse = sourceAny.parse;
|
|
10177
|
-
targetAny.parse = (data, params) => {
|
|
10178
|
-
const out = parser(data);
|
|
10179
|
-
if (out !== INVALID) {
|
|
10180
|
-
return out;
|
|
10181
|
-
}
|
|
10182
|
-
return originalParse(data, params);
|
|
10183
|
-
};
|
|
10184
|
-
}
|
|
10185
|
-
}
|
|
10186
|
-
function compileFn(schema, options) {
|
|
10187
|
-
let recursive = true;
|
|
10188
|
-
try {
|
|
10189
|
-
recursive = isRecursiveSchema(schema);
|
|
10190
|
-
} catch {}
|
|
10191
|
-
if (recursive) {
|
|
10192
|
-
throw new ZodCompileUnsupportedError("a schema whose subtree contains a reference cycle");
|
|
10193
|
-
}
|
|
10194
|
-
const ctx = {
|
|
10195
|
-
constants: new Map,
|
|
10196
|
-
constantCounter: 0,
|
|
10197
|
-
varCounter: 0,
|
|
10198
|
-
definite: true
|
|
10199
|
-
};
|
|
10200
|
-
const doc = new Doc(["input"]);
|
|
10201
|
-
const outputAccessor = generateCheck(doc, ctx, schema, "input", !options?.assertOnly);
|
|
10202
|
-
doc.write(outputAccessor === null ? `return true;` : `return ${outputAccessor};`);
|
|
10203
|
-
const constantNames = ["INVALID", ...ctx.constants.keys()];
|
|
10204
|
-
const constantValues = [INVALID, ...ctx.constants.values()];
|
|
10205
|
-
const code = doc.content.join(`
|
|
10206
|
-
`);
|
|
10207
|
-
const fullCode = options?.debug ? constantNames.length > 0 ? `// Constants: ${constantNames.join(", ")}
|
|
10208
|
-
${code}` : code : "";
|
|
10209
|
-
const F = Function;
|
|
10210
|
-
const factoryCode = `return (input) => {
|
|
10211
|
-
${code}
|
|
10212
|
-
}`;
|
|
10213
|
-
let fn;
|
|
10214
|
-
try {
|
|
10215
|
-
const factory = new F(...constantNames, factoryCode);
|
|
10216
|
-
fn = factory(...constantValues);
|
|
10217
|
-
} catch (err) {
|
|
10218
|
-
throw new ZodCompileUnsupportedError(`this schema (generated code failed to evaluate: ${err.message})`);
|
|
10219
|
-
}
|
|
10220
|
-
if (options?.debug) {
|
|
10221
|
-
fn.code = fullCode;
|
|
10222
|
-
}
|
|
10223
|
-
fn.definite = ctx.definite;
|
|
10224
|
-
return fn;
|
|
10225
|
-
}
|
|
10226
|
-
function addConstant(ctx, value) {
|
|
10227
|
-
for (const [name, v] of ctx.constants) {
|
|
10228
|
-
if (v === value)
|
|
10229
|
-
return name;
|
|
10230
|
-
}
|
|
10231
|
-
const name = `c${ctx.constantCounter++}`;
|
|
10232
|
-
ctx.constants.set(name, value);
|
|
10233
|
-
return name;
|
|
10234
|
-
}
|
|
10235
|
-
function addUserConstant(ctx, fn) {
|
|
10236
|
-
ctx.definite = false;
|
|
10237
|
-
return addConstant(ctx, fn);
|
|
10238
|
-
}
|
|
10239
|
-
function newVar(ctx) {
|
|
10240
|
-
return `v${ctx.varCounter++}`;
|
|
10241
|
-
}
|
|
10242
|
-
function runtimeRun(schema, value) {
|
|
10243
|
-
const result = schema._zod.run({ value, issues: [] }, {});
|
|
10244
|
-
if (result && typeof result.then === "function")
|
|
10245
|
-
return INVALID;
|
|
10246
|
-
const r = result;
|
|
10247
|
-
return r.issues.length === 0 ? r.value : INVALID;
|
|
10248
|
-
}
|
|
10249
|
-
function compileChild(doc, ctx, schema, accessor, needsValue = true) {
|
|
10250
|
-
const contentLen = doc.content.length;
|
|
10251
|
-
const constantCount = ctx.constants.size;
|
|
10252
|
-
const constantCounter = ctx.constantCounter;
|
|
10253
|
-
const varCounter = ctx.varCounter;
|
|
10254
|
-
try {
|
|
10255
|
-
return generateCheck(doc, ctx, schema, accessor, needsValue);
|
|
10256
|
-
} catch (err) {
|
|
10257
|
-
if (!(err instanceof ZodCompileUnsupportedError) || !err.islandable)
|
|
10258
|
-
throw err;
|
|
10259
|
-
doc.content.length = contentLen;
|
|
10260
|
-
if (ctx.constants.size > constantCount) {
|
|
10261
|
-
const trailing = Array.from(ctx.constants.keys()).slice(constantCount);
|
|
10262
|
-
for (const k of trailing)
|
|
10263
|
-
ctx.constants.delete(k);
|
|
10264
|
-
}
|
|
10265
|
-
ctx.constantCounter = constantCounter;
|
|
10266
|
-
ctx.varCounter = varCounter;
|
|
10267
|
-
return emitRuntimeIsland(doc, ctx, schema, accessor);
|
|
10268
|
-
}
|
|
10269
|
-
}
|
|
10270
|
-
function emitRuntimeIsland(doc, ctx, schema, accessor) {
|
|
10271
|
-
ctx.definite = false;
|
|
10272
|
-
const schemaConst = addConstant(ctx, schema);
|
|
10273
|
-
const runConst = addConstant(ctx, runtimeRun);
|
|
10274
|
-
const outVar = newVar(ctx);
|
|
10275
|
-
doc.write(`const ${outVar} = ${runConst}(${schemaConst}, ${accessor});`);
|
|
10276
|
-
doc.write(`if (${outVar} === INVALID) return INVALID;`);
|
|
10277
|
-
return outVar;
|
|
10278
|
-
}
|
|
10279
|
-
var WHEN_DEFAULTED_CHECKS = new Set([
|
|
10280
|
-
"max_size",
|
|
10281
|
-
"min_size",
|
|
10282
|
-
"size_equals",
|
|
10283
|
-
"max_length",
|
|
10284
|
-
"min_length",
|
|
10285
|
-
"length_equals"
|
|
10286
|
-
]);
|
|
10287
|
-
function generateChecks(doc, ctx, schema, accessor) {
|
|
10288
|
-
const schemaChecks = schema._zod.def.checks;
|
|
10289
|
-
if (!schemaChecks || schemaChecks.length === 0)
|
|
10290
|
-
return accessor;
|
|
10291
|
-
let currentAccessor = accessor;
|
|
10292
|
-
for (const check of schemaChecks) {
|
|
10293
|
-
const def = check._zod.def;
|
|
10294
|
-
if (def.when && !WHEN_DEFAULTED_CHECKS.has(def.check)) {
|
|
10295
|
-
throw new ZodCompileUnsupportedError(`check with a custom "when" condition`);
|
|
10296
|
-
}
|
|
10297
|
-
switch (def.check) {
|
|
10298
|
-
case "greater_than":
|
|
10299
|
-
generateGreaterThanCheck(doc, ctx, def, currentAccessor);
|
|
10300
|
-
break;
|
|
10301
|
-
case "less_than":
|
|
10302
|
-
generateLessThanCheck(doc, ctx, def, currentAccessor);
|
|
10303
|
-
break;
|
|
10304
|
-
case "multiple_of":
|
|
10305
|
-
generateMultipleOfCheck(doc, ctx, def, currentAccessor);
|
|
10306
|
-
break;
|
|
10307
|
-
case "number_format":
|
|
10308
|
-
generateNumberFormatCheck(doc, def, currentAccessor);
|
|
10309
|
-
break;
|
|
10310
|
-
case "min_length": {
|
|
10311
|
-
const min = numericOperand(def.minimum, "min_length");
|
|
10312
|
-
const len = codePointLengthVar(doc, ctx, currentAccessor, `${currentAccessor}.length >= ${min} && ${currentAccessor}.length < ${def.minimum * 2}`);
|
|
10313
|
-
doc.write(`if (${len} < ${min}) return INVALID;`);
|
|
10314
|
-
break;
|
|
10315
|
-
}
|
|
10316
|
-
case "max_length": {
|
|
10317
|
-
const max = numericOperand(def.maximum, "max_length");
|
|
10318
|
-
const len = codePointLengthVar(doc, ctx, currentAccessor, `${currentAccessor}.length > ${max}`);
|
|
10319
|
-
doc.write(`if (${len} > ${max}) return INVALID;`);
|
|
10320
|
-
break;
|
|
10321
|
-
}
|
|
10322
|
-
case "length_equals": {
|
|
10323
|
-
const exact = numericOperand(def.length, "length_equals");
|
|
10324
|
-
const len = codePointLengthVar(doc, ctx, currentAccessor, `${currentAccessor}.length >= ${exact} && ${currentAccessor}.length <= ${def.length * 2}`);
|
|
10325
|
-
doc.write(`if (${len} !== ${exact}) return INVALID;`);
|
|
10326
|
-
break;
|
|
10327
|
-
}
|
|
10328
|
-
case "min_size":
|
|
10329
|
-
doc.write(`if (${currentAccessor}.size < ${numericOperand(def.minimum, "min_size")}) return INVALID;`);
|
|
10330
|
-
break;
|
|
10331
|
-
case "max_size":
|
|
10332
|
-
doc.write(`if (${currentAccessor}.size > ${numericOperand(def.maximum, "max_size")}) return INVALID;`);
|
|
10333
|
-
break;
|
|
10334
|
-
case "size_equals":
|
|
10335
|
-
doc.write(`if (${currentAccessor}.size !== ${numericOperand(def.size, "size_equals")}) return INVALID;`);
|
|
10336
|
-
break;
|
|
10337
|
-
case "string_format":
|
|
10338
|
-
currentAccessor = generateStringFormatCheck(doc, ctx, def, currentAccessor);
|
|
10339
|
-
break;
|
|
10340
|
-
case "custom":
|
|
10341
|
-
currentAccessor = generateCustomRefineCheck(doc, ctx, check, currentAccessor);
|
|
10342
|
-
break;
|
|
10343
|
-
case "bigint_format":
|
|
10344
|
-
generateBigIntFormatCheck(doc, def, currentAccessor);
|
|
10345
|
-
break;
|
|
10346
|
-
case "mime_type":
|
|
10347
|
-
generateMimeTypeCheck(doc, ctx, def, currentAccessor);
|
|
10348
|
-
break;
|
|
10349
|
-
case "property":
|
|
10350
|
-
generatePropertyCheck(doc, ctx, def, currentAccessor);
|
|
10351
|
-
break;
|
|
10352
|
-
case "properties":
|
|
10353
|
-
generatePropertiesChecks(doc, ctx, def, currentAccessor);
|
|
10354
|
-
break;
|
|
10355
|
-
case "overwrite": {
|
|
10356
|
-
const newAccessor = newVar(ctx);
|
|
10357
|
-
generateOverwriteCheck(doc, ctx, check, currentAccessor, newAccessor);
|
|
10358
|
-
currentAccessor = newAccessor;
|
|
10359
|
-
break;
|
|
10360
|
-
}
|
|
10361
|
-
default: {
|
|
10362
|
-
throw new ZodCompileUnsupportedError(`check type ${def.check}`);
|
|
10363
|
-
}
|
|
10364
|
-
}
|
|
10365
|
-
}
|
|
10366
|
-
return currentAccessor;
|
|
10367
|
-
}
|
|
10368
|
-
function codePointLengthVar(doc, ctx, accessor, inDoubt) {
|
|
10369
|
-
const cpLen = addConstant(ctx, codePointLength);
|
|
10370
|
-
const v = newVar(ctx);
|
|
10371
|
-
doc.write(`const ${v} = typeof ${accessor} === "string" && ${inDoubt} ? ${cpLen}(${accessor}) : ${accessor}.length;`);
|
|
10372
|
-
return v;
|
|
10373
|
-
}
|
|
10374
|
-
function numericOperand(value, label) {
|
|
10375
|
-
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
10376
|
-
throw new ZodCompileUnsupportedError(`${label} bound of type ${typeof value}`);
|
|
10377
|
-
}
|
|
10378
|
-
return `${value}`;
|
|
10379
|
-
}
|
|
10380
|
-
function comparisonOperand(ctx, value) {
|
|
10381
|
-
if (typeof value === "bigint")
|
|
10382
|
-
return `${value}n`;
|
|
10383
|
-
if (typeof value === "number") {
|
|
10384
|
-
if (Number.isNaN(value))
|
|
10385
|
-
throw new ZodCompileUnsupportedError("comparison check with NaN bound");
|
|
10386
|
-
return `${value}`;
|
|
10387
|
-
}
|
|
10388
|
-
if (value instanceof Date) {
|
|
10389
|
-
if (Number.isNaN(value.getTime())) {
|
|
10390
|
-
throw new ZodCompileUnsupportedError("comparison check with Invalid Date bound");
|
|
10391
|
-
}
|
|
10392
|
-
return addConstant(ctx, value);
|
|
10393
|
-
}
|
|
10394
|
-
throw new ZodCompileUnsupportedError(`comparison check bound of type ${typeof value}`);
|
|
10395
|
-
}
|
|
10396
|
-
function generateGreaterThanCheck(doc, ctx, def, accessor) {
|
|
10397
|
-
const op = def.inclusive ? "<" : "<=";
|
|
10398
|
-
doc.write(`if (${accessor} ${op} ${comparisonOperand(ctx, def.value)}) return INVALID;`);
|
|
10399
|
-
}
|
|
10400
|
-
function generateLessThanCheck(doc, ctx, def, accessor) {
|
|
10401
|
-
const op = def.inclusive ? ">" : ">=";
|
|
10402
|
-
doc.write(`if (${accessor} ${op} ${comparisonOperand(ctx, def.value)}) return INVALID;`);
|
|
10403
|
-
}
|
|
10404
|
-
function generateMultipleOfCheck(doc, ctx, def, accessor) {
|
|
10405
|
-
if (typeof def.value === "bigint") {
|
|
10406
|
-
if (def.value === BigInt(0))
|
|
10407
|
-
throw new ZodCompileUnsupportedError("multiple_of check with a zero divisor");
|
|
10408
|
-
doc.write(`if (${accessor} % ${def.value}n !== 0n) return INVALID;`);
|
|
10409
|
-
} else {
|
|
10410
|
-
const remainder = addConstant(ctx, floatSafeRemainder);
|
|
10411
|
-
doc.write(`if (${remainder}(${accessor}, ${numericOperand(def.value, "multiple_of")}) !== 0) return INVALID;`);
|
|
10412
|
-
}
|
|
10413
|
-
}
|
|
10414
|
-
function generateNumberFormatCheck(doc, def, accessor) {
|
|
10415
|
-
const format = def.format;
|
|
10416
|
-
switch (format) {
|
|
10417
|
-
case "safeint":
|
|
10418
|
-
doc.write(`if (!Number.isSafeInteger(${accessor})) return INVALID;`);
|
|
10419
|
-
break;
|
|
10420
|
-
case "int32":
|
|
10421
|
-
doc.write(`if (!Number.isInteger(${accessor}) || ${accessor} < -2147483648 || ${accessor} > 2147483647) return INVALID;`);
|
|
10422
|
-
break;
|
|
10423
|
-
case "uint32":
|
|
10424
|
-
doc.write(`if (!Number.isInteger(${accessor}) || ${accessor} < 0 || ${accessor} > 4294967295) return INVALID;`);
|
|
10425
|
-
break;
|
|
10426
|
-
case "float32":
|
|
10427
|
-
doc.write(`if (!Number.isFinite(${accessor}) || ${accessor} < -3.4028234663852886e38 || ${accessor} > 3.4028234663852886e38) return INVALID;`);
|
|
10428
|
-
break;
|
|
10429
|
-
case "float64":
|
|
10430
|
-
doc.write(`if (!Number.isFinite(${accessor})) return INVALID;`);
|
|
10431
|
-
break;
|
|
10432
|
-
default: {
|
|
10433
|
-
throw new ZodCompileUnsupportedError(`number format ${format}`);
|
|
10434
|
-
}
|
|
10435
|
-
}
|
|
10436
|
-
}
|
|
10437
|
-
function generateBigIntFormatCheck(doc, def, accessor) {
|
|
10438
|
-
const format = def.format;
|
|
10439
|
-
if (!format)
|
|
10440
|
-
return;
|
|
10441
|
-
switch (format) {
|
|
10442
|
-
case "int64":
|
|
10443
|
-
doc.write(`if (${accessor} < -9223372036854775808n || ${accessor} > 9223372036854775807n) return INVALID;`);
|
|
10444
|
-
break;
|
|
10445
|
-
case "uint64":
|
|
10446
|
-
doc.write(`if (${accessor} < 0n || ${accessor} > 18446744073709551615n) return INVALID;`);
|
|
10447
|
-
break;
|
|
10448
|
-
default: {
|
|
10449
|
-
throw new ZodCompileUnsupportedError(`bigint format ${format}`);
|
|
10450
|
-
}
|
|
10451
|
-
}
|
|
10452
|
-
}
|
|
10453
|
-
function generateMimeTypeCheck(doc, ctx, def, accessor) {
|
|
10454
|
-
const mimeTypes = def.mime;
|
|
10455
|
-
if (mimeTypes && mimeTypes.length > 0) {
|
|
10456
|
-
const mimeSet = addConstant(ctx, new Set(mimeTypes));
|
|
10457
|
-
doc.write(`if (!${mimeSet}.has(${accessor}.type)) return INVALID;`);
|
|
10458
|
-
}
|
|
10459
|
-
}
|
|
10460
|
-
function generatePropertiesChecks(doc, ctx, def, accessor) {
|
|
10461
|
-
if (def.when) {
|
|
10462
|
-
throw new ZodCompileUnsupportedError(`check with a custom "when" condition`);
|
|
10463
|
-
}
|
|
10464
|
-
doc.write(`if (${accessor} == null) return INVALID;`);
|
|
10465
|
-
const shape = def.shape;
|
|
10466
|
-
for (const key of Reflect.ownKeys(shape)) {
|
|
10467
|
-
const keyExpr = typeof key === "symbol" ? addConstant(ctx, key) : esc(key);
|
|
10468
|
-
const inputVar = newVar(ctx);
|
|
10469
|
-
doc.write(`const ${inputVar} = ${accessor}[${keyExpr}];`);
|
|
10470
|
-
compileChild(doc, ctx, shape[key], inputVar, false);
|
|
10471
|
-
}
|
|
10472
|
-
}
|
|
10473
|
-
function generatePropertyCheck(doc, ctx, def, accessor) {
|
|
10474
|
-
const propAccessor = `${accessor}[${JSON.stringify(def.property)}]`;
|
|
10475
|
-
generateCheck(doc, ctx, def.schema, propAccessor);
|
|
10476
|
-
}
|
|
10477
|
-
function generateOverwriteCheck(doc, ctx, check, currentAccessor, newAccessor) {
|
|
10478
|
-
const tx = check._zod.def.tx;
|
|
10479
|
-
if (!tx) {
|
|
10480
|
-
throw new ZodCompileUnsupportedError("overwrite check without a transform function");
|
|
10481
|
-
}
|
|
10482
|
-
if (isAsyncFunction(tx)) {
|
|
10483
|
-
throw new ZodCompileAsyncError("z.compile: async overwrite transforms are not supported");
|
|
10484
|
-
}
|
|
10485
|
-
const txConst = addConstant(ctx, tx);
|
|
10486
|
-
doc.write(`const ${newAccessor} = ${txConst}(${currentAccessor});`);
|
|
10487
|
-
}
|
|
10488
|
-
function throwAsync() {
|
|
10489
|
-
throw new $ZodAsyncError;
|
|
10490
|
-
}
|
|
10491
|
-
function pushIssue(issue) {
|
|
10492
|
-
this.issues.push(issue);
|
|
10493
|
-
}
|
|
10494
|
-
function generateCustomRefineCheck(doc, ctx, check, accessor) {
|
|
10495
|
-
const def = check._zod.def;
|
|
10496
|
-
if (def.fn) {
|
|
10497
|
-
if (isAsyncFunction(def.fn)) {
|
|
10498
|
-
throw new ZodCompileAsyncError("z.compile: async .refine() predicates are not supported");
|
|
10499
|
-
}
|
|
10500
|
-
const fnConst = addUserConstant(ctx, def.fn);
|
|
10501
|
-
const throwAsyncConst = addConstant(ctx, throwAsync);
|
|
10502
|
-
const resVar = newVar(ctx);
|
|
10503
|
-
doc.write(`const ${resVar} = ${fnConst}(${accessor});`);
|
|
10504
|
-
doc.write(`if (${resVar} instanceof Promise) ${throwAsyncConst}();`);
|
|
10505
|
-
doc.write(`if (!${resVar}) return INVALID;`);
|
|
10506
|
-
return accessor;
|
|
10507
|
-
}
|
|
10508
|
-
if (check._zod.check) {
|
|
10509
|
-
if (isAsyncFunction(check._zod.check)) {
|
|
10510
|
-
throw new ZodCompileAsyncError("z.compile: async .superRefine() / check functions are not supported");
|
|
10511
|
-
}
|
|
10512
|
-
const checkFn = check._zod.check;
|
|
10513
|
-
const helperFn = (value) => {
|
|
10514
|
-
const fakePayload = { value, issues: [], addIssue: pushIssue };
|
|
10515
|
-
const result = checkFn(fakePayload);
|
|
10516
|
-
if (result instanceof Promise)
|
|
10517
|
-
throwAsync();
|
|
10518
|
-
return fakePayload.issues.length === 0 ? fakePayload.value : INVALID;
|
|
10519
|
-
};
|
|
10520
|
-
const helperConst = addUserConstant(ctx, helperFn);
|
|
10521
|
-
const outVar = newVar(ctx);
|
|
10522
|
-
doc.write(`const ${outVar} = ${helperConst}(${accessor});`);
|
|
10523
|
-
doc.write(`if (${outVar} === INVALID) return INVALID;`);
|
|
10524
|
-
return outVar;
|
|
10525
|
-
}
|
|
10526
|
-
throw new ZodCompileUnsupportedError("custom check without a predicate or check function");
|
|
10527
|
-
}
|
|
10528
|
-
var PATTERN_IS_COMPLETE = new Set([
|
|
10529
|
-
"cidrv4",
|
|
10530
|
-
"cuid",
|
|
10531
|
-
"cuid2",
|
|
10532
|
-
"date",
|
|
10533
|
-
"datetime",
|
|
10534
|
-
"duration",
|
|
10535
|
-
"e164",
|
|
10536
|
-
"email",
|
|
10537
|
-
"emoji",
|
|
10538
|
-
"ends_with",
|
|
10539
|
-
"guid",
|
|
10540
|
-
"includes",
|
|
10541
|
-
"ipv4",
|
|
10542
|
-
"ksuid",
|
|
10543
|
-
"lowercase",
|
|
10544
|
-
"mac",
|
|
10545
|
-
"nanoid",
|
|
10546
|
-
"regex",
|
|
10547
|
-
"starts_with",
|
|
10548
|
-
"time",
|
|
10549
|
-
"ulid",
|
|
10550
|
-
"uppercase",
|
|
10551
|
-
"uuid",
|
|
10552
|
-
"xid"
|
|
10553
|
-
]);
|
|
10554
|
-
function generateStringFormatCheck(doc, ctx, def, accessor, needsValue = true) {
|
|
10555
|
-
const fmt = def.format;
|
|
10556
|
-
if (fmt === "base64") {
|
|
10557
|
-
const validator = addConstant(ctx, isValidBase64);
|
|
10558
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
10559
|
-
return accessor;
|
|
10560
|
-
}
|
|
10561
|
-
if (fmt === "base64url") {
|
|
10562
|
-
const validator = addConstant(ctx, isValidBase64URL);
|
|
10563
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
10564
|
-
return accessor;
|
|
10565
|
-
}
|
|
10566
|
-
if (fmt === "jwt") {
|
|
10567
|
-
const validator = addConstant(ctx, isValidJWT);
|
|
10568
|
-
const alg = addConstant(ctx, def.alg ?? null);
|
|
10569
|
-
doc.write(`if (!${validator}(${accessor}, ${alg})) return INVALID;`);
|
|
10570
|
-
return accessor;
|
|
10571
|
-
}
|
|
10572
|
-
if (fmt === "ipv6") {
|
|
10573
|
-
const validator = addConstant(ctx, isValidIPv6);
|
|
10574
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
10575
|
-
return accessor;
|
|
10576
|
-
}
|
|
10577
|
-
if (fmt === "cidrv6") {
|
|
10578
|
-
const validator = addConstant(ctx, isValidCIDRv6);
|
|
10579
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
10580
|
-
return accessor;
|
|
10581
|
-
}
|
|
10582
|
-
if (fmt === "credit_card") {
|
|
10583
|
-
const validator = addConstant(ctx, isValidCreditCard);
|
|
10584
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
10585
|
-
return accessor;
|
|
10586
|
-
}
|
|
10587
|
-
if (fmt === "iban") {
|
|
10588
|
-
const validator = addConstant(ctx, isValidIBAN);
|
|
10589
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
10590
|
-
return accessor;
|
|
10591
|
-
}
|
|
10592
|
-
const formatDef = def;
|
|
10593
|
-
if (fmt === "url" || fmt === "httpurl" || formatDef.normalize || formatDef.hostname !== undefined || formatDef.protocol !== undefined) {
|
|
10594
|
-
const parseConst = addConstant(ctx, validateURL);
|
|
10595
|
-
const defConst = addConstant(ctx, def);
|
|
10596
|
-
const trimVar = newVar(ctx);
|
|
10597
|
-
const urlVar = newVar(ctx);
|
|
10598
|
-
doc.write(`const ${trimVar} = ${accessor}.trim();`);
|
|
10599
|
-
doc.write(`const ${urlVar} = ${parseConst}(${trimVar}, ${defConst});`);
|
|
10600
|
-
doc.write(`if (typeof ${urlVar} === "number") return INVALID;`);
|
|
10601
|
-
if (formatDef.hostname !== undefined) {
|
|
10602
|
-
const hostnameConst = addConstant(ctx, urlHostnameOk);
|
|
10603
|
-
doc.write(`if (!${hostnameConst}(${urlVar}, ${defConst}.hostname)) return INVALID;`);
|
|
10604
|
-
}
|
|
10605
|
-
if (formatDef.protocol !== undefined) {
|
|
10606
|
-
const protocolConst = addConstant(ctx, urlProtocolOk);
|
|
10607
|
-
doc.write(`if (!${protocolConst}(${urlVar}, ${defConst}.protocol)) return INVALID;`);
|
|
10608
|
-
}
|
|
10609
|
-
if (!needsValue)
|
|
10610
|
-
return null;
|
|
10611
|
-
const outputVar = newVar(ctx);
|
|
10612
|
-
const outputExpr = formatDef.normalize ? `${urlVar}.href` : `${addConstant(ctx, stripTabAndNewline)}(${trimVar})`;
|
|
10613
|
-
doc.write(`const ${outputVar} = ${outputExpr};`);
|
|
10614
|
-
return outputVar;
|
|
10615
|
-
}
|
|
10616
|
-
const customFn = def.fn;
|
|
10617
|
-
if (customFn) {
|
|
10618
|
-
if (isAsyncFunction(customFn))
|
|
10619
|
-
throw new ZodCompileUnsupportedError(`async string format ${fmt}`);
|
|
10620
|
-
const fnConst = addConstant(ctx, customFn);
|
|
10621
|
-
doc.write(`if (!${fnConst}(${accessor})) return INVALID;`);
|
|
10622
|
-
return accessor;
|
|
10623
|
-
}
|
|
10624
|
-
if (PATTERN_IS_COMPLETE.has(fmt) && def.pattern) {
|
|
10625
|
-
const patternConst = addConstant(ctx, def.pattern);
|
|
10626
|
-
doc.write(`${patternConst}.lastIndex = 0;`);
|
|
10627
|
-
doc.write(`if (!${patternConst}.test(${accessor})) return INVALID;`);
|
|
10628
|
-
return accessor;
|
|
10629
|
-
}
|
|
10630
|
-
const format = def.format;
|
|
10631
|
-
switch (format) {
|
|
10632
|
-
case "regex":
|
|
10633
|
-
throw new ZodCompileUnsupportedError("regex format without a pattern");
|
|
10634
|
-
case "lowercase":
|
|
10635
|
-
doc.write(`if (${accessor} !== ${accessor}.toLowerCase()) return INVALID;`);
|
|
10636
|
-
break;
|
|
10637
|
-
case "uppercase":
|
|
10638
|
-
doc.write(`if (${accessor} !== ${accessor}.toUpperCase()) return INVALID;`);
|
|
10639
|
-
break;
|
|
10640
|
-
case "includes":
|
|
10641
|
-
doc.write(`if (!${accessor}.includes(${esc(def.includes)})) return INVALID;`);
|
|
10642
|
-
break;
|
|
10643
|
-
case "starts_with": {
|
|
10644
|
-
const prefix = def.prefix;
|
|
10645
|
-
doc.write(`if (${accessor}.slice(0, ${prefix.length}) !== ${esc(prefix)}) return INVALID;`);
|
|
10646
|
-
break;
|
|
10647
|
-
}
|
|
10648
|
-
case "ends_with": {
|
|
10649
|
-
const suffix = def.suffix;
|
|
10650
|
-
doc.write(`if (${accessor}.slice(-${suffix.length}) !== ${esc(suffix)}) return INVALID;`);
|
|
10651
|
-
break;
|
|
10652
|
-
}
|
|
10653
|
-
default: {
|
|
10654
|
-
throw new ZodCompileUnsupportedError(`string format ${format}`);
|
|
10655
|
-
}
|
|
10656
|
-
}
|
|
10657
|
-
return accessor;
|
|
10658
|
-
}
|
|
10659
|
-
function generateCheck(doc, ctx, schema, accessor, needsValue = true) {
|
|
10660
|
-
const def = schema._zod.def;
|
|
10661
|
-
const type = def.type;
|
|
10662
|
-
if (def.coerce) {
|
|
10663
|
-
throw new ZodCompileUnsupportedError(`coercion (z.coerce.${type}())`);
|
|
10664
|
-
}
|
|
10665
|
-
const buildsValue = needsValue || !!def.checks?.length;
|
|
10666
|
-
let typeAccessor;
|
|
10667
|
-
switch (type) {
|
|
10668
|
-
case "string":
|
|
10669
|
-
typeAccessor = generateStringCheck(doc, ctx, schema, accessor, buildsValue);
|
|
10670
|
-
break;
|
|
10671
|
-
case "number":
|
|
10672
|
-
typeAccessor = generateNumberCheck(doc, schema, accessor);
|
|
10673
|
-
break;
|
|
10674
|
-
case "boolean":
|
|
10675
|
-
typeAccessor = generateBooleanCheck(doc, accessor);
|
|
10676
|
-
break;
|
|
10677
|
-
case "bigint":
|
|
10678
|
-
typeAccessor = generateBigIntCheck(doc, schema, accessor);
|
|
10679
|
-
break;
|
|
10680
|
-
case "symbol":
|
|
10681
|
-
typeAccessor = generateSymbolCheck(doc, accessor);
|
|
10682
|
-
break;
|
|
10683
|
-
case "undefined":
|
|
10684
|
-
typeAccessor = generateUndefinedCheck(doc, accessor);
|
|
10685
|
-
break;
|
|
10686
|
-
case "null":
|
|
10687
|
-
typeAccessor = generateNullCheck(doc, accessor);
|
|
10688
|
-
break;
|
|
10689
|
-
case "any":
|
|
10690
|
-
case "unknown":
|
|
10691
|
-
typeAccessor = accessor;
|
|
10692
|
-
break;
|
|
10693
|
-
case "never":
|
|
10694
|
-
doc.write("return INVALID;");
|
|
10695
|
-
typeAccessor = accessor;
|
|
10696
|
-
break;
|
|
10697
|
-
case "void":
|
|
10698
|
-
typeAccessor = generateVoidCheck(doc, accessor);
|
|
10699
|
-
break;
|
|
10700
|
-
case "nan":
|
|
10701
|
-
typeAccessor = generateNaNCheck(doc, accessor);
|
|
10702
|
-
break;
|
|
10703
|
-
case "date":
|
|
10704
|
-
typeAccessor = generateDateCheck(doc, accessor);
|
|
10705
|
-
break;
|
|
10706
|
-
case "object":
|
|
10707
|
-
typeAccessor = generateObjectCheck(doc, ctx, schema, accessor, buildsValue);
|
|
10708
|
-
break;
|
|
10709
|
-
case "optional":
|
|
10710
|
-
typeAccessor = generateOptionalCheck(doc, ctx, schema, accessor, buildsValue);
|
|
10711
|
-
break;
|
|
10712
|
-
case "nullable":
|
|
10713
|
-
typeAccessor = generateNullableCheck(doc, ctx, schema, accessor, buildsValue);
|
|
10714
|
-
break;
|
|
10715
|
-
case "array":
|
|
10716
|
-
typeAccessor = generateArrayCheck(doc, ctx, schema, accessor, buildsValue);
|
|
10717
|
-
break;
|
|
10718
|
-
case "literal":
|
|
10719
|
-
typeAccessor = generateLiteralCheck(doc, ctx, schema, accessor);
|
|
10720
|
-
break;
|
|
10721
|
-
case "enum":
|
|
10722
|
-
typeAccessor = generateEnumCheck(doc, ctx, schema, accessor);
|
|
10723
|
-
break;
|
|
10724
|
-
case "readonly": {
|
|
10725
|
-
const innerOut = generateWrapperCheck(doc, ctx, schema, accessor);
|
|
10726
|
-
const frozenVar = newVar(ctx);
|
|
10727
|
-
doc.write(`const ${frozenVar} = Object.freeze(${innerOut});`);
|
|
10728
|
-
typeAccessor = frozenVar;
|
|
10729
|
-
break;
|
|
10730
|
-
}
|
|
10731
|
-
case "success":
|
|
10732
|
-
generateWrapperCheck(doc, ctx, schema, accessor);
|
|
10733
|
-
typeAccessor = "true";
|
|
10734
|
-
break;
|
|
10735
|
-
case "default":
|
|
10736
|
-
case "prefault":
|
|
10737
|
-
typeAccessor = generateDefaultCheck(doc, ctx, schema, accessor);
|
|
10738
|
-
break;
|
|
10739
|
-
case "nonoptional":
|
|
10740
|
-
typeAccessor = generateNonOptionalCheck(doc, ctx, schema, accessor);
|
|
10741
|
-
break;
|
|
10742
|
-
case "tuple":
|
|
10743
|
-
typeAccessor = generateTupleCheck(doc, ctx, schema, accessor);
|
|
10744
|
-
break;
|
|
10745
|
-
case "union":
|
|
10746
|
-
typeAccessor = generateUnionCheck(doc, ctx, schema, accessor);
|
|
10747
|
-
break;
|
|
10748
|
-
case "intersection":
|
|
10749
|
-
typeAccessor = generateIntersectionCheck(doc, ctx, schema, accessor);
|
|
10750
|
-
break;
|
|
10751
|
-
case "record":
|
|
10752
|
-
typeAccessor = generateRecordCheck(doc, ctx, schema, accessor);
|
|
10753
|
-
break;
|
|
10754
|
-
case "map":
|
|
10755
|
-
typeAccessor = generateMapCheck(doc, ctx, schema, accessor);
|
|
10756
|
-
break;
|
|
10757
|
-
case "set":
|
|
10758
|
-
typeAccessor = generateSetCheck(doc, ctx, schema, accessor);
|
|
10759
|
-
break;
|
|
10760
|
-
case "file":
|
|
10761
|
-
typeAccessor = generateFileCheck(doc, accessor);
|
|
10762
|
-
break;
|
|
10763
|
-
case "template_literal":
|
|
10764
|
-
typeAccessor = generateTemplateLiteralCheck(doc, ctx, schema, accessor);
|
|
10765
|
-
break;
|
|
10766
|
-
case "lazy":
|
|
10767
|
-
typeAccessor = generateLazyCheck(doc, ctx, schema, accessor);
|
|
10768
|
-
break;
|
|
10769
|
-
case "pipe":
|
|
10770
|
-
typeAccessor = generatePipeCheck(doc, ctx, schema, accessor);
|
|
10771
|
-
break;
|
|
10772
|
-
case "custom":
|
|
10773
|
-
typeAccessor = generateCustomCheck(doc, ctx, schema, accessor);
|
|
10774
|
-
break;
|
|
10775
|
-
case "transform":
|
|
10776
|
-
typeAccessor = generateTransformCheck(doc, ctx, schema, accessor);
|
|
10777
|
-
break;
|
|
10778
|
-
case "catch":
|
|
10779
|
-
typeAccessor = generateCatchCheck(doc, ctx, schema, accessor);
|
|
10780
|
-
break;
|
|
10781
|
-
default: {
|
|
10782
|
-
throw new ZodCompileUnsupportedError(`schema type ${type}`);
|
|
10783
|
-
}
|
|
10784
|
-
}
|
|
10785
|
-
if (typeAccessor === null)
|
|
10786
|
-
return null;
|
|
10787
|
-
return generateChecks(doc, ctx, schema, typeAccessor);
|
|
10788
|
-
}
|
|
10789
|
-
function generateStringCheck(doc, ctx, schema, accessor, needsValue = true) {
|
|
10790
|
-
doc.write(`if (typeof ${accessor} !== "string") return INVALID;`);
|
|
10791
|
-
const def = schema._zod.def;
|
|
10792
|
-
if (def.format === undefined)
|
|
10793
|
-
return accessor;
|
|
10794
|
-
return generateStringFormatCheck(doc, ctx, def, accessor, needsValue);
|
|
10795
|
-
}
|
|
10796
|
-
function generateNumberCheck(doc, schema, accessor) {
|
|
10797
|
-
doc.write(`if (typeof ${accessor} !== "number" || !Number.isFinite(${accessor})) return INVALID;`);
|
|
10798
|
-
const def = schema._zod.def;
|
|
10799
|
-
if (def.check === "number_format" && def.format) {
|
|
10800
|
-
generateNumberFormatCheck(doc, { format: def.format }, accessor);
|
|
10801
|
-
}
|
|
10802
|
-
return accessor;
|
|
10803
|
-
}
|
|
10804
|
-
function generateBooleanCheck(doc, accessor) {
|
|
10805
|
-
doc.write(`if (typeof ${accessor} !== "boolean") return INVALID;`);
|
|
10806
|
-
return accessor;
|
|
10807
|
-
}
|
|
10808
|
-
function generateBigIntCheck(doc, schema, accessor) {
|
|
10809
|
-
doc.write(`if (typeof ${accessor} !== "bigint") return INVALID;`);
|
|
10810
|
-
const def = schema._zod.def;
|
|
10811
|
-
if (def.format) {
|
|
10812
|
-
switch (def.format) {
|
|
10813
|
-
case "int64":
|
|
10814
|
-
doc.write(`if (${accessor} < -9223372036854775808n || ${accessor} > 9223372036854775807n) return INVALID;`);
|
|
10815
|
-
break;
|
|
10816
|
-
case "uint64":
|
|
10817
|
-
doc.write(`if (${accessor} < 0n || ${accessor} > 18446744073709551615n) return INVALID;`);
|
|
10818
|
-
break;
|
|
10819
|
-
}
|
|
10820
|
-
}
|
|
10821
|
-
return accessor;
|
|
10822
|
-
}
|
|
10823
|
-
function generateSymbolCheck(doc, accessor) {
|
|
10824
|
-
doc.write(`if (typeof ${accessor} !== "symbol") return INVALID;`);
|
|
10825
|
-
return accessor;
|
|
10826
|
-
}
|
|
10827
|
-
function generateUndefinedCheck(doc, accessor) {
|
|
10828
|
-
doc.write(`if (${accessor} !== undefined) return INVALID;`);
|
|
10829
|
-
return accessor;
|
|
10830
|
-
}
|
|
10831
|
-
function generateNullCheck(doc, accessor) {
|
|
10832
|
-
doc.write(`if (${accessor} !== null) return INVALID;`);
|
|
10833
|
-
return accessor;
|
|
10834
|
-
}
|
|
10835
|
-
function generateVoidCheck(doc, accessor) {
|
|
10836
|
-
doc.write(`if (${accessor} !== undefined) return INVALID;`);
|
|
10837
|
-
return accessor;
|
|
10838
|
-
}
|
|
10839
|
-
function generateNaNCheck(doc, accessor) {
|
|
10840
|
-
doc.write(`if (typeof ${accessor} !== "number" || !Number.isNaN(${accessor})) return INVALID;`);
|
|
10841
|
-
return accessor;
|
|
10842
|
-
}
|
|
10843
|
-
function generateDateCheck(doc, accessor) {
|
|
10844
|
-
doc.write(`if (!(${accessor} instanceof Date) || Number.isNaN(${accessor}.getTime())) return INVALID;`);
|
|
10845
|
-
return accessor;
|
|
10846
|
-
}
|
|
10847
|
-
function generateObjectCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
10848
|
-
const def = schema._zod.def;
|
|
10849
|
-
doc.write(`if (typeof ${accessor} !== "object" || ${accessor} === null || Array.isArray(${accessor})) return INVALID;`);
|
|
10850
|
-
const shape = def.shape;
|
|
10851
|
-
const keys = Object.keys(shape);
|
|
10852
|
-
const symbolKeys = Object.getOwnPropertySymbols(shape);
|
|
10853
|
-
const allKeys = symbolKeys.length ? [...keys, ...symbolKeys] : keys;
|
|
10854
|
-
const keyExpr = (k) => typeof k === "symbol" ? addConstant(ctx, k) : esc(k);
|
|
10855
|
-
const propKey = (k) => typeof k === "symbol" ? `[${keyExpr(k)}]` : esc(k);
|
|
10856
|
-
const propShape = shape;
|
|
10857
|
-
if (keys.includes("__proto__")) {
|
|
10858
|
-
throw new ZodCompileUnsupportedError('object shape key "__proto__"');
|
|
10859
|
-
}
|
|
10860
|
-
const propOutputs = new Map;
|
|
10861
|
-
for (const key of allKeys) {
|
|
10862
|
-
const propSchema = propShape[key];
|
|
10863
|
-
const kx = keyExpr(key);
|
|
10864
|
-
const inputVar = newVar(ctx);
|
|
10865
|
-
doc.write(`const ${inputVar} = ${accessor}[${kx}];`);
|
|
10866
|
-
if (propSchema._zod.optin !== undefined) {
|
|
10867
|
-
const outputVar = newVar(ctx);
|
|
10868
|
-
doc.write(`let ${outputVar} = (() => {`);
|
|
10869
|
-
doc.indented((d) => {
|
|
10870
|
-
const outputAccessor = compileChild(d, ctx, propSchema, inputVar);
|
|
10871
|
-
d.write(`return ${outputAccessor};`);
|
|
10872
|
-
});
|
|
10873
|
-
doc.write(`})();`);
|
|
10874
|
-
if (propSchema._zod.optout === "optional") {
|
|
10875
|
-
doc.write(`if (${outputVar} === INVALID) {`);
|
|
10876
|
-
doc.indented((d) => {
|
|
10877
|
-
d.write(`if (${kx} in ${accessor}) return INVALID;`);
|
|
10878
|
-
d.write(`${outputVar} = undefined;`);
|
|
10879
|
-
});
|
|
10880
|
-
doc.write(`}`);
|
|
10881
|
-
} else {
|
|
10882
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
10883
|
-
}
|
|
10884
|
-
propOutputs.set(key, outputVar);
|
|
10885
|
-
} else {
|
|
10886
|
-
if (requiresPresenceCheck(propSchema)) {
|
|
10887
|
-
doc.write(`if (!(${kx} in ${accessor})) return INVALID;`);
|
|
10888
|
-
}
|
|
10889
|
-
const outputAccessor = compileChild(doc, ctx, propSchema, inputVar, buildsValue);
|
|
10890
|
-
if (outputAccessor !== null)
|
|
10891
|
-
propOutputs.set(key, outputAccessor);
|
|
10892
|
-
}
|
|
10893
|
-
}
|
|
10894
|
-
const catchall = def.catchall;
|
|
10895
|
-
let unknownKeysMode = "none";
|
|
10896
|
-
if (catchall) {
|
|
10897
|
-
const catchallType = catchall._zod.def.type;
|
|
10898
|
-
if (catchallType === "never") {
|
|
10899
|
-
const condition = keys.map((k) => `k !== ${esc(k)}`).join(" && ") || "true";
|
|
10900
|
-
doc.write(`for (const k in ${accessor}) {`);
|
|
10901
|
-
doc.indented((d) => {
|
|
10902
|
-
d.write(`if (${condition}) return INVALID;`);
|
|
10903
|
-
});
|
|
10904
|
-
doc.write(`}`);
|
|
10905
|
-
} else if ((catchallType === "unknown" || catchallType === "any") && !catchall._zod.def.checks?.length) {
|
|
10906
|
-
unknownKeysMode = "passthrough";
|
|
10907
|
-
} else {
|
|
10908
|
-
unknownKeysMode = "schema";
|
|
10909
|
-
}
|
|
10910
|
-
}
|
|
10911
|
-
const outputVar = newVar(ctx);
|
|
10912
|
-
const hasConditionalKeys = allKeys.some((k) => mayOmitUndefined(propShape[k]) || dropsWhenAbsent(propShape[k]));
|
|
10913
|
-
if (!buildsValue) {
|
|
10914
|
-
if (unknownKeysMode === "schema") {
|
|
10915
|
-
const knownSet = keys.length > 0 ? addConstant(ctx, new Set(keys)) : null;
|
|
10916
|
-
doc.write(`for (const k in ${accessor}) {`);
|
|
10917
|
-
doc.indented((d) => {
|
|
10918
|
-
d.write(`if (k === "__proto__") continue;`);
|
|
10919
|
-
if (knownSet)
|
|
10920
|
-
d.write(`if (${knownSet}.has(k)) continue;`);
|
|
10921
|
-
const valVar = newVar(ctx);
|
|
10922
|
-
d.write(`const ${valVar} = ${accessor}[k];`);
|
|
10923
|
-
compileChild(d, ctx, catchall, valVar, false);
|
|
10924
|
-
});
|
|
10925
|
-
doc.write(`}`);
|
|
10926
|
-
}
|
|
10927
|
-
return null;
|
|
10928
|
-
}
|
|
10929
|
-
if (!hasConditionalKeys) {
|
|
10930
|
-
const propLiterals = allKeys.map((k) => `${propKey(k)}: ${propOutputs.get(k)}`).join(", ");
|
|
10931
|
-
doc.write(`const ${outputVar} = { ${propLiterals} };`);
|
|
10932
|
-
} else {
|
|
10933
|
-
doc.write(`const ${outputVar} = {};`);
|
|
10934
|
-
for (const k of allKeys) {
|
|
10935
|
-
const kx = keyExpr(k);
|
|
10936
|
-
const out = propOutputs.get(k);
|
|
10937
|
-
if (dropsWhenAbsent(propShape[k])) {
|
|
10938
|
-
doc.write(`if (${kx} in ${accessor}) ${outputVar}[${kx}] = ${out};`);
|
|
10939
|
-
} else if (mayOmitUndefined(propShape[k])) {
|
|
10940
|
-
doc.write(`if (${out} !== undefined || ${kx} in ${accessor}) ${outputVar}[${kx}] = ${out};`);
|
|
10941
|
-
} else {
|
|
10942
|
-
doc.write(`${outputVar}[${kx}] = ${out};`);
|
|
10943
|
-
}
|
|
10944
|
-
}
|
|
10945
|
-
}
|
|
10946
|
-
if (unknownKeysMode !== "none") {
|
|
10947
|
-
const knownSet = keys.length > 0 ? addConstant(ctx, new Set(keys)) : null;
|
|
10948
|
-
doc.write(`for (const k in ${accessor}) {`);
|
|
10949
|
-
doc.indented((d) => {
|
|
10950
|
-
d.write(`if (k === "__proto__") continue;`);
|
|
10951
|
-
if (knownSet)
|
|
10952
|
-
d.write(`if (${knownSet}.has(k)) continue;`);
|
|
10953
|
-
if (unknownKeysMode === "passthrough") {
|
|
10954
|
-
d.write(`${outputVar}[k] = ${accessor}[k];`);
|
|
10955
|
-
} else {
|
|
10956
|
-
const valVar = newVar(ctx);
|
|
10957
|
-
d.write(`const ${valVar} = ${accessor}[k];`);
|
|
10958
|
-
const catchallOut = compileChild(d, ctx, catchall, valVar);
|
|
10959
|
-
d.write(`${outputVar}[k] = ${catchallOut};`);
|
|
10960
|
-
}
|
|
10961
|
-
});
|
|
10962
|
-
doc.write(`}`);
|
|
10963
|
-
}
|
|
10964
|
-
return outputVar;
|
|
10965
|
-
}
|
|
10966
|
-
function generateOptionalCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
10967
|
-
const def = schema._zod.def;
|
|
10968
|
-
if (isExactOptional(schema)) {
|
|
10969
|
-
return generateCheck(doc, ctx, def.innerType, accessor, buildsValue);
|
|
10970
|
-
}
|
|
10971
|
-
if (def.innerType._zod.optin === "defaulted") {
|
|
10972
|
-
const outputVar = newVar(ctx);
|
|
10973
|
-
const branchVar = newVar(ctx);
|
|
10974
|
-
doc.write(`let ${outputVar};`);
|
|
10975
|
-
doc.write(`if (${accessor} === undefined) {`);
|
|
10976
|
-
doc.indented((d) => {
|
|
10977
|
-
d.write(`const ${branchVar} = (() => {`);
|
|
10978
|
-
d.indented((d2) => {
|
|
10979
|
-
const innerOutput = generateCheck(d2, ctx, def.innerType, accessor);
|
|
10980
|
-
d2.write(`return ${innerOutput};`);
|
|
10981
|
-
});
|
|
10982
|
-
d.write(`})();`);
|
|
10983
|
-
d.write(`if (${branchVar} !== INVALID) ${outputVar} = ${branchVar};`);
|
|
10984
|
-
});
|
|
10985
|
-
doc.write(`} else {`);
|
|
10986
|
-
doc.indented((d) => {
|
|
10987
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor);
|
|
10988
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
10989
|
-
});
|
|
10990
|
-
doc.write(`}`);
|
|
10991
|
-
return outputVar;
|
|
10992
|
-
}
|
|
10993
|
-
const outputVar = buildsValue ? newVar(ctx) : null;
|
|
10994
|
-
if (outputVar)
|
|
10995
|
-
doc.write(`let ${outputVar};`);
|
|
10996
|
-
doc.write(`if (${accessor} !== undefined) {`);
|
|
10997
|
-
doc.indented((d) => {
|
|
10998
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor, buildsValue);
|
|
10999
|
-
if (outputVar && innerOutput !== null)
|
|
11000
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
11001
|
-
});
|
|
11002
|
-
doc.write(`}`);
|
|
11003
|
-
return outputVar;
|
|
11004
|
-
}
|
|
11005
|
-
function isExactOptional(schema) {
|
|
11006
|
-
return schema._zod.traits?.has("$ZodExactOptional") === true;
|
|
11007
|
-
}
|
|
11008
|
-
function requiresPresenceCheck(schema) {
|
|
11009
|
-
return schema._zod.optin === undefined && fastPathAcceptsAbsence(schema);
|
|
11010
|
-
}
|
|
11011
|
-
function fastPathAcceptsAbsence(schema) {
|
|
11012
|
-
if (schema._zod.def.coerce)
|
|
11013
|
-
return true;
|
|
11014
|
-
const def = schema._zod.def;
|
|
11015
|
-
switch (def.type) {
|
|
11016
|
-
case "any":
|
|
11017
|
-
case "unknown":
|
|
11018
|
-
case "undefined":
|
|
11019
|
-
case "void":
|
|
11020
|
-
case "default":
|
|
11021
|
-
case "prefault":
|
|
11022
|
-
case "transform":
|
|
11023
|
-
case "custom":
|
|
11024
|
-
case "lazy":
|
|
11025
|
-
return true;
|
|
11026
|
-
case "string":
|
|
11027
|
-
case "number":
|
|
11028
|
-
case "boolean":
|
|
11029
|
-
case "bigint":
|
|
11030
|
-
case "symbol":
|
|
11031
|
-
case "null":
|
|
11032
|
-
case "never":
|
|
11033
|
-
case "nan":
|
|
11034
|
-
case "date":
|
|
11035
|
-
case "object":
|
|
11036
|
-
case "array":
|
|
11037
|
-
case "tuple":
|
|
11038
|
-
case "record":
|
|
11039
|
-
case "map":
|
|
11040
|
-
case "set":
|
|
11041
|
-
case "file":
|
|
11042
|
-
case "template_literal":
|
|
11043
|
-
return false;
|
|
11044
|
-
case "nonoptional":
|
|
11045
|
-
return def.innerType ? fastPathAcceptsAbsence(def.innerType) : false;
|
|
11046
|
-
case "literal":
|
|
11047
|
-
return !!def.values?.includes(undefined);
|
|
11048
|
-
case "enum":
|
|
11049
|
-
return !!schema._zod.values?.has(undefined);
|
|
11050
|
-
case "optional":
|
|
11051
|
-
case "nullable":
|
|
11052
|
-
case "readonly":
|
|
11053
|
-
case "success":
|
|
11054
|
-
return def.innerType ? fastPathAcceptsAbsence(def.innerType) : true;
|
|
11055
|
-
case "catch":
|
|
11056
|
-
return true;
|
|
11057
|
-
case "union":
|
|
11058
|
-
return def.options ? def.options.some(fastPathAcceptsAbsence) : true;
|
|
11059
|
-
case "intersection":
|
|
11060
|
-
if (!def.left || !def.right)
|
|
11061
|
-
return true;
|
|
11062
|
-
return fastPathAcceptsAbsence(def.left) && fastPathAcceptsAbsence(def.right);
|
|
11063
|
-
case "pipe":
|
|
11064
|
-
return def.in ? fastPathAcceptsAbsence(def.in) : true;
|
|
11065
|
-
default:
|
|
11066
|
-
return true;
|
|
11067
|
-
}
|
|
11068
|
-
}
|
|
11069
|
-
function dropsWhenAbsent(schema) {
|
|
11070
|
-
return schema._zod.optin === "optional" && schema._zod.optout === "optional";
|
|
11071
|
-
}
|
|
11072
|
-
function mayOmitUndefined(schema) {
|
|
11073
|
-
return (schema._zod.optin !== "defaulted" || schema._zod.optout === "optional") && mayOutputUndefined(schema);
|
|
11074
|
-
}
|
|
11075
|
-
function mayOutputUndefined(schema) {
|
|
11076
|
-
const def = schema._zod.def;
|
|
11077
|
-
switch (def.type) {
|
|
11078
|
-
case "string":
|
|
11079
|
-
case "number":
|
|
11080
|
-
case "boolean":
|
|
11081
|
-
case "bigint":
|
|
11082
|
-
case "symbol":
|
|
11083
|
-
case "null":
|
|
11084
|
-
case "nan":
|
|
11085
|
-
case "date":
|
|
11086
|
-
case "object":
|
|
11087
|
-
case "array":
|
|
11088
|
-
case "tuple":
|
|
11089
|
-
case "record":
|
|
11090
|
-
case "map":
|
|
11091
|
-
case "set":
|
|
11092
|
-
case "file":
|
|
11093
|
-
case "template_literal":
|
|
11094
|
-
case "never":
|
|
11095
|
-
case "success":
|
|
11096
|
-
return false;
|
|
11097
|
-
case "literal":
|
|
11098
|
-
return !!def.values?.includes(undefined);
|
|
11099
|
-
case "enum":
|
|
11100
|
-
return !!schema._zod.values?.has(undefined);
|
|
11101
|
-
case "optional":
|
|
11102
|
-
return true;
|
|
11103
|
-
case "nullable":
|
|
11104
|
-
case "readonly":
|
|
11105
|
-
case "nonoptional":
|
|
11106
|
-
return def.innerType ? mayOutputUndefined(def.innerType) : true;
|
|
11107
|
-
case "union":
|
|
11108
|
-
return def.options ? def.options.some(mayOutputUndefined) : true;
|
|
11109
|
-
case "intersection":
|
|
11110
|
-
return !def.left || !def.right || mayOutputUndefined(def.left) || mayOutputUndefined(def.right);
|
|
11111
|
-
case "pipe":
|
|
11112
|
-
return def.out ? mayOutputUndefined(def.out) : true;
|
|
11113
|
-
default:
|
|
11114
|
-
return true;
|
|
11115
|
-
}
|
|
11116
|
-
}
|
|
11117
|
-
function generateNullableCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
11118
|
-
const def = schema._zod.def;
|
|
11119
|
-
const outputVar = buildsValue ? newVar(ctx) : null;
|
|
11120
|
-
if (outputVar)
|
|
11121
|
-
doc.write(`let ${outputVar} = null;`);
|
|
11122
|
-
doc.write(`if (${accessor} !== null) {`);
|
|
11123
|
-
doc.indented((d) => {
|
|
11124
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor, buildsValue);
|
|
11125
|
-
if (outputVar && innerOutput !== null)
|
|
11126
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
11127
|
-
});
|
|
11128
|
-
doc.write(`}`);
|
|
11129
|
-
return outputVar;
|
|
11130
|
-
}
|
|
11131
|
-
function generateArrayCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
11132
|
-
const def = schema._zod.def;
|
|
11133
|
-
doc.write(`if (!Array.isArray(${accessor})) return INVALID;`);
|
|
11134
|
-
const outputVar = buildsValue ? newVar(ctx) : null;
|
|
11135
|
-
const iVar = newVar(ctx);
|
|
11136
|
-
const elemVar = newVar(ctx);
|
|
11137
|
-
if (outputVar)
|
|
11138
|
-
doc.write(`const ${outputVar} = new Array(${accessor}.length);`);
|
|
11139
|
-
doc.write(`for (let ${iVar} = 0; ${iVar} < ${accessor}.length; ${iVar}++) {`);
|
|
11140
|
-
doc.indented((d) => {
|
|
11141
|
-
d.write(`const ${elemVar} = ${accessor}[${iVar}];`);
|
|
11142
|
-
const elemOutput = compileChild(d, ctx, def.element, elemVar, buildsValue);
|
|
11143
|
-
if (outputVar && elemOutput !== null)
|
|
11144
|
-
d.write(`${outputVar}[${iVar}] = ${elemOutput};`);
|
|
11145
|
-
});
|
|
11146
|
-
doc.write(`}`);
|
|
11147
|
-
return outputVar;
|
|
11148
|
-
}
|
|
11149
|
-
function generateLiteralCheck(doc, ctx, schema, accessor) {
|
|
11150
|
-
const def = schema._zod.def;
|
|
11151
|
-
const values = def.values;
|
|
11152
|
-
if (values.length !== 1) {
|
|
11153
|
-
const literalSet = addConstant(ctx, new Set(values));
|
|
11154
|
-
doc.write(`if (!${literalSet}.has(${accessor})) return INVALID;`);
|
|
11155
|
-
return accessor;
|
|
11156
|
-
}
|
|
11157
|
-
const value = values[0];
|
|
11158
|
-
if (typeof value === "number" && Number.isNaN(value)) {
|
|
11159
|
-
const literalSet = addConstant(ctx, new Set(values));
|
|
11160
|
-
doc.write(`if (!${literalSet}.has(${accessor})) return INVALID;`);
|
|
11161
|
-
return accessor;
|
|
11162
|
-
}
|
|
11163
|
-
if (typeof value === "string") {
|
|
11164
|
-
doc.write(`if (${accessor} !== ${esc(value)}) return INVALID;`);
|
|
11165
|
-
} else if (typeof value === "number" || typeof value === "boolean") {
|
|
11166
|
-
doc.write(`if (${accessor} !== ${value}) return INVALID;`);
|
|
11167
|
-
} else if (value === null) {
|
|
11168
|
-
doc.write(`if (${accessor} !== null) return INVALID;`);
|
|
11169
|
-
} else if (value === undefined) {
|
|
11170
|
-
doc.write(`if (${accessor} !== undefined) return INVALID;`);
|
|
11171
|
-
} else if (typeof value === "bigint") {
|
|
11172
|
-
doc.write(`if (${accessor} !== ${value}n) return INVALID;`);
|
|
11173
|
-
} else {
|
|
11174
|
-
throw new ZodCompileUnsupportedError(`literal type ${typeof value}`);
|
|
11175
|
-
}
|
|
11176
|
-
return accessor;
|
|
11177
|
-
}
|
|
11178
|
-
function generateEnumCheck(doc, ctx, schema, accessor) {
|
|
11179
|
-
const values = schema._zod.values;
|
|
11180
|
-
if (!values) {
|
|
11181
|
-
throw new ZodCompileUnsupportedError("enum schema without enumerated values");
|
|
11182
|
-
}
|
|
11183
|
-
const enumSet = addConstant(ctx, values);
|
|
11184
|
-
doc.write(`if (!${enumSet}.has(${accessor})) return INVALID;`);
|
|
11185
|
-
return accessor;
|
|
11186
|
-
}
|
|
11187
|
-
function generateWrapperCheck(doc, ctx, schema, accessor) {
|
|
11188
|
-
const def = schema._zod.def;
|
|
11189
|
-
return generateCheck(doc, ctx, def.innerType, accessor);
|
|
11190
|
-
}
|
|
11191
|
-
function generateDefaultCheck(doc, ctx, schema, accessor) {
|
|
11192
|
-
const def = schema._zod.def;
|
|
11193
|
-
const descriptor = Object.getOwnPropertyDescriptor(schema._zod.def, "defaultValue");
|
|
11194
|
-
const defaultGetter = descriptor ? () => schema._zod.def.defaultValue : undefined;
|
|
11195
|
-
if (schema._zod.def.type === "prefault") {
|
|
11196
|
-
if (!defaultGetter) {
|
|
11197
|
-
return generateCheck(doc, ctx, def.innerType, accessor);
|
|
11198
|
-
}
|
|
11199
|
-
const defaultFn = addConstant(ctx, defaultGetter);
|
|
11200
|
-
const inputVar = newVar(ctx);
|
|
11201
|
-
doc.write(`let ${inputVar} = ${accessor};`);
|
|
11202
|
-
doc.write(`if (${accessor} === undefined) ${inputVar} = ${defaultFn}();`);
|
|
11203
|
-
return generateCheck(doc, ctx, def.innerType, inputVar);
|
|
11204
|
-
}
|
|
11205
|
-
const outputVar = newVar(ctx);
|
|
11206
|
-
if (defaultGetter) {
|
|
11207
|
-
const defaultFn = addConstant(ctx, defaultGetter);
|
|
11208
|
-
const cloneFn = addConstant(ctx, shallowClone);
|
|
11209
|
-
doc.write(`let ${outputVar};`);
|
|
11210
|
-
doc.write(`if (${accessor} === undefined) {`);
|
|
11211
|
-
doc.indented((d) => {
|
|
11212
|
-
d.write(`${outputVar} = ${cloneFn}(${defaultFn}());`);
|
|
11213
|
-
});
|
|
11214
|
-
doc.write(`} else {`);
|
|
11215
|
-
doc.indented((d) => {
|
|
11216
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor);
|
|
11217
|
-
d.write(`${outputVar} = ${innerOutput} === undefined ? ${cloneFn}(${defaultFn}()) : ${innerOutput};`);
|
|
11218
|
-
});
|
|
11219
|
-
doc.write(`}`);
|
|
11220
|
-
} else {
|
|
11221
|
-
doc.write(`let ${outputVar};`);
|
|
11222
|
-
doc.write(`if (${accessor} !== undefined) {`);
|
|
11223
|
-
doc.indented((d) => {
|
|
11224
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor);
|
|
11225
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
11226
|
-
});
|
|
11227
|
-
doc.write(`}`);
|
|
11228
|
-
}
|
|
11229
|
-
return outputVar;
|
|
11230
|
-
}
|
|
11231
|
-
function generateNonOptionalCheck(doc, ctx, schema, accessor) {
|
|
11232
|
-
const def = schema._zod.def;
|
|
11233
|
-
const innerOutput = generateCheck(doc, ctx, def.innerType, accessor);
|
|
11234
|
-
const outputVar = newVar(ctx);
|
|
11235
|
-
doc.write(`const ${outputVar} = ${innerOutput};`);
|
|
11236
|
-
doc.write(`if (${outputVar} === undefined) return INVALID;`);
|
|
11237
|
-
return outputVar;
|
|
11238
|
-
}
|
|
11239
|
-
function generateTupleCheck(doc, ctx, schema, accessor) {
|
|
11240
|
-
const def = schema._zod.def;
|
|
11241
|
-
const items = def.items;
|
|
11242
|
-
const rest = def.rest;
|
|
11243
|
-
doc.write(`if (!Array.isArray(${accessor})) return INVALID;`);
|
|
11244
|
-
const optinStart = getTupleOptStart(items, "optin");
|
|
11245
|
-
const optoutStart = getTupleOptStart(items, "optout");
|
|
11246
|
-
if (rest) {
|
|
11247
|
-
doc.write(`if (${accessor}.length < ${optinStart}) return INVALID;`);
|
|
11248
|
-
} else {
|
|
11249
|
-
doc.write(`if (${accessor}.length < ${optinStart} || ${accessor}.length > ${items.length}) return INVALID;`);
|
|
11250
|
-
}
|
|
11251
|
-
const outputVar = newVar(ctx);
|
|
11252
|
-
doc.write(`const ${outputVar} = [];`);
|
|
11253
|
-
for (let i = 0;i < items.length; i++) {
|
|
11254
|
-
const itemSchema = items[i];
|
|
11255
|
-
if (i >= optoutStart) {
|
|
11256
|
-
doc.write(`if (${outputVar}.length === ${i}) {`);
|
|
11257
|
-
doc.indented((d) => {
|
|
11258
|
-
d.write(`if (${i} < ${accessor}.length) {`);
|
|
11259
|
-
d.indented((d2) => {
|
|
11260
|
-
const elemVar = newVar(ctx);
|
|
11261
|
-
d2.write(`const ${elemVar} = ${accessor}[${i}];`);
|
|
11262
|
-
const elemOutput = compileChild(d2, ctx, itemSchema, elemVar);
|
|
11263
|
-
d2.write(`${outputVar}[${i}] = ${elemOutput};`);
|
|
11264
|
-
});
|
|
11265
|
-
d.write(`} else {`);
|
|
11266
|
-
d.indented((d2) => {
|
|
11267
|
-
if (dropsWhenAbsent(itemSchema)) {
|
|
11268
|
-
d2.write(`${outputVar}.length = ${i};`);
|
|
11269
|
-
return;
|
|
11270
|
-
}
|
|
11271
|
-
const elemVar = newVar(ctx);
|
|
11272
|
-
const branchVar = newVar(ctx);
|
|
11273
|
-
d2.write(`const ${elemVar} = undefined;`);
|
|
11274
|
-
d2.write(`const ${branchVar} = (() => {`);
|
|
11275
|
-
d2.indented((d3) => {
|
|
11276
|
-
const elemOutput = compileChild(d3, ctx, itemSchema, elemVar);
|
|
11277
|
-
d3.write(`return ${elemOutput};`);
|
|
11278
|
-
});
|
|
11279
|
-
d2.write(`})();`);
|
|
11280
|
-
d2.write(`if (${branchVar} === INVALID || ${branchVar} === undefined) ${outputVar}.length = ${i};`);
|
|
11281
|
-
d2.write(`else ${outputVar}[${i}] = ${branchVar};`);
|
|
11282
|
-
});
|
|
11283
|
-
d.write(`}`);
|
|
11284
|
-
});
|
|
11285
|
-
doc.write(`}`);
|
|
11286
|
-
} else {
|
|
11287
|
-
const elemVar = newVar(ctx);
|
|
11288
|
-
doc.write(`const ${elemVar} = ${accessor}[${i}];`);
|
|
11289
|
-
const elemOutput = compileChild(doc, ctx, itemSchema, elemVar);
|
|
11290
|
-
doc.write(`${outputVar}[${i}] = ${elemOutput};`);
|
|
11291
|
-
}
|
|
11292
|
-
}
|
|
11293
|
-
if (rest) {
|
|
11294
|
-
const iVar = newVar(ctx);
|
|
11295
|
-
const elemVar = newVar(ctx);
|
|
11296
|
-
doc.write(`for (let ${iVar} = ${items.length}; ${iVar} < ${accessor}.length; ${iVar}++) {`);
|
|
11297
|
-
doc.indented((d) => {
|
|
11298
|
-
d.write(`const ${elemVar} = ${accessor}[${iVar}];`);
|
|
11299
|
-
const elemOutput = compileChild(d, ctx, rest, elemVar);
|
|
11300
|
-
d.write(`${outputVar}[${iVar}] = ${elemOutput};`);
|
|
11301
|
-
});
|
|
11302
|
-
doc.write(`}`);
|
|
11303
|
-
}
|
|
11304
|
-
return outputVar;
|
|
11305
|
-
}
|
|
11306
|
-
function getTupleOptStart(items, key) {
|
|
11307
|
-
for (let i = items.length - 1;i >= 0; i--) {
|
|
11308
|
-
const omittable = key === "optin" ? items[i]._zod.optin !== undefined : items[i]._zod.optout === "optional";
|
|
11309
|
-
if (!omittable)
|
|
11310
|
-
return i + 1;
|
|
11311
|
-
}
|
|
11312
|
-
return 0;
|
|
11313
|
-
}
|
|
11314
|
-
function generateUnionCheck(doc, ctx, schema, accessor) {
|
|
11315
|
-
const def = schema._zod.def;
|
|
11316
|
-
const options = def.options;
|
|
11317
|
-
if (def.discriminator) {
|
|
11318
|
-
return generateDiscriminatedUnionCheck(doc, ctx, def, accessor);
|
|
11319
|
-
}
|
|
11320
|
-
if (def.inclusive === false) {
|
|
11321
|
-
throw new ZodCompileUnsupportedError("exclusive unions (z.xor)");
|
|
11322
|
-
}
|
|
11323
|
-
if (options.length === 0) {
|
|
11324
|
-
doc.write("return INVALID;");
|
|
11325
|
-
return accessor;
|
|
11326
|
-
}
|
|
11327
|
-
if (options.length === 1) {
|
|
11328
|
-
return generateCheck(doc, ctx, options[0], accessor);
|
|
11329
|
-
}
|
|
11330
|
-
const allLiterals = options.every((opt) => opt._zod.def.type === "literal" && !opt._zod.def.checks?.length);
|
|
11331
|
-
if (allLiterals) {
|
|
11332
|
-
const values = new Set(options.flatMap((opt) => opt._zod.def.values));
|
|
11333
|
-
const valuesConst = addConstant(ctx, values);
|
|
11334
|
-
doc.write(`if (!${valuesConst}.has(${accessor})) return INVALID;`);
|
|
11335
|
-
return accessor;
|
|
11336
|
-
}
|
|
11337
|
-
const outputVar = newVar(ctx);
|
|
11338
|
-
doc.write(`let ${outputVar};`);
|
|
11339
|
-
for (let i = 0;i < options.length; i++) {
|
|
11340
|
-
const opt = options[i];
|
|
11341
|
-
if (i === 0) {
|
|
11342
|
-
doc.write(`${outputVar} = (() => {`);
|
|
11343
|
-
} else {
|
|
11344
|
-
doc.write(`if (${outputVar} === INVALID) ${outputVar} = (() => {`);
|
|
11345
|
-
}
|
|
11346
|
-
doc.indented((d) => {
|
|
11347
|
-
const branchOutput = generateCheck(d, ctx, opt, accessor);
|
|
11348
|
-
d.write(`return ${branchOutput};`);
|
|
11349
|
-
});
|
|
11350
|
-
doc.write(`})();`);
|
|
11351
|
-
}
|
|
11352
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
11353
|
-
return outputVar;
|
|
11354
|
-
}
|
|
11355
|
-
function generateDiscriminatedUnionCheck(doc, ctx, def, accessor) {
|
|
11356
|
-
if (def.unionFallback) {
|
|
11357
|
-
throw new ZodCompileUnsupportedError("discriminated union with unionFallback");
|
|
11358
|
-
}
|
|
11359
|
-
if (def.options.length === 0) {
|
|
11360
|
-
doc.write("return INVALID;");
|
|
11361
|
-
return accessor;
|
|
11362
|
-
}
|
|
11363
|
-
const discVar = newVar(ctx);
|
|
11364
|
-
const outputVar = newVar(ctx);
|
|
11365
|
-
doc.write(`const ${discVar} = ${accessor}?.[${esc(def.discriminator)}];`);
|
|
11366
|
-
doc.write(`let ${outputVar};`);
|
|
11367
|
-
let firstBranch = true;
|
|
11368
|
-
const claimed = new Set;
|
|
11369
|
-
for (const option of def.options) {
|
|
11370
|
-
const values = option._zod.propValues?.[def.discriminator];
|
|
11371
|
-
if (!values || values.size === 0) {
|
|
11372
|
-
throw new ZodCompileUnsupportedError("discriminated union option without static discriminator values");
|
|
11373
|
-
}
|
|
11374
|
-
for (const value of values) {
|
|
11375
|
-
if (claimed.has(value)) {
|
|
11376
|
-
throw new ZodCompileUnsupportedError(`duplicate discriminator value ${String(value)}`);
|
|
11377
|
-
}
|
|
11378
|
-
claimed.add(value);
|
|
11379
|
-
}
|
|
11380
|
-
const conditions = Array.from(values, (value) => literalEquality(ctx, discVar, value));
|
|
11381
|
-
const prefix = firstBranch ? "if" : "else if";
|
|
11382
|
-
doc.write(`${prefix} (${conditions.join(" || ")}) {`);
|
|
11383
|
-
doc.indented((d) => {
|
|
11384
|
-
const branchOutput = generateCheck(d, ctx, option, accessor);
|
|
11385
|
-
d.write(`${outputVar} = ${branchOutput};`);
|
|
11386
|
-
});
|
|
11387
|
-
doc.write(`}`);
|
|
11388
|
-
firstBranch = false;
|
|
11389
|
-
}
|
|
11390
|
-
doc.write(`else { return INVALID; }`);
|
|
11391
|
-
return outputVar;
|
|
11392
|
-
}
|
|
11393
|
-
function literalEquality(ctx, accessor, value) {
|
|
11394
|
-
if (typeof value === "string")
|
|
11395
|
-
return `${accessor} === ${esc(value)}`;
|
|
11396
|
-
if (typeof value === "number") {
|
|
11397
|
-
if (Number.isNaN(value))
|
|
11398
|
-
return `Number.isNaN(${accessor})`;
|
|
11399
|
-
return `${accessor} === ${value}`;
|
|
11400
|
-
}
|
|
11401
|
-
if (typeof value === "boolean")
|
|
11402
|
-
return `${accessor} === ${value}`;
|
|
11403
|
-
if (value === null)
|
|
11404
|
-
return `${accessor} === null`;
|
|
11405
|
-
if (value === undefined)
|
|
11406
|
-
return `${accessor} === undefined`;
|
|
11407
|
-
if (typeof value === "bigint")
|
|
11408
|
-
return `${accessor} === ${value}n`;
|
|
11409
|
-
if (typeof value === "symbol") {
|
|
11410
|
-
const symbolConst = addConstant(ctx, value);
|
|
11411
|
-
return `${accessor} === ${symbolConst}`;
|
|
11412
|
-
}
|
|
11413
|
-
throw new ZodCompileUnsupportedError(`literal discriminator value ${String(value)}`);
|
|
11414
|
-
}
|
|
11415
|
-
function generateIntersectionCheck(doc, ctx, schema, accessor) {
|
|
11416
|
-
const def = schema._zod.def;
|
|
11417
|
-
ctx.definite = false;
|
|
11418
|
-
const leftOutput = compileChild(doc, ctx, def.left, accessor);
|
|
11419
|
-
const rightOutput = compileChild(doc, ctx, def.right, accessor);
|
|
11420
|
-
const mergeConst = addConstant(ctx, mergeValues);
|
|
11421
|
-
const mergedVar = newVar(ctx);
|
|
11422
|
-
doc.write(`const ${mergedVar} = ${mergeConst}(${leftOutput}, ${rightOutput});`);
|
|
11423
|
-
doc.write(`if (!${mergedVar}.valid) return INVALID;`);
|
|
11424
|
-
return `${mergedVar}.data`;
|
|
11425
|
-
}
|
|
11426
|
-
function generateRecordCheck(doc, ctx, schema, accessor) {
|
|
11427
|
-
const def = schema._zod.def;
|
|
11428
|
-
const isPlainObjectConst = addConstant(ctx, isPlainObject);
|
|
11429
|
-
doc.write(`if (!${isPlainObjectConst}(${accessor})) return INVALID;`);
|
|
11430
|
-
const outputVar = newVar(ctx);
|
|
11431
|
-
const kVar = newVar(ctx);
|
|
11432
|
-
const valVar = newVar(ctx);
|
|
11433
|
-
doc.write(`const ${outputVar} = {};`);
|
|
11434
|
-
const recordDef = def;
|
|
11435
|
-
const keyValues = recordDef.partial ? undefined : def.keyType._zod.values;
|
|
11436
|
-
if (keyValues) {
|
|
11437
|
-
const inputKeys = [];
|
|
11438
|
-
for (const key of keyValues) {
|
|
11439
|
-
if (!(typeof key === "string" || typeof key === "number" || typeof key === "symbol")) {
|
|
11440
|
-
throw new ZodCompileUnsupportedError(`record key value ${String(key)}`);
|
|
11441
|
-
}
|
|
11442
|
-
const inputKey = typeof key === "number" ? key.toString() : key;
|
|
11443
|
-
if (inputKey === "__proto__") {
|
|
11444
|
-
throw new ZodCompileUnsupportedError('record key "__proto__"');
|
|
11445
|
-
}
|
|
11446
|
-
inputKeys.push(inputKey);
|
|
11447
|
-
const keyConst = addConstant(ctx, key);
|
|
11448
|
-
const outKey = generateCheck(doc, ctx, def.keyType, keyConst);
|
|
11449
|
-
const valueVar = newVar(ctx);
|
|
11450
|
-
doc.write(`const ${valueVar} = ${accessor}[${literalPropertyKey(ctx, inputKey)}];`);
|
|
11451
|
-
const valOutput = compileChild(doc, ctx, def.valueType, valueVar);
|
|
11452
|
-
doc.write(`${outputVar}[${outKey}] = ${valOutput};`);
|
|
11453
|
-
}
|
|
11454
|
-
const knownKeysConst = addConstant(ctx, new Set(inputKeys));
|
|
11455
|
-
doc.write(`for (const ${kVar} in ${accessor}) {`);
|
|
11456
|
-
doc.indented((d) => {
|
|
11457
|
-
d.write(`if (${knownKeysConst}.has(${kVar})) continue;`);
|
|
11458
|
-
if (recordDef.mode === "loose") {
|
|
11459
|
-
d.write(`if (${kVar} !== "__proto__") ${outputVar}[${kVar}] = ${accessor}[${kVar}];`);
|
|
11460
|
-
} else {
|
|
11461
|
-
d.write(`return INVALID;`);
|
|
11462
|
-
}
|
|
11463
|
-
});
|
|
11464
|
-
doc.write(`}`);
|
|
11465
|
-
return outputVar;
|
|
11466
|
-
}
|
|
11467
|
-
const keyDef = def.keyType._zod.def;
|
|
11468
|
-
const keyIsBareString = keyDef.type === "string" && keyDef.format === undefined && !keyDef.coerce && (keyDef.checks?.length ?? 0) === 0;
|
|
11469
|
-
if (!keyIsBareString) {
|
|
11470
|
-
const isLoose = def.mode === "loose";
|
|
11471
|
-
const keyFn = compileFn(def.keyType);
|
|
11472
|
-
if (keyFn.definite === false)
|
|
11473
|
-
ctx.definite = false;
|
|
11474
|
-
const keyFast = addConstant(ctx, keyFn);
|
|
11475
|
-
const numericConst = addConstant(ctx, number);
|
|
11476
|
-
const outKeyVar = newVar(ctx);
|
|
11477
|
-
emitOwnKeys(doc, ctx, accessor, kVar, (d) => {
|
|
11478
|
-
d.write(`let ${outKeyVar} = ${keyFast}(${kVar});`);
|
|
11479
|
-
d.write(`if (${outKeyVar} === INVALID && typeof ${kVar} === "string" && ${numericConst}.test(${kVar})) ${outKeyVar} = ${keyFast}(Number(${kVar}));`);
|
|
11480
|
-
if (isLoose) {
|
|
11481
|
-
d.write(`if (${outKeyVar} === INVALID) { ${outputVar}[${kVar}] = ${accessor}[${kVar}]; continue; }`);
|
|
11482
|
-
} else {
|
|
11483
|
-
d.write(`if (${outKeyVar} === INVALID) return INVALID;`);
|
|
11484
|
-
}
|
|
11485
|
-
d.write(`if (${outKeyVar} === "__proto__") continue;`);
|
|
11486
|
-
const valueVar = newVar(ctx);
|
|
11487
|
-
d.write(`const ${valueVar} = ${accessor}[${kVar}];`);
|
|
11488
|
-
const valOutput = compileChild(d, ctx, def.valueType, valueVar);
|
|
11489
|
-
d.write(`${outputVar}[${outKeyVar}] = ${valOutput};`);
|
|
11490
|
-
});
|
|
11491
|
-
return outputVar;
|
|
11492
|
-
}
|
|
11493
|
-
emitOwnKeys(doc, ctx, accessor, kVar, (d) => {
|
|
11494
|
-
d.write(`const ${valVar} = ${accessor}[${kVar}];`);
|
|
11495
|
-
const valOutput = compileChild(d, ctx, def.valueType, valVar);
|
|
11496
|
-
d.write(`${outputVar}[${kVar}] = ${valOutput};`);
|
|
11497
|
-
}, `return INVALID;`);
|
|
11498
|
-
return outputVar;
|
|
11499
|
-
}
|
|
11500
|
-
function emitOwnKeys(doc, ctx, accessor, kVar, body, onSymbol) {
|
|
11501
|
-
const propIsEnumerableConst = addConstant(ctx, Object.prototype.propertyIsEnumerable);
|
|
11502
|
-
const symsVar = newVar(ctx);
|
|
11503
|
-
const keysVar = newVar(ctx);
|
|
11504
|
-
const iVar = newVar(ctx);
|
|
11505
|
-
doc.write(`const ${symsVar} = Object.getOwnPropertySymbols(${accessor});`);
|
|
11506
|
-
doc.write(`const ${keysVar} = Object.getOwnPropertyNames(${accessor});`);
|
|
11507
|
-
doc.write(`for (let ${iVar} = 0; ${iVar} < ${keysVar}.length; ${iVar}++) {`);
|
|
11508
|
-
doc.indented((d) => {
|
|
11509
|
-
d.write(`const ${kVar} = ${keysVar}[${iVar}];`);
|
|
11510
|
-
d.write(`if (${kVar} === "__proto__" || !${propIsEnumerableConst}.call(${accessor}, ${kVar})) continue;`);
|
|
11511
|
-
body(d);
|
|
11512
|
-
});
|
|
11513
|
-
doc.write(`}`);
|
|
11514
|
-
doc.write(`for (let ${iVar} = 0; ${iVar} < ${symsVar}.length; ${iVar}++) {`);
|
|
11515
|
-
doc.indented((d) => {
|
|
11516
|
-
d.write(`const ${kVar} = ${symsVar}[${iVar}];`);
|
|
11517
|
-
d.write(`if (!${propIsEnumerableConst}.call(${accessor}, ${kVar})) continue;`);
|
|
11518
|
-
if (onSymbol)
|
|
11519
|
-
d.write(onSymbol);
|
|
11520
|
-
else
|
|
11521
|
-
body(d);
|
|
11522
|
-
});
|
|
11523
|
-
doc.write(`}`);
|
|
11524
|
-
}
|
|
11525
|
-
function literalPropertyKey(ctx, key) {
|
|
11526
|
-
if (typeof key === "string")
|
|
11527
|
-
return esc(key);
|
|
11528
|
-
return addConstant(ctx, key);
|
|
11529
|
-
}
|
|
11530
|
-
function generateMapCheck(doc, ctx, schema, accessor) {
|
|
11531
|
-
const def = schema._zod.def;
|
|
11532
|
-
doc.write(`if (!(${accessor} instanceof Map)) return INVALID;`);
|
|
11533
|
-
const outputVar = newVar(ctx);
|
|
11534
|
-
const kVar = newVar(ctx);
|
|
11535
|
-
const valVar = newVar(ctx);
|
|
11536
|
-
doc.write(`const ${outputVar} = new Map();`);
|
|
11537
|
-
doc.write(`for (const [${kVar}, ${valVar}] of ${accessor}) {`);
|
|
11538
|
-
doc.indented((d) => {
|
|
11539
|
-
const keyOutput = generateCheck(d, ctx, def.keyType, kVar);
|
|
11540
|
-
const valOutput = generateCheck(d, ctx, def.valueType, valVar);
|
|
11541
|
-
d.write(`${outputVar}.set(${keyOutput}, ${valOutput});`);
|
|
11542
|
-
});
|
|
11543
|
-
doc.write(`}`);
|
|
11544
|
-
return outputVar;
|
|
11545
|
-
}
|
|
11546
|
-
function generateSetCheck(doc, ctx, schema, accessor) {
|
|
11547
|
-
const def = schema._zod.def;
|
|
11548
|
-
doc.write(`if (!(${accessor} instanceof Set)) return INVALID;`);
|
|
11549
|
-
const outputVar = newVar(ctx);
|
|
11550
|
-
const valVar = newVar(ctx);
|
|
11551
|
-
doc.write(`const ${outputVar} = new Set();`);
|
|
11552
|
-
doc.write(`for (const ${valVar} of ${accessor}) {`);
|
|
11553
|
-
doc.indented((d) => {
|
|
11554
|
-
const valOutput = generateCheck(d, ctx, def.valueType, valVar);
|
|
11555
|
-
d.write(`${outputVar}.add(${valOutput});`);
|
|
11556
|
-
});
|
|
11557
|
-
doc.write(`}`);
|
|
11558
|
-
return outputVar;
|
|
11559
|
-
}
|
|
11560
|
-
function generateFileCheck(doc, accessor) {
|
|
11561
|
-
doc.write(`if (!(${accessor} instanceof File)) return INVALID;`);
|
|
11562
|
-
return accessor;
|
|
11563
|
-
}
|
|
11564
|
-
function generateTemplateLiteralCheck(doc, ctx, schema, accessor) {
|
|
11565
|
-
doc.write(`if (typeof ${accessor} !== "string") return INVALID;`);
|
|
11566
|
-
const pattern = schema._zod.pattern;
|
|
11567
|
-
if (pattern) {
|
|
11568
|
-
const patternConst = addConstant(ctx, pattern);
|
|
11569
|
-
doc.write(`${patternConst}.lastIndex = 0;`);
|
|
11570
|
-
doc.write(`if (!${patternConst}.test(${accessor})) return INVALID;`);
|
|
11571
|
-
}
|
|
11572
|
-
return accessor;
|
|
11573
|
-
}
|
|
11574
|
-
function generateLazyCheck(doc, ctx, schema, accessor) {
|
|
11575
|
-
const def = schema._zod.def;
|
|
11576
|
-
const getterConst = addUserConstant(ctx, def.getter);
|
|
11577
|
-
const cacheConst = addConstant(ctx, { parser: null });
|
|
11578
|
-
doc.write(`if (!${cacheConst}.parser) {`);
|
|
11579
|
-
doc.indented((d) => {
|
|
11580
|
-
d.write(`const inner = ${getterConst}();`);
|
|
11581
|
-
d.write(`${cacheConst}.parser = function(input) {`);
|
|
11582
|
-
d.indented((d2) => {
|
|
11583
|
-
d2.write(`const result = inner._zod.run({ value: input, issues: [] }, {});`);
|
|
11584
|
-
d2.write(`return result.issues.length === 0 ? result.value : INVALID;`);
|
|
11585
|
-
});
|
|
11586
|
-
d.write(`};`);
|
|
11587
|
-
});
|
|
11588
|
-
doc.write(`}`);
|
|
11589
|
-
const outputVar = newVar(ctx);
|
|
11590
|
-
doc.write(`const ${outputVar} = ${cacheConst}.parser(${accessor});`);
|
|
11591
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
11592
|
-
return outputVar;
|
|
11593
|
-
}
|
|
11594
|
-
function generatePipeCheck(doc, ctx, schema, accessor) {
|
|
11595
|
-
const def = schema._zod.def;
|
|
11596
|
-
const inputOutput = generateCheck(doc, ctx, def.in, accessor);
|
|
11597
|
-
if (def.transform) {
|
|
11598
|
-
if (isAsyncFunction(def.transform)) {
|
|
11599
|
-
throw new ZodCompileAsyncError("z.compile: async transforms in pipes are not supported");
|
|
11600
|
-
}
|
|
11601
|
-
const transformFn = def.transform;
|
|
11602
|
-
const helperFn = (value) => {
|
|
11603
|
-
const fakePayload = { value, issues: [], addIssue: pushIssue };
|
|
11604
|
-
const result = transformFn(value, fakePayload);
|
|
11605
|
-
if (result instanceof Promise)
|
|
11606
|
-
return INVALID;
|
|
11607
|
-
return fakePayload.issues.length === 0 ? result : INVALID;
|
|
11608
|
-
};
|
|
11609
|
-
const helperConst = addUserConstant(ctx, helperFn);
|
|
11610
|
-
const transformedVar = newVar(ctx);
|
|
11611
|
-
doc.write(`const ${transformedVar} = ${helperConst}(${inputOutput});`);
|
|
11612
|
-
doc.write(`if (${transformedVar} === INVALID) return INVALID;`);
|
|
11613
|
-
return generateCheck(doc, ctx, def.out, transformedVar);
|
|
11614
|
-
} else {
|
|
11615
|
-
return generateCheck(doc, ctx, def.out, inputOutput);
|
|
11616
|
-
}
|
|
11617
|
-
}
|
|
11618
|
-
function isAsyncFunction(fn) {
|
|
11619
|
-
return typeof fn === "function" && (fn.constructor.name === "AsyncFunction" || fn[Symbol.toStringTag] === "AsyncFunction");
|
|
11620
|
-
}
|
|
11621
|
-
function generateCustomCheck(doc, ctx, schema, accessor) {
|
|
11622
|
-
const def = schema._zod.def;
|
|
11623
|
-
if (def.fn) {
|
|
11624
|
-
if (isAsyncFunction(def.fn)) {
|
|
11625
|
-
throw new ZodCompileAsyncError("z.compile: async custom predicates are not supported");
|
|
11626
|
-
}
|
|
11627
|
-
const fnConst = addUserConstant(ctx, def.fn);
|
|
11628
|
-
const throwAsyncConst = addConstant(ctx, throwAsync);
|
|
11629
|
-
const resVar = newVar(ctx);
|
|
11630
|
-
doc.write(`const ${resVar} = ${fnConst}(${accessor});`);
|
|
11631
|
-
doc.write(`if (${resVar} instanceof Promise) ${throwAsyncConst}();`);
|
|
11632
|
-
doc.write(`if (!${resVar}) return INVALID;`);
|
|
11633
|
-
} else {
|
|
11634
|
-
throw new ZodCompileUnsupportedError("custom schema without a predicate function");
|
|
11635
|
-
}
|
|
11636
|
-
return accessor;
|
|
11637
|
-
}
|
|
11638
|
-
function runtimeCatch(innerSchema, catchValue, value) {
|
|
11639
|
-
const result = innerSchema._zod.run({ value, issues: [] }, {});
|
|
11640
|
-
if (result && typeof result.then === "function")
|
|
11641
|
-
return INVALID;
|
|
11642
|
-
const r = result;
|
|
11643
|
-
if (r.issues.length === 0)
|
|
11644
|
-
return r.value;
|
|
11645
|
-
return catchValue();
|
|
11646
|
-
}
|
|
11647
|
-
function generateCatchCheck(doc, ctx, schema, accessor) {
|
|
11648
|
-
const def = schema._zod.def;
|
|
11649
|
-
if (!def.catchValue[CONSTANT_CATCH]) {
|
|
11650
|
-
throw new ZodCompileUnsupportedError("catch with a callback (only a constant catch value compiles)", false);
|
|
11651
|
-
}
|
|
11652
|
-
const outputVar = newVar(ctx);
|
|
11653
|
-
doc.write(`let ${outputVar} = (() => {`);
|
|
11654
|
-
doc.indented((d) => {
|
|
11655
|
-
const innerOut = compileChild(d, ctx, def.innerType, accessor);
|
|
11656
|
-
d.write(`return ${innerOut};`);
|
|
11657
|
-
});
|
|
11658
|
-
doc.write(`})();`);
|
|
11659
|
-
const innerConst = addConstant(ctx, def.innerType);
|
|
11660
|
-
const catchConst = addUserConstant(ctx, def.catchValue);
|
|
11661
|
-
const catchHelperConst = addConstant(ctx, runtimeCatch);
|
|
11662
|
-
doc.write(`if (${outputVar} === INVALID) {`);
|
|
11663
|
-
doc.indented((d) => {
|
|
11664
|
-
d.write(`${outputVar} = ${catchHelperConst}(${innerConst}, ${catchConst}, ${accessor});`);
|
|
11665
|
-
d.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
11666
|
-
});
|
|
11667
|
-
doc.write(`}`);
|
|
11668
|
-
return outputVar;
|
|
11669
|
-
}
|
|
11670
|
-
function generateTransformCheck(doc, ctx, schema, accessor) {
|
|
11671
|
-
const def = schema._zod.def;
|
|
11672
|
-
if (def.transform) {
|
|
11673
|
-
if (isAsyncFunction(def.transform)) {
|
|
11674
|
-
throw new ZodCompileAsyncError("z.compile: async transforms are not supported");
|
|
11675
|
-
}
|
|
11676
|
-
const transformFn = def.transform;
|
|
11677
|
-
const helperFn = (value) => {
|
|
11678
|
-
const fakePayload = { value, issues: [], addIssue: pushIssue };
|
|
11679
|
-
const result = transformFn(value, fakePayload);
|
|
11680
|
-
if (result instanceof Promise)
|
|
11681
|
-
return INVALID;
|
|
11682
|
-
return fakePayload.issues.length === 0 ? result : INVALID;
|
|
11683
|
-
};
|
|
11684
|
-
const helperConst = addUserConstant(ctx, helperFn);
|
|
11685
|
-
const outputVar = newVar(ctx);
|
|
11686
|
-
doc.write(`const ${outputVar} = ${helperConst}(${accessor});`);
|
|
11687
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
11688
|
-
return outputVar;
|
|
11689
|
-
}
|
|
11690
|
-
return accessor;
|
|
11691
|
-
}
|
|
11692
10054
|
// node_modules/zod/v4/core/api.js
|
|
11693
10055
|
function snapshotChecks(def) {
|
|
11694
10056
|
if (def.checks)
|
|
@@ -14951,7 +13313,6 @@ var advertisedOperationSchemas = {
|
|
|
14951
13313
|
worker: advertised(workerOperations),
|
|
14952
13314
|
state: advertised(stateOperations)
|
|
14953
13315
|
};
|
|
14954
|
-
var compiledOperationSchemas = Object.fromEntries(OPERATION_FAMILIES.map((family) => [family, compile(operationSchemas[family])]));
|
|
14955
13316
|
var success = (revisionValue, workspaceRevisionValue, data) => ({
|
|
14956
13317
|
ok: true,
|
|
14957
13318
|
schemaVersion: 1,
|
|
@@ -14973,8 +13334,7 @@ function resolveIdRef(kind, ref, ids) {
|
|
|
14973
13334
|
function parseOperation(family, input) {
|
|
14974
13335
|
if (typeof input === "object" && input !== null && "schemaVersion" in input && input.schemaVersion !== SCHEMA_VERSION)
|
|
14975
13336
|
return failure2("unsupported_version", "unsupported schema version", { operation: family });
|
|
14976
|
-
const
|
|
14977
|
-
const parsed = compiled.success ? operationSchemas[family].safeParse(input) : compiled;
|
|
13337
|
+
const parsed = operationSchemas[family].safeParse(input);
|
|
14978
13338
|
if (parsed.success)
|
|
14979
13339
|
return success(null, null, parsed.data);
|
|
14980
13340
|
const fields = parsed.error.issues.map((issue) => ({
|
|
@@ -28748,6 +27108,9 @@ var EXEC = new Set(["exec", "x", "dlx"]);
|
|
|
28748
27108
|
|
|
28749
27109
|
// packages/workit-core/src/core/heavy-words.ts
|
|
28750
27110
|
var HEAVY_WORDS = new Set(("npm npx pnpm pnpx yarn bun bunx deno corepack make gmake docker podman nerdctl " + "python python3 py uv poetry pdm pipenv hatch rye bundle rake nx " + "jest vitest mocha ava tap karma jasmine nyc c8 tsc vue-tsc svelte-check eslint stylelint " + "tslint webpack webpack-cli rollup parcel tsup vite next nuxt nuxi astro ng react-scripts " + "playwright cypress wdio testcafe turbo lerna sonar-scanner sonarless sonarless-mcp go " + "golangci-lint gotestsum staticcheck cargo mvn mvnw gradle gradlew ant sbt bazel bazelisk " + "buck2 dotnet msbuild pytest py.test tox nox mypy pyright pylint coverage maturin pip pip3 " + "rspec rails phpunit pest phpstan psalm composer mix swift xcodebuild flutter dart pod cmake " + "ninja meson ctest scons docker-compose buildah earthly").split(" "));
|
|
27111
|
+
// packages/workit-core/src/hooks/write-gate.ts
|
|
27112
|
+
import path23 from "node:path";
|
|
27113
|
+
|
|
28751
27114
|
// packages/workit-core/src/core/live-session.ts
|
|
28752
27115
|
import { spawnSync as spawnSync15 } from "node:child_process";
|
|
28753
27116
|
var LIVE_SESSION_TTL_MS = 15 * 60000;
|
|
@@ -28760,7 +27123,7 @@ var PIDS = new Map;
|
|
|
28760
27123
|
|
|
28761
27124
|
// packages/workit-core/src/hooks/write-gate.ts
|
|
28762
27125
|
var CASE_INSENSITIVE = process.platform === "darwin" || process.platform === "win32";
|
|
28763
|
-
var PREFIXES2 = new Set(["sudo", "command", "env", "nohup", "time", "exec", "xargs"]);
|
|
27126
|
+
var PREFIXES2 = new Set(["sudo", "command", "env", "nohup", "time", "exec", "xargs", "busybox"]);
|
|
28764
27127
|
var SCRIPT_SHELLS = new Set(["sh", "bash", "zsh", "dash", "ksh"]);
|
|
28765
27128
|
var cache2 = new Map;
|
|
28766
27129
|
var MUTATING_GIT = new Set([
|
|
@@ -28787,6 +27150,7 @@ var MUTATING_WORKIT_GIT = new Set(["commit", "branch"]);
|
|
|
28787
27150
|
var CD2 = new Set(["cd", "chdir", "set-location", "sl"]);
|
|
28788
27151
|
var PUSHD2 = new Set(["pushd", "push-location"]);
|
|
28789
27152
|
var POPD2 = new Set(["popd", "pop-location"]);
|
|
27153
|
+
var UNKNOWN_DIR = path23.resolve(path23.parse(process.cwd()).root, ".workit-unknown-dir");
|
|
28790
27154
|
// packages/workit-core/src/core/skill-manifests.ts
|
|
28791
27155
|
if (false) {}
|
|
28792
27156
|
|
|
@@ -28831,6 +27195,19 @@ var sinceStart = new Map;
|
|
|
28831
27195
|
// packages/workit-core/src/hooks/cwd-normalize.ts
|
|
28832
27196
|
var FILTERS = new Set(["tail", "head", "grep"]);
|
|
28833
27197
|
|
|
27198
|
+
// packages/workit-core/src/hooks/worktree-confine.ts
|
|
27199
|
+
var CASE_INSENSITIVE2 = process.platform === "darwin" || process.platform === "win32";
|
|
27200
|
+
var MUTATING_REMOTE = new Set([
|
|
27201
|
+
"add",
|
|
27202
|
+
"remove",
|
|
27203
|
+
"rm",
|
|
27204
|
+
"rename",
|
|
27205
|
+
"set-url",
|
|
27206
|
+
"set-head",
|
|
27207
|
+
"set-branches",
|
|
27208
|
+
"prune"
|
|
27209
|
+
]);
|
|
27210
|
+
|
|
28834
27211
|
// packages/workit-core/src/hooks/handle.ts
|
|
28835
27212
|
var offered = new Set;
|
|
28836
27213
|
var PRE_TOOL = new Set(["shell.pre", "write.pre", "tool.pre"]);
|
|
@@ -28996,14 +27373,14 @@ import {
|
|
|
28996
27373
|
writeFileSync as writeFileSync3
|
|
28997
27374
|
} from "node:fs";
|
|
28998
27375
|
import os5 from "node:os";
|
|
28999
|
-
import
|
|
27376
|
+
import path24 from "node:path";
|
|
29000
27377
|
var CODEX_AGENT_MARKER = "# Generated by Workit";
|
|
29001
27378
|
var codexHomeOfInstall = (pluginRoot) => {
|
|
29002
|
-
const parts =
|
|
27379
|
+
const parts = path24.resolve(pluginRoot).split(path24.sep);
|
|
29003
27380
|
const cache = parts.length - 4;
|
|
29004
|
-
return cache >= 2 && parts[cache] === "cache" && parts[cache - 1] === "plugins" ? parts.slice(0, cache - 1).join(
|
|
27381
|
+
return cache >= 2 && parts[cache] === "cache" && parts[cache - 1] === "plugins" ? parts.slice(0, cache - 1).join(path24.sep) || path24.sep : null;
|
|
29005
27382
|
};
|
|
29006
|
-
var codexHomeFor = (pluginRoot, env) => codexHomeOfInstall(pluginRoot) ?? (env.CODEX_HOME ||
|
|
27383
|
+
var codexHomeFor = (pluginRoot, env) => codexHomeOfInstall(pluginRoot) ?? (env.CODEX_HOME || path24.join(env.HOME || os5.homedir(), ".codex"));
|
|
29007
27384
|
var MARKER_VERSION = /^# Generated by Workit (\d+\.\d+\.\d+)\b/;
|
|
29008
27385
|
var agentMarkerVersion = (text) => MARKER_VERSION.exec(text)?.[1] ?? null;
|
|
29009
27386
|
var stampCodexAgent = (content, version) => version && content.startsWith(CODEX_AGENT_MARKER) && !agentMarkerVersion(content) ? `${CODEX_AGENT_MARKER} ${version}${content.slice(CODEX_AGENT_MARKER.length)}` : content;
|
|
@@ -29017,7 +27394,7 @@ var versionAtLeast = (version, min) => {
|
|
|
29017
27394
|
};
|
|
29018
27395
|
var pluginVersion = (pluginRoot) => {
|
|
29019
27396
|
try {
|
|
29020
|
-
const version = JSON.parse(readFileSync7(
|
|
27397
|
+
const version = JSON.parse(readFileSync7(path24.join(pluginRoot, "package.json"), "utf8")).version;
|
|
29021
27398
|
return typeof version === "string" && /^\d+\.\d+\.\d+/.test(version) ? version.match(/^\d+\.\d+\.\d+/)[0] : null;
|
|
29022
27399
|
} catch {
|
|
29023
27400
|
return null;
|
|
@@ -29031,15 +27408,15 @@ var lstatOrNull = (file) => {
|
|
|
29031
27408
|
}
|
|
29032
27409
|
};
|
|
29033
27410
|
var installCodexAgents = (pluginRoot, codexHome) => {
|
|
29034
|
-
const source =
|
|
27411
|
+
const source = path24.join(pluginRoot, "agents");
|
|
29035
27412
|
if (!existsSync9(source))
|
|
29036
27413
|
return [];
|
|
29037
27414
|
const version = pluginVersion(pluginRoot);
|
|
29038
|
-
const target =
|
|
27415
|
+
const target = path24.join(codexHome, "agents");
|
|
29039
27416
|
const results = [];
|
|
29040
27417
|
for (const file of readdirSync5(source).filter((name) => name.endsWith(".toml")).toSorted()) {
|
|
29041
|
-
const content = stampCodexAgent(readFileSync7(
|
|
29042
|
-
const dest =
|
|
27418
|
+
const content = stampCodexAgent(readFileSync7(path24.join(source, file), "utf8"), version);
|
|
27419
|
+
const dest = path24.join(target, file);
|
|
29043
27420
|
const stat = lstatOrNull(dest);
|
|
29044
27421
|
if (stat?.isSymbolicLink()) {
|
|
29045
27422
|
results.push({ file: dest, action: "skipped-symlink" });
|
|
@@ -29075,7 +27452,7 @@ var installCodexAgents = (pluginRoot, codexHome) => {
|
|
|
29075
27452
|
results.push({ file: dest, action: "kept-newer-version" });
|
|
29076
27453
|
continue;
|
|
29077
27454
|
}
|
|
29078
|
-
const temp =
|
|
27455
|
+
const temp = path24.join(target, `.${file}.${process.pid}.${Date.now()}.tmp`);
|
|
29079
27456
|
writeFileSync3(temp, content, { flag: "wx" });
|
|
29080
27457
|
try {
|
|
29081
27458
|
renameSync2(temp, dest);
|
|
@@ -29097,8 +27474,8 @@ var codexQualification = (host) => host === "codex_cli" ? { surface: host, cli:
|
|
|
29097
27474
|
var resolveCodexWorkspaceRoot = (pluginRoot, env) => {
|
|
29098
27475
|
const explicit = env.WORKFLOW_WORKSPACE_ROOT;
|
|
29099
27476
|
const inherited = env.PWD;
|
|
29100
|
-
const candidate = explicit ?? (inherited &&
|
|
29101
|
-
if (!candidate || !
|
|
27477
|
+
const candidate = explicit ?? (inherited && path25.resolve(inherited) !== path25.resolve(pluginRoot) ? inherited : undefined);
|
|
27478
|
+
if (!candidate || !path25.isAbsolute(candidate) || !existsSync10(candidate))
|
|
29102
27479
|
return null;
|
|
29103
27480
|
try {
|
|
29104
27481
|
if (!statSync3(candidate).isDirectory())
|
|
@@ -29137,7 +27514,7 @@ var codexContextProvider = (host = detectCodexSurface(process.env), root) => ({
|
|
|
29137
27514
|
};
|
|
29138
27515
|
}
|
|
29139
27516
|
});
|
|
29140
|
-
var pluginRoot = () =>
|
|
27517
|
+
var pluginRoot = () => path25.resolve(path25.dirname(fileURLToPath3(import.meta.url)), "..");
|
|
29141
27518
|
var syncCodexAgents = (root) => {
|
|
29142
27519
|
const codexHome = codexHomeOfInstall(root);
|
|
29143
27520
|
if (!codexHome)
|