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