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