@oimdb/cli 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +141 -2
- package/bin/oimdb.cjs +100 -0
- package/dist/index.cjs +4607 -0
- package/dist/index.d.cts +168 -0
- package/dist/index.d.ts +168 -0
- package/dist/index.js +4579 -0
- package/package.json +65 -4
- package/ui/app.css +323 -0
- package/ui/app.js +409 -0
- package/ui/index.html +63 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,4579 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
// src/core/OIMDevBridge.ts
|
|
8
|
+
import { WebSocketServer } from "ws";
|
|
9
|
+
|
|
10
|
+
// ../../node_modules/zod/v3/external.js
|
|
11
|
+
var external_exports = {};
|
|
12
|
+
__export(external_exports, {
|
|
13
|
+
BRAND: () => BRAND,
|
|
14
|
+
DIRTY: () => DIRTY,
|
|
15
|
+
EMPTY_PATH: () => EMPTY_PATH,
|
|
16
|
+
INVALID: () => INVALID,
|
|
17
|
+
NEVER: () => NEVER,
|
|
18
|
+
OK: () => OK,
|
|
19
|
+
ParseStatus: () => ParseStatus,
|
|
20
|
+
Schema: () => ZodType,
|
|
21
|
+
ZodAny: () => ZodAny,
|
|
22
|
+
ZodArray: () => ZodArray,
|
|
23
|
+
ZodBigInt: () => ZodBigInt,
|
|
24
|
+
ZodBoolean: () => ZodBoolean,
|
|
25
|
+
ZodBranded: () => ZodBranded,
|
|
26
|
+
ZodCatch: () => ZodCatch,
|
|
27
|
+
ZodDate: () => ZodDate,
|
|
28
|
+
ZodDefault: () => ZodDefault,
|
|
29
|
+
ZodDiscriminatedUnion: () => ZodDiscriminatedUnion,
|
|
30
|
+
ZodEffects: () => ZodEffects,
|
|
31
|
+
ZodEnum: () => ZodEnum,
|
|
32
|
+
ZodError: () => ZodError,
|
|
33
|
+
ZodFirstPartyTypeKind: () => ZodFirstPartyTypeKind,
|
|
34
|
+
ZodFunction: () => ZodFunction,
|
|
35
|
+
ZodIntersection: () => ZodIntersection,
|
|
36
|
+
ZodIssueCode: () => ZodIssueCode,
|
|
37
|
+
ZodLazy: () => ZodLazy,
|
|
38
|
+
ZodLiteral: () => ZodLiteral,
|
|
39
|
+
ZodMap: () => ZodMap,
|
|
40
|
+
ZodNaN: () => ZodNaN,
|
|
41
|
+
ZodNativeEnum: () => ZodNativeEnum,
|
|
42
|
+
ZodNever: () => ZodNever,
|
|
43
|
+
ZodNull: () => ZodNull,
|
|
44
|
+
ZodNullable: () => ZodNullable,
|
|
45
|
+
ZodNumber: () => ZodNumber,
|
|
46
|
+
ZodObject: () => ZodObject,
|
|
47
|
+
ZodOptional: () => ZodOptional,
|
|
48
|
+
ZodParsedType: () => ZodParsedType,
|
|
49
|
+
ZodPipeline: () => ZodPipeline,
|
|
50
|
+
ZodPromise: () => ZodPromise,
|
|
51
|
+
ZodReadonly: () => ZodReadonly,
|
|
52
|
+
ZodRecord: () => ZodRecord,
|
|
53
|
+
ZodSchema: () => ZodType,
|
|
54
|
+
ZodSet: () => ZodSet,
|
|
55
|
+
ZodString: () => ZodString,
|
|
56
|
+
ZodSymbol: () => ZodSymbol,
|
|
57
|
+
ZodTransformer: () => ZodEffects,
|
|
58
|
+
ZodTuple: () => ZodTuple,
|
|
59
|
+
ZodType: () => ZodType,
|
|
60
|
+
ZodUndefined: () => ZodUndefined,
|
|
61
|
+
ZodUnion: () => ZodUnion,
|
|
62
|
+
ZodUnknown: () => ZodUnknown,
|
|
63
|
+
ZodVoid: () => ZodVoid,
|
|
64
|
+
addIssueToContext: () => addIssueToContext,
|
|
65
|
+
any: () => anyType,
|
|
66
|
+
array: () => arrayType,
|
|
67
|
+
bigint: () => bigIntType,
|
|
68
|
+
boolean: () => booleanType,
|
|
69
|
+
coerce: () => coerce,
|
|
70
|
+
custom: () => custom,
|
|
71
|
+
date: () => dateType,
|
|
72
|
+
datetimeRegex: () => datetimeRegex,
|
|
73
|
+
defaultErrorMap: () => en_default,
|
|
74
|
+
discriminatedUnion: () => discriminatedUnionType,
|
|
75
|
+
effect: () => effectsType,
|
|
76
|
+
enum: () => enumType,
|
|
77
|
+
function: () => functionType,
|
|
78
|
+
getErrorMap: () => getErrorMap,
|
|
79
|
+
getParsedType: () => getParsedType,
|
|
80
|
+
instanceof: () => instanceOfType,
|
|
81
|
+
intersection: () => intersectionType,
|
|
82
|
+
isAborted: () => isAborted,
|
|
83
|
+
isAsync: () => isAsync,
|
|
84
|
+
isDirty: () => isDirty,
|
|
85
|
+
isValid: () => isValid,
|
|
86
|
+
late: () => late,
|
|
87
|
+
lazy: () => lazyType,
|
|
88
|
+
literal: () => literalType,
|
|
89
|
+
makeIssue: () => makeIssue,
|
|
90
|
+
map: () => mapType,
|
|
91
|
+
nan: () => nanType,
|
|
92
|
+
nativeEnum: () => nativeEnumType,
|
|
93
|
+
never: () => neverType,
|
|
94
|
+
null: () => nullType,
|
|
95
|
+
nullable: () => nullableType,
|
|
96
|
+
number: () => numberType,
|
|
97
|
+
object: () => objectType,
|
|
98
|
+
objectUtil: () => objectUtil,
|
|
99
|
+
oboolean: () => oboolean,
|
|
100
|
+
onumber: () => onumber,
|
|
101
|
+
optional: () => optionalType,
|
|
102
|
+
ostring: () => ostring,
|
|
103
|
+
pipeline: () => pipelineType,
|
|
104
|
+
preprocess: () => preprocessType,
|
|
105
|
+
promise: () => promiseType,
|
|
106
|
+
quotelessJson: () => quotelessJson,
|
|
107
|
+
record: () => recordType,
|
|
108
|
+
set: () => setType,
|
|
109
|
+
setErrorMap: () => setErrorMap,
|
|
110
|
+
strictObject: () => strictObjectType,
|
|
111
|
+
string: () => stringType,
|
|
112
|
+
symbol: () => symbolType,
|
|
113
|
+
transformer: () => effectsType,
|
|
114
|
+
tuple: () => tupleType,
|
|
115
|
+
undefined: () => undefinedType,
|
|
116
|
+
union: () => unionType,
|
|
117
|
+
unknown: () => unknownType,
|
|
118
|
+
util: () => util,
|
|
119
|
+
void: () => voidType
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// ../../node_modules/zod/v3/helpers/util.js
|
|
123
|
+
var util;
|
|
124
|
+
(function(util2) {
|
|
125
|
+
util2.assertEqual = (_) => {
|
|
126
|
+
};
|
|
127
|
+
function assertIs(_arg) {
|
|
128
|
+
}
|
|
129
|
+
util2.assertIs = assertIs;
|
|
130
|
+
function assertNever(_x) {
|
|
131
|
+
throw new Error();
|
|
132
|
+
}
|
|
133
|
+
util2.assertNever = assertNever;
|
|
134
|
+
util2.arrayToEnum = (items) => {
|
|
135
|
+
const obj = {};
|
|
136
|
+
for (const item of items) {
|
|
137
|
+
obj[item] = item;
|
|
138
|
+
}
|
|
139
|
+
return obj;
|
|
140
|
+
};
|
|
141
|
+
util2.getValidEnumValues = (obj) => {
|
|
142
|
+
const validKeys = util2.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
|
|
143
|
+
const filtered = {};
|
|
144
|
+
for (const k of validKeys) {
|
|
145
|
+
filtered[k] = obj[k];
|
|
146
|
+
}
|
|
147
|
+
return util2.objectValues(filtered);
|
|
148
|
+
};
|
|
149
|
+
util2.objectValues = (obj) => {
|
|
150
|
+
return util2.objectKeys(obj).map(function(e) {
|
|
151
|
+
return obj[e];
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
util2.objectKeys = typeof Object.keys === "function" ? (obj) => Object.keys(obj) : (object) => {
|
|
155
|
+
const keys = [];
|
|
156
|
+
for (const key in object) {
|
|
157
|
+
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
158
|
+
keys.push(key);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return keys;
|
|
162
|
+
};
|
|
163
|
+
util2.find = (arr, checker) => {
|
|
164
|
+
for (const item of arr) {
|
|
165
|
+
if (checker(item))
|
|
166
|
+
return item;
|
|
167
|
+
}
|
|
168
|
+
return void 0;
|
|
169
|
+
};
|
|
170
|
+
util2.isInteger = typeof Number.isInteger === "function" ? (val) => Number.isInteger(val) : (val) => typeof val === "number" && Number.isFinite(val) && Math.floor(val) === val;
|
|
171
|
+
function joinValues(array, separator = " | ") {
|
|
172
|
+
return array.map((val) => typeof val === "string" ? `'${val}'` : val).join(separator);
|
|
173
|
+
}
|
|
174
|
+
util2.joinValues = joinValues;
|
|
175
|
+
util2.jsonStringifyReplacer = (_, value) => {
|
|
176
|
+
if (typeof value === "bigint") {
|
|
177
|
+
return value.toString();
|
|
178
|
+
}
|
|
179
|
+
return value;
|
|
180
|
+
};
|
|
181
|
+
})(util || (util = {}));
|
|
182
|
+
var objectUtil;
|
|
183
|
+
(function(objectUtil2) {
|
|
184
|
+
objectUtil2.mergeShapes = (first, second) => {
|
|
185
|
+
return {
|
|
186
|
+
...first,
|
|
187
|
+
...second
|
|
188
|
+
// second overwrites first
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
})(objectUtil || (objectUtil = {}));
|
|
192
|
+
var ZodParsedType = util.arrayToEnum([
|
|
193
|
+
"string",
|
|
194
|
+
"nan",
|
|
195
|
+
"number",
|
|
196
|
+
"integer",
|
|
197
|
+
"float",
|
|
198
|
+
"boolean",
|
|
199
|
+
"date",
|
|
200
|
+
"bigint",
|
|
201
|
+
"symbol",
|
|
202
|
+
"function",
|
|
203
|
+
"undefined",
|
|
204
|
+
"null",
|
|
205
|
+
"array",
|
|
206
|
+
"object",
|
|
207
|
+
"unknown",
|
|
208
|
+
"promise",
|
|
209
|
+
"void",
|
|
210
|
+
"never",
|
|
211
|
+
"map",
|
|
212
|
+
"set"
|
|
213
|
+
]);
|
|
214
|
+
var getParsedType = (data) => {
|
|
215
|
+
const t = typeof data;
|
|
216
|
+
switch (t) {
|
|
217
|
+
case "undefined":
|
|
218
|
+
return ZodParsedType.undefined;
|
|
219
|
+
case "string":
|
|
220
|
+
return ZodParsedType.string;
|
|
221
|
+
case "number":
|
|
222
|
+
return Number.isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
|
|
223
|
+
case "boolean":
|
|
224
|
+
return ZodParsedType.boolean;
|
|
225
|
+
case "function":
|
|
226
|
+
return ZodParsedType.function;
|
|
227
|
+
case "bigint":
|
|
228
|
+
return ZodParsedType.bigint;
|
|
229
|
+
case "symbol":
|
|
230
|
+
return ZodParsedType.symbol;
|
|
231
|
+
case "object":
|
|
232
|
+
if (Array.isArray(data)) {
|
|
233
|
+
return ZodParsedType.array;
|
|
234
|
+
}
|
|
235
|
+
if (data === null) {
|
|
236
|
+
return ZodParsedType.null;
|
|
237
|
+
}
|
|
238
|
+
if (data.then && typeof data.then === "function" && data.catch && typeof data.catch === "function") {
|
|
239
|
+
return ZodParsedType.promise;
|
|
240
|
+
}
|
|
241
|
+
if (typeof Map !== "undefined" && data instanceof Map) {
|
|
242
|
+
return ZodParsedType.map;
|
|
243
|
+
}
|
|
244
|
+
if (typeof Set !== "undefined" && data instanceof Set) {
|
|
245
|
+
return ZodParsedType.set;
|
|
246
|
+
}
|
|
247
|
+
if (typeof Date !== "undefined" && data instanceof Date) {
|
|
248
|
+
return ZodParsedType.date;
|
|
249
|
+
}
|
|
250
|
+
return ZodParsedType.object;
|
|
251
|
+
default:
|
|
252
|
+
return ZodParsedType.unknown;
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
// ../../node_modules/zod/v3/ZodError.js
|
|
257
|
+
var ZodIssueCode = util.arrayToEnum([
|
|
258
|
+
"invalid_type",
|
|
259
|
+
"invalid_literal",
|
|
260
|
+
"custom",
|
|
261
|
+
"invalid_union",
|
|
262
|
+
"invalid_union_discriminator",
|
|
263
|
+
"invalid_enum_value",
|
|
264
|
+
"unrecognized_keys",
|
|
265
|
+
"invalid_arguments",
|
|
266
|
+
"invalid_return_type",
|
|
267
|
+
"invalid_date",
|
|
268
|
+
"invalid_string",
|
|
269
|
+
"too_small",
|
|
270
|
+
"too_big",
|
|
271
|
+
"invalid_intersection_types",
|
|
272
|
+
"not_multiple_of",
|
|
273
|
+
"not_finite"
|
|
274
|
+
]);
|
|
275
|
+
var quotelessJson = (obj) => {
|
|
276
|
+
const json = JSON.stringify(obj, null, 2);
|
|
277
|
+
return json.replace(/"([^"]+)":/g, "$1:");
|
|
278
|
+
};
|
|
279
|
+
var ZodError = class _ZodError extends Error {
|
|
280
|
+
get errors() {
|
|
281
|
+
return this.issues;
|
|
282
|
+
}
|
|
283
|
+
constructor(issues) {
|
|
284
|
+
super();
|
|
285
|
+
this.issues = [];
|
|
286
|
+
this.addIssue = (sub) => {
|
|
287
|
+
this.issues = [...this.issues, sub];
|
|
288
|
+
};
|
|
289
|
+
this.addIssues = (subs = []) => {
|
|
290
|
+
this.issues = [...this.issues, ...subs];
|
|
291
|
+
};
|
|
292
|
+
const actualProto = new.target.prototype;
|
|
293
|
+
if (Object.setPrototypeOf) {
|
|
294
|
+
Object.setPrototypeOf(this, actualProto);
|
|
295
|
+
} else {
|
|
296
|
+
this.__proto__ = actualProto;
|
|
297
|
+
}
|
|
298
|
+
this.name = "ZodError";
|
|
299
|
+
this.issues = issues;
|
|
300
|
+
}
|
|
301
|
+
format(_mapper) {
|
|
302
|
+
const mapper = _mapper || function(issue) {
|
|
303
|
+
return issue.message;
|
|
304
|
+
};
|
|
305
|
+
const fieldErrors = { _errors: [] };
|
|
306
|
+
const processError = (error) => {
|
|
307
|
+
for (const issue of error.issues) {
|
|
308
|
+
if (issue.code === "invalid_union") {
|
|
309
|
+
issue.unionErrors.map(processError);
|
|
310
|
+
} else if (issue.code === "invalid_return_type") {
|
|
311
|
+
processError(issue.returnTypeError);
|
|
312
|
+
} else if (issue.code === "invalid_arguments") {
|
|
313
|
+
processError(issue.argumentsError);
|
|
314
|
+
} else if (issue.path.length === 0) {
|
|
315
|
+
fieldErrors._errors.push(mapper(issue));
|
|
316
|
+
} else {
|
|
317
|
+
let curr = fieldErrors;
|
|
318
|
+
let i = 0;
|
|
319
|
+
while (i < issue.path.length) {
|
|
320
|
+
const el = issue.path[i];
|
|
321
|
+
const terminal = i === issue.path.length - 1;
|
|
322
|
+
if (!terminal) {
|
|
323
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
324
|
+
} else {
|
|
325
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
326
|
+
curr[el]._errors.push(mapper(issue));
|
|
327
|
+
}
|
|
328
|
+
curr = curr[el];
|
|
329
|
+
i++;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
processError(this);
|
|
335
|
+
return fieldErrors;
|
|
336
|
+
}
|
|
337
|
+
static assert(value) {
|
|
338
|
+
if (!(value instanceof _ZodError)) {
|
|
339
|
+
throw new Error(`Not a ZodError: ${value}`);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
toString() {
|
|
343
|
+
return this.message;
|
|
344
|
+
}
|
|
345
|
+
get message() {
|
|
346
|
+
return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
|
|
347
|
+
}
|
|
348
|
+
get isEmpty() {
|
|
349
|
+
return this.issues.length === 0;
|
|
350
|
+
}
|
|
351
|
+
flatten(mapper = (issue) => issue.message) {
|
|
352
|
+
const fieldErrors = {};
|
|
353
|
+
const formErrors = [];
|
|
354
|
+
for (const sub of this.issues) {
|
|
355
|
+
if (sub.path.length > 0) {
|
|
356
|
+
const firstEl = sub.path[0];
|
|
357
|
+
fieldErrors[firstEl] = fieldErrors[firstEl] || [];
|
|
358
|
+
fieldErrors[firstEl].push(mapper(sub));
|
|
359
|
+
} else {
|
|
360
|
+
formErrors.push(mapper(sub));
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return { formErrors, fieldErrors };
|
|
364
|
+
}
|
|
365
|
+
get formErrors() {
|
|
366
|
+
return this.flatten();
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
ZodError.create = (issues) => {
|
|
370
|
+
const error = new ZodError(issues);
|
|
371
|
+
return error;
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
// ../../node_modules/zod/v3/locales/en.js
|
|
375
|
+
var errorMap = (issue, _ctx) => {
|
|
376
|
+
let message;
|
|
377
|
+
switch (issue.code) {
|
|
378
|
+
case ZodIssueCode.invalid_type:
|
|
379
|
+
if (issue.received === ZodParsedType.undefined) {
|
|
380
|
+
message = "Required";
|
|
381
|
+
} else {
|
|
382
|
+
message = `Expected ${issue.expected}, received ${issue.received}`;
|
|
383
|
+
}
|
|
384
|
+
break;
|
|
385
|
+
case ZodIssueCode.invalid_literal:
|
|
386
|
+
message = `Invalid literal value, expected ${JSON.stringify(issue.expected, util.jsonStringifyReplacer)}`;
|
|
387
|
+
break;
|
|
388
|
+
case ZodIssueCode.unrecognized_keys:
|
|
389
|
+
message = `Unrecognized key(s) in object: ${util.joinValues(issue.keys, ", ")}`;
|
|
390
|
+
break;
|
|
391
|
+
case ZodIssueCode.invalid_union:
|
|
392
|
+
message = `Invalid input`;
|
|
393
|
+
break;
|
|
394
|
+
case ZodIssueCode.invalid_union_discriminator:
|
|
395
|
+
message = `Invalid discriminator value. Expected ${util.joinValues(issue.options)}`;
|
|
396
|
+
break;
|
|
397
|
+
case ZodIssueCode.invalid_enum_value:
|
|
398
|
+
message = `Invalid enum value. Expected ${util.joinValues(issue.options)}, received '${issue.received}'`;
|
|
399
|
+
break;
|
|
400
|
+
case ZodIssueCode.invalid_arguments:
|
|
401
|
+
message = `Invalid function arguments`;
|
|
402
|
+
break;
|
|
403
|
+
case ZodIssueCode.invalid_return_type:
|
|
404
|
+
message = `Invalid function return type`;
|
|
405
|
+
break;
|
|
406
|
+
case ZodIssueCode.invalid_date:
|
|
407
|
+
message = `Invalid date`;
|
|
408
|
+
break;
|
|
409
|
+
case ZodIssueCode.invalid_string:
|
|
410
|
+
if (typeof issue.validation === "object") {
|
|
411
|
+
if ("includes" in issue.validation) {
|
|
412
|
+
message = `Invalid input: must include "${issue.validation.includes}"`;
|
|
413
|
+
if (typeof issue.validation.position === "number") {
|
|
414
|
+
message = `${message} at one or more positions greater than or equal to ${issue.validation.position}`;
|
|
415
|
+
}
|
|
416
|
+
} else if ("startsWith" in issue.validation) {
|
|
417
|
+
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
|
|
418
|
+
} else if ("endsWith" in issue.validation) {
|
|
419
|
+
message = `Invalid input: must end with "${issue.validation.endsWith}"`;
|
|
420
|
+
} else {
|
|
421
|
+
util.assertNever(issue.validation);
|
|
422
|
+
}
|
|
423
|
+
} else if (issue.validation !== "regex") {
|
|
424
|
+
message = `Invalid ${issue.validation}`;
|
|
425
|
+
} else {
|
|
426
|
+
message = "Invalid";
|
|
427
|
+
}
|
|
428
|
+
break;
|
|
429
|
+
case ZodIssueCode.too_small:
|
|
430
|
+
if (issue.type === "array")
|
|
431
|
+
message = `Array must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `more than`} ${issue.minimum} element(s)`;
|
|
432
|
+
else if (issue.type === "string")
|
|
433
|
+
message = `String must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
|
|
434
|
+
else if (issue.type === "number")
|
|
435
|
+
message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
|
|
436
|
+
else if (issue.type === "bigint")
|
|
437
|
+
message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
|
|
438
|
+
else if (issue.type === "date")
|
|
439
|
+
message = `Date must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${new Date(Number(issue.minimum))}`;
|
|
440
|
+
else
|
|
441
|
+
message = "Invalid input";
|
|
442
|
+
break;
|
|
443
|
+
case ZodIssueCode.too_big:
|
|
444
|
+
if (issue.type === "array")
|
|
445
|
+
message = `Array must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `less than`} ${issue.maximum} element(s)`;
|
|
446
|
+
else if (issue.type === "string")
|
|
447
|
+
message = `String must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
|
|
448
|
+
else if (issue.type === "number")
|
|
449
|
+
message = `Number must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
450
|
+
else if (issue.type === "bigint")
|
|
451
|
+
message = `BigInt must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
452
|
+
else if (issue.type === "date")
|
|
453
|
+
message = `Date must be ${issue.exact ? `exactly` : issue.inclusive ? `smaller than or equal to` : `smaller than`} ${new Date(Number(issue.maximum))}`;
|
|
454
|
+
else
|
|
455
|
+
message = "Invalid input";
|
|
456
|
+
break;
|
|
457
|
+
case ZodIssueCode.custom:
|
|
458
|
+
message = `Invalid input`;
|
|
459
|
+
break;
|
|
460
|
+
case ZodIssueCode.invalid_intersection_types:
|
|
461
|
+
message = `Intersection results could not be merged`;
|
|
462
|
+
break;
|
|
463
|
+
case ZodIssueCode.not_multiple_of:
|
|
464
|
+
message = `Number must be a multiple of ${issue.multipleOf}`;
|
|
465
|
+
break;
|
|
466
|
+
case ZodIssueCode.not_finite:
|
|
467
|
+
message = "Number must be finite";
|
|
468
|
+
break;
|
|
469
|
+
default:
|
|
470
|
+
message = _ctx.defaultError;
|
|
471
|
+
util.assertNever(issue);
|
|
472
|
+
}
|
|
473
|
+
return { message };
|
|
474
|
+
};
|
|
475
|
+
var en_default = errorMap;
|
|
476
|
+
|
|
477
|
+
// ../../node_modules/zod/v3/errors.js
|
|
478
|
+
var overrideErrorMap = en_default;
|
|
479
|
+
function setErrorMap(map) {
|
|
480
|
+
overrideErrorMap = map;
|
|
481
|
+
}
|
|
482
|
+
function getErrorMap() {
|
|
483
|
+
return overrideErrorMap;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// ../../node_modules/zod/v3/helpers/parseUtil.js
|
|
487
|
+
var makeIssue = (params) => {
|
|
488
|
+
const { data, path, errorMaps, issueData } = params;
|
|
489
|
+
const fullPath = [...path, ...issueData.path || []];
|
|
490
|
+
const fullIssue = {
|
|
491
|
+
...issueData,
|
|
492
|
+
path: fullPath
|
|
493
|
+
};
|
|
494
|
+
if (issueData.message !== void 0) {
|
|
495
|
+
return {
|
|
496
|
+
...issueData,
|
|
497
|
+
path: fullPath,
|
|
498
|
+
message: issueData.message
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
let errorMessage = "";
|
|
502
|
+
const maps = errorMaps.filter((m) => !!m).slice().reverse();
|
|
503
|
+
for (const map of maps) {
|
|
504
|
+
errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message;
|
|
505
|
+
}
|
|
506
|
+
return {
|
|
507
|
+
...issueData,
|
|
508
|
+
path: fullPath,
|
|
509
|
+
message: errorMessage
|
|
510
|
+
};
|
|
511
|
+
};
|
|
512
|
+
var EMPTY_PATH = [];
|
|
513
|
+
function addIssueToContext(ctx, issueData) {
|
|
514
|
+
const overrideMap = getErrorMap();
|
|
515
|
+
const issue = makeIssue({
|
|
516
|
+
issueData,
|
|
517
|
+
data: ctx.data,
|
|
518
|
+
path: ctx.path,
|
|
519
|
+
errorMaps: [
|
|
520
|
+
ctx.common.contextualErrorMap,
|
|
521
|
+
// contextual error map is first priority
|
|
522
|
+
ctx.schemaErrorMap,
|
|
523
|
+
// then schema-bound map if available
|
|
524
|
+
overrideMap,
|
|
525
|
+
// then global override map
|
|
526
|
+
overrideMap === en_default ? void 0 : en_default
|
|
527
|
+
// then global default map
|
|
528
|
+
].filter((x) => !!x)
|
|
529
|
+
});
|
|
530
|
+
ctx.common.issues.push(issue);
|
|
531
|
+
}
|
|
532
|
+
var ParseStatus = class _ParseStatus {
|
|
533
|
+
constructor() {
|
|
534
|
+
this.value = "valid";
|
|
535
|
+
}
|
|
536
|
+
dirty() {
|
|
537
|
+
if (this.value === "valid")
|
|
538
|
+
this.value = "dirty";
|
|
539
|
+
}
|
|
540
|
+
abort() {
|
|
541
|
+
if (this.value !== "aborted")
|
|
542
|
+
this.value = "aborted";
|
|
543
|
+
}
|
|
544
|
+
static mergeArray(status, results) {
|
|
545
|
+
const arrayValue = [];
|
|
546
|
+
for (const s of results) {
|
|
547
|
+
if (s.status === "aborted")
|
|
548
|
+
return INVALID;
|
|
549
|
+
if (s.status === "dirty")
|
|
550
|
+
status.dirty();
|
|
551
|
+
arrayValue.push(s.value);
|
|
552
|
+
}
|
|
553
|
+
return { status: status.value, value: arrayValue };
|
|
554
|
+
}
|
|
555
|
+
static async mergeObjectAsync(status, pairs) {
|
|
556
|
+
const syncPairs = [];
|
|
557
|
+
for (const pair of pairs) {
|
|
558
|
+
const key = await pair.key;
|
|
559
|
+
const value = await pair.value;
|
|
560
|
+
syncPairs.push({
|
|
561
|
+
key,
|
|
562
|
+
value
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
return _ParseStatus.mergeObjectSync(status, syncPairs);
|
|
566
|
+
}
|
|
567
|
+
static mergeObjectSync(status, pairs) {
|
|
568
|
+
const finalObject = {};
|
|
569
|
+
for (const pair of pairs) {
|
|
570
|
+
const { key, value } = pair;
|
|
571
|
+
if (key.status === "aborted")
|
|
572
|
+
return INVALID;
|
|
573
|
+
if (value.status === "aborted")
|
|
574
|
+
return INVALID;
|
|
575
|
+
if (key.status === "dirty")
|
|
576
|
+
status.dirty();
|
|
577
|
+
if (value.status === "dirty")
|
|
578
|
+
status.dirty();
|
|
579
|
+
if (key.value !== "__proto__" && (typeof value.value !== "undefined" || pair.alwaysSet)) {
|
|
580
|
+
finalObject[key.value] = value.value;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
return { status: status.value, value: finalObject };
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
var INVALID = Object.freeze({
|
|
587
|
+
status: "aborted"
|
|
588
|
+
});
|
|
589
|
+
var DIRTY = (value) => ({ status: "dirty", value });
|
|
590
|
+
var OK = (value) => ({ status: "valid", value });
|
|
591
|
+
var isAborted = (x) => x.status === "aborted";
|
|
592
|
+
var isDirty = (x) => x.status === "dirty";
|
|
593
|
+
var isValid = (x) => x.status === "valid";
|
|
594
|
+
var isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
|
|
595
|
+
|
|
596
|
+
// ../../node_modules/zod/v3/helpers/errorUtil.js
|
|
597
|
+
var errorUtil;
|
|
598
|
+
(function(errorUtil2) {
|
|
599
|
+
errorUtil2.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
600
|
+
errorUtil2.toString = (message) => typeof message === "string" ? message : message?.message;
|
|
601
|
+
})(errorUtil || (errorUtil = {}));
|
|
602
|
+
|
|
603
|
+
// ../../node_modules/zod/v3/types.js
|
|
604
|
+
var ParseInputLazyPath = class {
|
|
605
|
+
constructor(parent, value, path, key) {
|
|
606
|
+
this._cachedPath = [];
|
|
607
|
+
this.parent = parent;
|
|
608
|
+
this.data = value;
|
|
609
|
+
this._path = path;
|
|
610
|
+
this._key = key;
|
|
611
|
+
}
|
|
612
|
+
get path() {
|
|
613
|
+
if (!this._cachedPath.length) {
|
|
614
|
+
if (Array.isArray(this._key)) {
|
|
615
|
+
this._cachedPath.push(...this._path, ...this._key);
|
|
616
|
+
} else {
|
|
617
|
+
this._cachedPath.push(...this._path, this._key);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
return this._cachedPath;
|
|
621
|
+
}
|
|
622
|
+
};
|
|
623
|
+
var handleResult = (ctx, result) => {
|
|
624
|
+
if (isValid(result)) {
|
|
625
|
+
return { success: true, data: result.value };
|
|
626
|
+
} else {
|
|
627
|
+
if (!ctx.common.issues.length) {
|
|
628
|
+
throw new Error("Validation failed but no issues detected.");
|
|
629
|
+
}
|
|
630
|
+
return {
|
|
631
|
+
success: false,
|
|
632
|
+
get error() {
|
|
633
|
+
if (this._error)
|
|
634
|
+
return this._error;
|
|
635
|
+
const error = new ZodError(ctx.common.issues);
|
|
636
|
+
this._error = error;
|
|
637
|
+
return this._error;
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
};
|
|
642
|
+
function processCreateParams(params) {
|
|
643
|
+
if (!params)
|
|
644
|
+
return {};
|
|
645
|
+
const { errorMap: errorMap2, invalid_type_error, required_error, description } = params;
|
|
646
|
+
if (errorMap2 && (invalid_type_error || required_error)) {
|
|
647
|
+
throw new Error(`Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`);
|
|
648
|
+
}
|
|
649
|
+
if (errorMap2)
|
|
650
|
+
return { errorMap: errorMap2, description };
|
|
651
|
+
const customMap = (iss, ctx) => {
|
|
652
|
+
const { message } = params;
|
|
653
|
+
if (iss.code === "invalid_enum_value") {
|
|
654
|
+
return { message: message ?? ctx.defaultError };
|
|
655
|
+
}
|
|
656
|
+
if (typeof ctx.data === "undefined") {
|
|
657
|
+
return { message: message ?? required_error ?? ctx.defaultError };
|
|
658
|
+
}
|
|
659
|
+
if (iss.code !== "invalid_type")
|
|
660
|
+
return { message: ctx.defaultError };
|
|
661
|
+
return { message: message ?? invalid_type_error ?? ctx.defaultError };
|
|
662
|
+
};
|
|
663
|
+
return { errorMap: customMap, description };
|
|
664
|
+
}
|
|
665
|
+
var ZodType = class {
|
|
666
|
+
get description() {
|
|
667
|
+
return this._def.description;
|
|
668
|
+
}
|
|
669
|
+
_getType(input) {
|
|
670
|
+
return getParsedType(input.data);
|
|
671
|
+
}
|
|
672
|
+
_getOrReturnCtx(input, ctx) {
|
|
673
|
+
return ctx || {
|
|
674
|
+
common: input.parent.common,
|
|
675
|
+
data: input.data,
|
|
676
|
+
parsedType: getParsedType(input.data),
|
|
677
|
+
schemaErrorMap: this._def.errorMap,
|
|
678
|
+
path: input.path,
|
|
679
|
+
parent: input.parent
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
_processInputParams(input) {
|
|
683
|
+
return {
|
|
684
|
+
status: new ParseStatus(),
|
|
685
|
+
ctx: {
|
|
686
|
+
common: input.parent.common,
|
|
687
|
+
data: input.data,
|
|
688
|
+
parsedType: getParsedType(input.data),
|
|
689
|
+
schemaErrorMap: this._def.errorMap,
|
|
690
|
+
path: input.path,
|
|
691
|
+
parent: input.parent
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
_parseSync(input) {
|
|
696
|
+
const result = this._parse(input);
|
|
697
|
+
if (isAsync(result)) {
|
|
698
|
+
throw new Error("Synchronous parse encountered promise.");
|
|
699
|
+
}
|
|
700
|
+
return result;
|
|
701
|
+
}
|
|
702
|
+
_parseAsync(input) {
|
|
703
|
+
const result = this._parse(input);
|
|
704
|
+
return Promise.resolve(result);
|
|
705
|
+
}
|
|
706
|
+
parse(data, params) {
|
|
707
|
+
const result = this.safeParse(data, params);
|
|
708
|
+
if (result.success)
|
|
709
|
+
return result.data;
|
|
710
|
+
throw result.error;
|
|
711
|
+
}
|
|
712
|
+
safeParse(data, params) {
|
|
713
|
+
const ctx = {
|
|
714
|
+
common: {
|
|
715
|
+
issues: [],
|
|
716
|
+
async: params?.async ?? false,
|
|
717
|
+
contextualErrorMap: params?.errorMap
|
|
718
|
+
},
|
|
719
|
+
path: params?.path || [],
|
|
720
|
+
schemaErrorMap: this._def.errorMap,
|
|
721
|
+
parent: null,
|
|
722
|
+
data,
|
|
723
|
+
parsedType: getParsedType(data)
|
|
724
|
+
};
|
|
725
|
+
const result = this._parseSync({ data, path: ctx.path, parent: ctx });
|
|
726
|
+
return handleResult(ctx, result);
|
|
727
|
+
}
|
|
728
|
+
"~validate"(data) {
|
|
729
|
+
const ctx = {
|
|
730
|
+
common: {
|
|
731
|
+
issues: [],
|
|
732
|
+
async: !!this["~standard"].async
|
|
733
|
+
},
|
|
734
|
+
path: [],
|
|
735
|
+
schemaErrorMap: this._def.errorMap,
|
|
736
|
+
parent: null,
|
|
737
|
+
data,
|
|
738
|
+
parsedType: getParsedType(data)
|
|
739
|
+
};
|
|
740
|
+
if (!this["~standard"].async) {
|
|
741
|
+
try {
|
|
742
|
+
const result = this._parseSync({ data, path: [], parent: ctx });
|
|
743
|
+
return isValid(result) ? {
|
|
744
|
+
value: result.value
|
|
745
|
+
} : {
|
|
746
|
+
issues: ctx.common.issues
|
|
747
|
+
};
|
|
748
|
+
} catch (err) {
|
|
749
|
+
if (err?.message?.toLowerCase()?.includes("encountered")) {
|
|
750
|
+
this["~standard"].async = true;
|
|
751
|
+
}
|
|
752
|
+
ctx.common = {
|
|
753
|
+
issues: [],
|
|
754
|
+
async: true
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
return this._parseAsync({ data, path: [], parent: ctx }).then((result) => isValid(result) ? {
|
|
759
|
+
value: result.value
|
|
760
|
+
} : {
|
|
761
|
+
issues: ctx.common.issues
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
async parseAsync(data, params) {
|
|
765
|
+
const result = await this.safeParseAsync(data, params);
|
|
766
|
+
if (result.success)
|
|
767
|
+
return result.data;
|
|
768
|
+
throw result.error;
|
|
769
|
+
}
|
|
770
|
+
async safeParseAsync(data, params) {
|
|
771
|
+
const ctx = {
|
|
772
|
+
common: {
|
|
773
|
+
issues: [],
|
|
774
|
+
contextualErrorMap: params?.errorMap,
|
|
775
|
+
async: true
|
|
776
|
+
},
|
|
777
|
+
path: params?.path || [],
|
|
778
|
+
schemaErrorMap: this._def.errorMap,
|
|
779
|
+
parent: null,
|
|
780
|
+
data,
|
|
781
|
+
parsedType: getParsedType(data)
|
|
782
|
+
};
|
|
783
|
+
const maybeAsyncResult = this._parse({ data, path: ctx.path, parent: ctx });
|
|
784
|
+
const result = await (isAsync(maybeAsyncResult) ? maybeAsyncResult : Promise.resolve(maybeAsyncResult));
|
|
785
|
+
return handleResult(ctx, result);
|
|
786
|
+
}
|
|
787
|
+
refine(check, message) {
|
|
788
|
+
const getIssueProperties = (val) => {
|
|
789
|
+
if (typeof message === "string" || typeof message === "undefined") {
|
|
790
|
+
return { message };
|
|
791
|
+
} else if (typeof message === "function") {
|
|
792
|
+
return message(val);
|
|
793
|
+
} else {
|
|
794
|
+
return message;
|
|
795
|
+
}
|
|
796
|
+
};
|
|
797
|
+
return this._refinement((val, ctx) => {
|
|
798
|
+
const result = check(val);
|
|
799
|
+
const setError = () => ctx.addIssue({
|
|
800
|
+
code: ZodIssueCode.custom,
|
|
801
|
+
...getIssueProperties(val)
|
|
802
|
+
});
|
|
803
|
+
if (typeof Promise !== "undefined" && result instanceof Promise) {
|
|
804
|
+
return result.then((data) => {
|
|
805
|
+
if (!data) {
|
|
806
|
+
setError();
|
|
807
|
+
return false;
|
|
808
|
+
} else {
|
|
809
|
+
return true;
|
|
810
|
+
}
|
|
811
|
+
});
|
|
812
|
+
}
|
|
813
|
+
if (!result) {
|
|
814
|
+
setError();
|
|
815
|
+
return false;
|
|
816
|
+
} else {
|
|
817
|
+
return true;
|
|
818
|
+
}
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
refinement(check, refinementData) {
|
|
822
|
+
return this._refinement((val, ctx) => {
|
|
823
|
+
if (!check(val)) {
|
|
824
|
+
ctx.addIssue(typeof refinementData === "function" ? refinementData(val, ctx) : refinementData);
|
|
825
|
+
return false;
|
|
826
|
+
} else {
|
|
827
|
+
return true;
|
|
828
|
+
}
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
_refinement(refinement) {
|
|
832
|
+
return new ZodEffects({
|
|
833
|
+
schema: this,
|
|
834
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
835
|
+
effect: { type: "refinement", refinement }
|
|
836
|
+
});
|
|
837
|
+
}
|
|
838
|
+
superRefine(refinement) {
|
|
839
|
+
return this._refinement(refinement);
|
|
840
|
+
}
|
|
841
|
+
constructor(def) {
|
|
842
|
+
this.spa = this.safeParseAsync;
|
|
843
|
+
this._def = def;
|
|
844
|
+
this.parse = this.parse.bind(this);
|
|
845
|
+
this.safeParse = this.safeParse.bind(this);
|
|
846
|
+
this.parseAsync = this.parseAsync.bind(this);
|
|
847
|
+
this.safeParseAsync = this.safeParseAsync.bind(this);
|
|
848
|
+
this.spa = this.spa.bind(this);
|
|
849
|
+
this.refine = this.refine.bind(this);
|
|
850
|
+
this.refinement = this.refinement.bind(this);
|
|
851
|
+
this.superRefine = this.superRefine.bind(this);
|
|
852
|
+
this.optional = this.optional.bind(this);
|
|
853
|
+
this.nullable = this.nullable.bind(this);
|
|
854
|
+
this.nullish = this.nullish.bind(this);
|
|
855
|
+
this.array = this.array.bind(this);
|
|
856
|
+
this.promise = this.promise.bind(this);
|
|
857
|
+
this.or = this.or.bind(this);
|
|
858
|
+
this.and = this.and.bind(this);
|
|
859
|
+
this.transform = this.transform.bind(this);
|
|
860
|
+
this.brand = this.brand.bind(this);
|
|
861
|
+
this.default = this.default.bind(this);
|
|
862
|
+
this.catch = this.catch.bind(this);
|
|
863
|
+
this.describe = this.describe.bind(this);
|
|
864
|
+
this.pipe = this.pipe.bind(this);
|
|
865
|
+
this.readonly = this.readonly.bind(this);
|
|
866
|
+
this.isNullable = this.isNullable.bind(this);
|
|
867
|
+
this.isOptional = this.isOptional.bind(this);
|
|
868
|
+
this["~standard"] = {
|
|
869
|
+
version: 1,
|
|
870
|
+
vendor: "zod",
|
|
871
|
+
validate: (data) => this["~validate"](data)
|
|
872
|
+
};
|
|
873
|
+
}
|
|
874
|
+
optional() {
|
|
875
|
+
return ZodOptional.create(this, this._def);
|
|
876
|
+
}
|
|
877
|
+
nullable() {
|
|
878
|
+
return ZodNullable.create(this, this._def);
|
|
879
|
+
}
|
|
880
|
+
nullish() {
|
|
881
|
+
return this.nullable().optional();
|
|
882
|
+
}
|
|
883
|
+
array() {
|
|
884
|
+
return ZodArray.create(this);
|
|
885
|
+
}
|
|
886
|
+
promise() {
|
|
887
|
+
return ZodPromise.create(this, this._def);
|
|
888
|
+
}
|
|
889
|
+
or(option) {
|
|
890
|
+
return ZodUnion.create([this, option], this._def);
|
|
891
|
+
}
|
|
892
|
+
and(incoming) {
|
|
893
|
+
return ZodIntersection.create(this, incoming, this._def);
|
|
894
|
+
}
|
|
895
|
+
transform(transform) {
|
|
896
|
+
return new ZodEffects({
|
|
897
|
+
...processCreateParams(this._def),
|
|
898
|
+
schema: this,
|
|
899
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
900
|
+
effect: { type: "transform", transform }
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
default(def) {
|
|
904
|
+
const defaultValueFunc = typeof def === "function" ? def : () => def;
|
|
905
|
+
return new ZodDefault({
|
|
906
|
+
...processCreateParams(this._def),
|
|
907
|
+
innerType: this,
|
|
908
|
+
defaultValue: defaultValueFunc,
|
|
909
|
+
typeName: ZodFirstPartyTypeKind.ZodDefault
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
brand() {
|
|
913
|
+
return new ZodBranded({
|
|
914
|
+
typeName: ZodFirstPartyTypeKind.ZodBranded,
|
|
915
|
+
type: this,
|
|
916
|
+
...processCreateParams(this._def)
|
|
917
|
+
});
|
|
918
|
+
}
|
|
919
|
+
catch(def) {
|
|
920
|
+
const catchValueFunc = typeof def === "function" ? def : () => def;
|
|
921
|
+
return new ZodCatch({
|
|
922
|
+
...processCreateParams(this._def),
|
|
923
|
+
innerType: this,
|
|
924
|
+
catchValue: catchValueFunc,
|
|
925
|
+
typeName: ZodFirstPartyTypeKind.ZodCatch
|
|
926
|
+
});
|
|
927
|
+
}
|
|
928
|
+
describe(description) {
|
|
929
|
+
const This = this.constructor;
|
|
930
|
+
return new This({
|
|
931
|
+
...this._def,
|
|
932
|
+
description
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
pipe(target) {
|
|
936
|
+
return ZodPipeline.create(this, target);
|
|
937
|
+
}
|
|
938
|
+
readonly() {
|
|
939
|
+
return ZodReadonly.create(this);
|
|
940
|
+
}
|
|
941
|
+
isOptional() {
|
|
942
|
+
return this.safeParse(void 0).success;
|
|
943
|
+
}
|
|
944
|
+
isNullable() {
|
|
945
|
+
return this.safeParse(null).success;
|
|
946
|
+
}
|
|
947
|
+
};
|
|
948
|
+
var cuidRegex = /^c[^\s-]{8,}$/i;
|
|
949
|
+
var cuid2Regex = /^[0-9a-z]+$/;
|
|
950
|
+
var ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
|
|
951
|
+
var uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
|
|
952
|
+
var nanoidRegex = /^[a-z0-9_-]{21}$/i;
|
|
953
|
+
var jwtRegex = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/;
|
|
954
|
+
var durationRegex = /^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
|
|
955
|
+
var emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
|
|
956
|
+
var _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
957
|
+
var emojiRegex;
|
|
958
|
+
var ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
|
|
959
|
+
var ipv4CidrRegex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/(3[0-2]|[12]?[0-9])$/;
|
|
960
|
+
var ipv6Regex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;
|
|
961
|
+
var ipv6CidrRegex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/;
|
|
962
|
+
var base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
963
|
+
var base64urlRegex = /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/;
|
|
964
|
+
var dateRegexSource = `((\\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])))`;
|
|
965
|
+
var dateRegex = new RegExp(`^${dateRegexSource}$`);
|
|
966
|
+
function timeRegexSource(args) {
|
|
967
|
+
let secondsRegexSource = `[0-5]\\d`;
|
|
968
|
+
if (args.precision) {
|
|
969
|
+
secondsRegexSource = `${secondsRegexSource}\\.\\d{${args.precision}}`;
|
|
970
|
+
} else if (args.precision == null) {
|
|
971
|
+
secondsRegexSource = `${secondsRegexSource}(\\.\\d+)?`;
|
|
972
|
+
}
|
|
973
|
+
const secondsQuantifier = args.precision ? "+" : "?";
|
|
974
|
+
return `([01]\\d|2[0-3]):[0-5]\\d(:${secondsRegexSource})${secondsQuantifier}`;
|
|
975
|
+
}
|
|
976
|
+
function timeRegex(args) {
|
|
977
|
+
return new RegExp(`^${timeRegexSource(args)}$`);
|
|
978
|
+
}
|
|
979
|
+
function datetimeRegex(args) {
|
|
980
|
+
let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
|
|
981
|
+
const opts = [];
|
|
982
|
+
opts.push(args.local ? `Z?` : `Z`);
|
|
983
|
+
if (args.offset)
|
|
984
|
+
opts.push(`([+-]\\d{2}:?\\d{2})`);
|
|
985
|
+
regex = `${regex}(${opts.join("|")})`;
|
|
986
|
+
return new RegExp(`^${regex}$`);
|
|
987
|
+
}
|
|
988
|
+
function isValidIP(ip, version) {
|
|
989
|
+
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
|
|
990
|
+
return true;
|
|
991
|
+
}
|
|
992
|
+
if ((version === "v6" || !version) && ipv6Regex.test(ip)) {
|
|
993
|
+
return true;
|
|
994
|
+
}
|
|
995
|
+
return false;
|
|
996
|
+
}
|
|
997
|
+
function isValidJWT(jwt, alg) {
|
|
998
|
+
if (!jwtRegex.test(jwt))
|
|
999
|
+
return false;
|
|
1000
|
+
try {
|
|
1001
|
+
const [header] = jwt.split(".");
|
|
1002
|
+
if (!header)
|
|
1003
|
+
return false;
|
|
1004
|
+
const base64 = header.replace(/-/g, "+").replace(/_/g, "/").padEnd(header.length + (4 - header.length % 4) % 4, "=");
|
|
1005
|
+
const decoded = JSON.parse(atob(base64));
|
|
1006
|
+
if (typeof decoded !== "object" || decoded === null)
|
|
1007
|
+
return false;
|
|
1008
|
+
if ("typ" in decoded && decoded?.typ !== "JWT")
|
|
1009
|
+
return false;
|
|
1010
|
+
if (!decoded.alg)
|
|
1011
|
+
return false;
|
|
1012
|
+
if (alg && decoded.alg !== alg)
|
|
1013
|
+
return false;
|
|
1014
|
+
return true;
|
|
1015
|
+
} catch {
|
|
1016
|
+
return false;
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
function isValidCidr(ip, version) {
|
|
1020
|
+
if ((version === "v4" || !version) && ipv4CidrRegex.test(ip)) {
|
|
1021
|
+
return true;
|
|
1022
|
+
}
|
|
1023
|
+
if ((version === "v6" || !version) && ipv6CidrRegex.test(ip)) {
|
|
1024
|
+
return true;
|
|
1025
|
+
}
|
|
1026
|
+
return false;
|
|
1027
|
+
}
|
|
1028
|
+
var ZodString = class _ZodString extends ZodType {
|
|
1029
|
+
_parse(input) {
|
|
1030
|
+
if (this._def.coerce) {
|
|
1031
|
+
input.data = String(input.data);
|
|
1032
|
+
}
|
|
1033
|
+
const parsedType = this._getType(input);
|
|
1034
|
+
if (parsedType !== ZodParsedType.string) {
|
|
1035
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
1036
|
+
addIssueToContext(ctx2, {
|
|
1037
|
+
code: ZodIssueCode.invalid_type,
|
|
1038
|
+
expected: ZodParsedType.string,
|
|
1039
|
+
received: ctx2.parsedType
|
|
1040
|
+
});
|
|
1041
|
+
return INVALID;
|
|
1042
|
+
}
|
|
1043
|
+
const status = new ParseStatus();
|
|
1044
|
+
let ctx = void 0;
|
|
1045
|
+
for (const check of this._def.checks) {
|
|
1046
|
+
if (check.kind === "min") {
|
|
1047
|
+
if (input.data.length < check.value) {
|
|
1048
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1049
|
+
addIssueToContext(ctx, {
|
|
1050
|
+
code: ZodIssueCode.too_small,
|
|
1051
|
+
minimum: check.value,
|
|
1052
|
+
type: "string",
|
|
1053
|
+
inclusive: true,
|
|
1054
|
+
exact: false,
|
|
1055
|
+
message: check.message
|
|
1056
|
+
});
|
|
1057
|
+
status.dirty();
|
|
1058
|
+
}
|
|
1059
|
+
} else if (check.kind === "max") {
|
|
1060
|
+
if (input.data.length > check.value) {
|
|
1061
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1062
|
+
addIssueToContext(ctx, {
|
|
1063
|
+
code: ZodIssueCode.too_big,
|
|
1064
|
+
maximum: check.value,
|
|
1065
|
+
type: "string",
|
|
1066
|
+
inclusive: true,
|
|
1067
|
+
exact: false,
|
|
1068
|
+
message: check.message
|
|
1069
|
+
});
|
|
1070
|
+
status.dirty();
|
|
1071
|
+
}
|
|
1072
|
+
} else if (check.kind === "length") {
|
|
1073
|
+
const tooBig = input.data.length > check.value;
|
|
1074
|
+
const tooSmall = input.data.length < check.value;
|
|
1075
|
+
if (tooBig || tooSmall) {
|
|
1076
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1077
|
+
if (tooBig) {
|
|
1078
|
+
addIssueToContext(ctx, {
|
|
1079
|
+
code: ZodIssueCode.too_big,
|
|
1080
|
+
maximum: check.value,
|
|
1081
|
+
type: "string",
|
|
1082
|
+
inclusive: true,
|
|
1083
|
+
exact: true,
|
|
1084
|
+
message: check.message
|
|
1085
|
+
});
|
|
1086
|
+
} else if (tooSmall) {
|
|
1087
|
+
addIssueToContext(ctx, {
|
|
1088
|
+
code: ZodIssueCode.too_small,
|
|
1089
|
+
minimum: check.value,
|
|
1090
|
+
type: "string",
|
|
1091
|
+
inclusive: true,
|
|
1092
|
+
exact: true,
|
|
1093
|
+
message: check.message
|
|
1094
|
+
});
|
|
1095
|
+
}
|
|
1096
|
+
status.dirty();
|
|
1097
|
+
}
|
|
1098
|
+
} else if (check.kind === "email") {
|
|
1099
|
+
if (!emailRegex.test(input.data)) {
|
|
1100
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1101
|
+
addIssueToContext(ctx, {
|
|
1102
|
+
validation: "email",
|
|
1103
|
+
code: ZodIssueCode.invalid_string,
|
|
1104
|
+
message: check.message
|
|
1105
|
+
});
|
|
1106
|
+
status.dirty();
|
|
1107
|
+
}
|
|
1108
|
+
} else if (check.kind === "emoji") {
|
|
1109
|
+
if (!emojiRegex) {
|
|
1110
|
+
emojiRegex = new RegExp(_emojiRegex, "u");
|
|
1111
|
+
}
|
|
1112
|
+
if (!emojiRegex.test(input.data)) {
|
|
1113
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1114
|
+
addIssueToContext(ctx, {
|
|
1115
|
+
validation: "emoji",
|
|
1116
|
+
code: ZodIssueCode.invalid_string,
|
|
1117
|
+
message: check.message
|
|
1118
|
+
});
|
|
1119
|
+
status.dirty();
|
|
1120
|
+
}
|
|
1121
|
+
} else if (check.kind === "uuid") {
|
|
1122
|
+
if (!uuidRegex.test(input.data)) {
|
|
1123
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1124
|
+
addIssueToContext(ctx, {
|
|
1125
|
+
validation: "uuid",
|
|
1126
|
+
code: ZodIssueCode.invalid_string,
|
|
1127
|
+
message: check.message
|
|
1128
|
+
});
|
|
1129
|
+
status.dirty();
|
|
1130
|
+
}
|
|
1131
|
+
} else if (check.kind === "nanoid") {
|
|
1132
|
+
if (!nanoidRegex.test(input.data)) {
|
|
1133
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1134
|
+
addIssueToContext(ctx, {
|
|
1135
|
+
validation: "nanoid",
|
|
1136
|
+
code: ZodIssueCode.invalid_string,
|
|
1137
|
+
message: check.message
|
|
1138
|
+
});
|
|
1139
|
+
status.dirty();
|
|
1140
|
+
}
|
|
1141
|
+
} else if (check.kind === "cuid") {
|
|
1142
|
+
if (!cuidRegex.test(input.data)) {
|
|
1143
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1144
|
+
addIssueToContext(ctx, {
|
|
1145
|
+
validation: "cuid",
|
|
1146
|
+
code: ZodIssueCode.invalid_string,
|
|
1147
|
+
message: check.message
|
|
1148
|
+
});
|
|
1149
|
+
status.dirty();
|
|
1150
|
+
}
|
|
1151
|
+
} else if (check.kind === "cuid2") {
|
|
1152
|
+
if (!cuid2Regex.test(input.data)) {
|
|
1153
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1154
|
+
addIssueToContext(ctx, {
|
|
1155
|
+
validation: "cuid2",
|
|
1156
|
+
code: ZodIssueCode.invalid_string,
|
|
1157
|
+
message: check.message
|
|
1158
|
+
});
|
|
1159
|
+
status.dirty();
|
|
1160
|
+
}
|
|
1161
|
+
} else if (check.kind === "ulid") {
|
|
1162
|
+
if (!ulidRegex.test(input.data)) {
|
|
1163
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1164
|
+
addIssueToContext(ctx, {
|
|
1165
|
+
validation: "ulid",
|
|
1166
|
+
code: ZodIssueCode.invalid_string,
|
|
1167
|
+
message: check.message
|
|
1168
|
+
});
|
|
1169
|
+
status.dirty();
|
|
1170
|
+
}
|
|
1171
|
+
} else if (check.kind === "url") {
|
|
1172
|
+
try {
|
|
1173
|
+
new URL(input.data);
|
|
1174
|
+
} catch {
|
|
1175
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1176
|
+
addIssueToContext(ctx, {
|
|
1177
|
+
validation: "url",
|
|
1178
|
+
code: ZodIssueCode.invalid_string,
|
|
1179
|
+
message: check.message
|
|
1180
|
+
});
|
|
1181
|
+
status.dirty();
|
|
1182
|
+
}
|
|
1183
|
+
} else if (check.kind === "regex") {
|
|
1184
|
+
check.regex.lastIndex = 0;
|
|
1185
|
+
const testResult = check.regex.test(input.data);
|
|
1186
|
+
if (!testResult) {
|
|
1187
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1188
|
+
addIssueToContext(ctx, {
|
|
1189
|
+
validation: "regex",
|
|
1190
|
+
code: ZodIssueCode.invalid_string,
|
|
1191
|
+
message: check.message
|
|
1192
|
+
});
|
|
1193
|
+
status.dirty();
|
|
1194
|
+
}
|
|
1195
|
+
} else if (check.kind === "trim") {
|
|
1196
|
+
input.data = input.data.trim();
|
|
1197
|
+
} else if (check.kind === "includes") {
|
|
1198
|
+
if (!input.data.includes(check.value, check.position)) {
|
|
1199
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1200
|
+
addIssueToContext(ctx, {
|
|
1201
|
+
code: ZodIssueCode.invalid_string,
|
|
1202
|
+
validation: { includes: check.value, position: check.position },
|
|
1203
|
+
message: check.message
|
|
1204
|
+
});
|
|
1205
|
+
status.dirty();
|
|
1206
|
+
}
|
|
1207
|
+
} else if (check.kind === "toLowerCase") {
|
|
1208
|
+
input.data = input.data.toLowerCase();
|
|
1209
|
+
} else if (check.kind === "toUpperCase") {
|
|
1210
|
+
input.data = input.data.toUpperCase();
|
|
1211
|
+
} else if (check.kind === "startsWith") {
|
|
1212
|
+
if (!input.data.startsWith(check.value)) {
|
|
1213
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1214
|
+
addIssueToContext(ctx, {
|
|
1215
|
+
code: ZodIssueCode.invalid_string,
|
|
1216
|
+
validation: { startsWith: check.value },
|
|
1217
|
+
message: check.message
|
|
1218
|
+
});
|
|
1219
|
+
status.dirty();
|
|
1220
|
+
}
|
|
1221
|
+
} else if (check.kind === "endsWith") {
|
|
1222
|
+
if (!input.data.endsWith(check.value)) {
|
|
1223
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1224
|
+
addIssueToContext(ctx, {
|
|
1225
|
+
code: ZodIssueCode.invalid_string,
|
|
1226
|
+
validation: { endsWith: check.value },
|
|
1227
|
+
message: check.message
|
|
1228
|
+
});
|
|
1229
|
+
status.dirty();
|
|
1230
|
+
}
|
|
1231
|
+
} else if (check.kind === "datetime") {
|
|
1232
|
+
const regex = datetimeRegex(check);
|
|
1233
|
+
if (!regex.test(input.data)) {
|
|
1234
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1235
|
+
addIssueToContext(ctx, {
|
|
1236
|
+
code: ZodIssueCode.invalid_string,
|
|
1237
|
+
validation: "datetime",
|
|
1238
|
+
message: check.message
|
|
1239
|
+
});
|
|
1240
|
+
status.dirty();
|
|
1241
|
+
}
|
|
1242
|
+
} else if (check.kind === "date") {
|
|
1243
|
+
const regex = dateRegex;
|
|
1244
|
+
if (!regex.test(input.data)) {
|
|
1245
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1246
|
+
addIssueToContext(ctx, {
|
|
1247
|
+
code: ZodIssueCode.invalid_string,
|
|
1248
|
+
validation: "date",
|
|
1249
|
+
message: check.message
|
|
1250
|
+
});
|
|
1251
|
+
status.dirty();
|
|
1252
|
+
}
|
|
1253
|
+
} else if (check.kind === "time") {
|
|
1254
|
+
const regex = timeRegex(check);
|
|
1255
|
+
if (!regex.test(input.data)) {
|
|
1256
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1257
|
+
addIssueToContext(ctx, {
|
|
1258
|
+
code: ZodIssueCode.invalid_string,
|
|
1259
|
+
validation: "time",
|
|
1260
|
+
message: check.message
|
|
1261
|
+
});
|
|
1262
|
+
status.dirty();
|
|
1263
|
+
}
|
|
1264
|
+
} else if (check.kind === "duration") {
|
|
1265
|
+
if (!durationRegex.test(input.data)) {
|
|
1266
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1267
|
+
addIssueToContext(ctx, {
|
|
1268
|
+
validation: "duration",
|
|
1269
|
+
code: ZodIssueCode.invalid_string,
|
|
1270
|
+
message: check.message
|
|
1271
|
+
});
|
|
1272
|
+
status.dirty();
|
|
1273
|
+
}
|
|
1274
|
+
} else if (check.kind === "ip") {
|
|
1275
|
+
if (!isValidIP(input.data, check.version)) {
|
|
1276
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1277
|
+
addIssueToContext(ctx, {
|
|
1278
|
+
validation: "ip",
|
|
1279
|
+
code: ZodIssueCode.invalid_string,
|
|
1280
|
+
message: check.message
|
|
1281
|
+
});
|
|
1282
|
+
status.dirty();
|
|
1283
|
+
}
|
|
1284
|
+
} else if (check.kind === "jwt") {
|
|
1285
|
+
if (!isValidJWT(input.data, check.alg)) {
|
|
1286
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1287
|
+
addIssueToContext(ctx, {
|
|
1288
|
+
validation: "jwt",
|
|
1289
|
+
code: ZodIssueCode.invalid_string,
|
|
1290
|
+
message: check.message
|
|
1291
|
+
});
|
|
1292
|
+
status.dirty();
|
|
1293
|
+
}
|
|
1294
|
+
} else if (check.kind === "cidr") {
|
|
1295
|
+
if (!isValidCidr(input.data, check.version)) {
|
|
1296
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1297
|
+
addIssueToContext(ctx, {
|
|
1298
|
+
validation: "cidr",
|
|
1299
|
+
code: ZodIssueCode.invalid_string,
|
|
1300
|
+
message: check.message
|
|
1301
|
+
});
|
|
1302
|
+
status.dirty();
|
|
1303
|
+
}
|
|
1304
|
+
} else if (check.kind === "base64") {
|
|
1305
|
+
if (!base64Regex.test(input.data)) {
|
|
1306
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1307
|
+
addIssueToContext(ctx, {
|
|
1308
|
+
validation: "base64",
|
|
1309
|
+
code: ZodIssueCode.invalid_string,
|
|
1310
|
+
message: check.message
|
|
1311
|
+
});
|
|
1312
|
+
status.dirty();
|
|
1313
|
+
}
|
|
1314
|
+
} else if (check.kind === "base64url") {
|
|
1315
|
+
if (!base64urlRegex.test(input.data)) {
|
|
1316
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1317
|
+
addIssueToContext(ctx, {
|
|
1318
|
+
validation: "base64url",
|
|
1319
|
+
code: ZodIssueCode.invalid_string,
|
|
1320
|
+
message: check.message
|
|
1321
|
+
});
|
|
1322
|
+
status.dirty();
|
|
1323
|
+
}
|
|
1324
|
+
} else {
|
|
1325
|
+
util.assertNever(check);
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
return { status: status.value, value: input.data };
|
|
1329
|
+
}
|
|
1330
|
+
_regex(regex, validation, message) {
|
|
1331
|
+
return this.refinement((data) => regex.test(data), {
|
|
1332
|
+
validation,
|
|
1333
|
+
code: ZodIssueCode.invalid_string,
|
|
1334
|
+
...errorUtil.errToObj(message)
|
|
1335
|
+
});
|
|
1336
|
+
}
|
|
1337
|
+
_addCheck(check) {
|
|
1338
|
+
return new _ZodString({
|
|
1339
|
+
...this._def,
|
|
1340
|
+
checks: [...this._def.checks, check]
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
1343
|
+
email(message) {
|
|
1344
|
+
return this._addCheck({ kind: "email", ...errorUtil.errToObj(message) });
|
|
1345
|
+
}
|
|
1346
|
+
url(message) {
|
|
1347
|
+
return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
|
|
1348
|
+
}
|
|
1349
|
+
emoji(message) {
|
|
1350
|
+
return this._addCheck({ kind: "emoji", ...errorUtil.errToObj(message) });
|
|
1351
|
+
}
|
|
1352
|
+
uuid(message) {
|
|
1353
|
+
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
|
|
1354
|
+
}
|
|
1355
|
+
nanoid(message) {
|
|
1356
|
+
return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) });
|
|
1357
|
+
}
|
|
1358
|
+
cuid(message) {
|
|
1359
|
+
return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
|
|
1360
|
+
}
|
|
1361
|
+
cuid2(message) {
|
|
1362
|
+
return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(message) });
|
|
1363
|
+
}
|
|
1364
|
+
ulid(message) {
|
|
1365
|
+
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
|
|
1366
|
+
}
|
|
1367
|
+
base64(message) {
|
|
1368
|
+
return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
|
|
1369
|
+
}
|
|
1370
|
+
base64url(message) {
|
|
1371
|
+
return this._addCheck({
|
|
1372
|
+
kind: "base64url",
|
|
1373
|
+
...errorUtil.errToObj(message)
|
|
1374
|
+
});
|
|
1375
|
+
}
|
|
1376
|
+
jwt(options) {
|
|
1377
|
+
return this._addCheck({ kind: "jwt", ...errorUtil.errToObj(options) });
|
|
1378
|
+
}
|
|
1379
|
+
ip(options) {
|
|
1380
|
+
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
|
|
1381
|
+
}
|
|
1382
|
+
cidr(options) {
|
|
1383
|
+
return this._addCheck({ kind: "cidr", ...errorUtil.errToObj(options) });
|
|
1384
|
+
}
|
|
1385
|
+
datetime(options) {
|
|
1386
|
+
if (typeof options === "string") {
|
|
1387
|
+
return this._addCheck({
|
|
1388
|
+
kind: "datetime",
|
|
1389
|
+
precision: null,
|
|
1390
|
+
offset: false,
|
|
1391
|
+
local: false,
|
|
1392
|
+
message: options
|
|
1393
|
+
});
|
|
1394
|
+
}
|
|
1395
|
+
return this._addCheck({
|
|
1396
|
+
kind: "datetime",
|
|
1397
|
+
precision: typeof options?.precision === "undefined" ? null : options?.precision,
|
|
1398
|
+
offset: options?.offset ?? false,
|
|
1399
|
+
local: options?.local ?? false,
|
|
1400
|
+
...errorUtil.errToObj(options?.message)
|
|
1401
|
+
});
|
|
1402
|
+
}
|
|
1403
|
+
date(message) {
|
|
1404
|
+
return this._addCheck({ kind: "date", message });
|
|
1405
|
+
}
|
|
1406
|
+
time(options) {
|
|
1407
|
+
if (typeof options === "string") {
|
|
1408
|
+
return this._addCheck({
|
|
1409
|
+
kind: "time",
|
|
1410
|
+
precision: null,
|
|
1411
|
+
message: options
|
|
1412
|
+
});
|
|
1413
|
+
}
|
|
1414
|
+
return this._addCheck({
|
|
1415
|
+
kind: "time",
|
|
1416
|
+
precision: typeof options?.precision === "undefined" ? null : options?.precision,
|
|
1417
|
+
...errorUtil.errToObj(options?.message)
|
|
1418
|
+
});
|
|
1419
|
+
}
|
|
1420
|
+
duration(message) {
|
|
1421
|
+
return this._addCheck({ kind: "duration", ...errorUtil.errToObj(message) });
|
|
1422
|
+
}
|
|
1423
|
+
regex(regex, message) {
|
|
1424
|
+
return this._addCheck({
|
|
1425
|
+
kind: "regex",
|
|
1426
|
+
regex,
|
|
1427
|
+
...errorUtil.errToObj(message)
|
|
1428
|
+
});
|
|
1429
|
+
}
|
|
1430
|
+
includes(value, options) {
|
|
1431
|
+
return this._addCheck({
|
|
1432
|
+
kind: "includes",
|
|
1433
|
+
value,
|
|
1434
|
+
position: options?.position,
|
|
1435
|
+
...errorUtil.errToObj(options?.message)
|
|
1436
|
+
});
|
|
1437
|
+
}
|
|
1438
|
+
startsWith(value, message) {
|
|
1439
|
+
return this._addCheck({
|
|
1440
|
+
kind: "startsWith",
|
|
1441
|
+
value,
|
|
1442
|
+
...errorUtil.errToObj(message)
|
|
1443
|
+
});
|
|
1444
|
+
}
|
|
1445
|
+
endsWith(value, message) {
|
|
1446
|
+
return this._addCheck({
|
|
1447
|
+
kind: "endsWith",
|
|
1448
|
+
value,
|
|
1449
|
+
...errorUtil.errToObj(message)
|
|
1450
|
+
});
|
|
1451
|
+
}
|
|
1452
|
+
min(minLength, message) {
|
|
1453
|
+
return this._addCheck({
|
|
1454
|
+
kind: "min",
|
|
1455
|
+
value: minLength,
|
|
1456
|
+
...errorUtil.errToObj(message)
|
|
1457
|
+
});
|
|
1458
|
+
}
|
|
1459
|
+
max(maxLength, message) {
|
|
1460
|
+
return this._addCheck({
|
|
1461
|
+
kind: "max",
|
|
1462
|
+
value: maxLength,
|
|
1463
|
+
...errorUtil.errToObj(message)
|
|
1464
|
+
});
|
|
1465
|
+
}
|
|
1466
|
+
length(len, message) {
|
|
1467
|
+
return this._addCheck({
|
|
1468
|
+
kind: "length",
|
|
1469
|
+
value: len,
|
|
1470
|
+
...errorUtil.errToObj(message)
|
|
1471
|
+
});
|
|
1472
|
+
}
|
|
1473
|
+
/**
|
|
1474
|
+
* Equivalent to `.min(1)`
|
|
1475
|
+
*/
|
|
1476
|
+
nonempty(message) {
|
|
1477
|
+
return this.min(1, errorUtil.errToObj(message));
|
|
1478
|
+
}
|
|
1479
|
+
trim() {
|
|
1480
|
+
return new _ZodString({
|
|
1481
|
+
...this._def,
|
|
1482
|
+
checks: [...this._def.checks, { kind: "trim" }]
|
|
1483
|
+
});
|
|
1484
|
+
}
|
|
1485
|
+
toLowerCase() {
|
|
1486
|
+
return new _ZodString({
|
|
1487
|
+
...this._def,
|
|
1488
|
+
checks: [...this._def.checks, { kind: "toLowerCase" }]
|
|
1489
|
+
});
|
|
1490
|
+
}
|
|
1491
|
+
toUpperCase() {
|
|
1492
|
+
return new _ZodString({
|
|
1493
|
+
...this._def,
|
|
1494
|
+
checks: [...this._def.checks, { kind: "toUpperCase" }]
|
|
1495
|
+
});
|
|
1496
|
+
}
|
|
1497
|
+
get isDatetime() {
|
|
1498
|
+
return !!this._def.checks.find((ch) => ch.kind === "datetime");
|
|
1499
|
+
}
|
|
1500
|
+
get isDate() {
|
|
1501
|
+
return !!this._def.checks.find((ch) => ch.kind === "date");
|
|
1502
|
+
}
|
|
1503
|
+
get isTime() {
|
|
1504
|
+
return !!this._def.checks.find((ch) => ch.kind === "time");
|
|
1505
|
+
}
|
|
1506
|
+
get isDuration() {
|
|
1507
|
+
return !!this._def.checks.find((ch) => ch.kind === "duration");
|
|
1508
|
+
}
|
|
1509
|
+
get isEmail() {
|
|
1510
|
+
return !!this._def.checks.find((ch) => ch.kind === "email");
|
|
1511
|
+
}
|
|
1512
|
+
get isURL() {
|
|
1513
|
+
return !!this._def.checks.find((ch) => ch.kind === "url");
|
|
1514
|
+
}
|
|
1515
|
+
get isEmoji() {
|
|
1516
|
+
return !!this._def.checks.find((ch) => ch.kind === "emoji");
|
|
1517
|
+
}
|
|
1518
|
+
get isUUID() {
|
|
1519
|
+
return !!this._def.checks.find((ch) => ch.kind === "uuid");
|
|
1520
|
+
}
|
|
1521
|
+
get isNANOID() {
|
|
1522
|
+
return !!this._def.checks.find((ch) => ch.kind === "nanoid");
|
|
1523
|
+
}
|
|
1524
|
+
get isCUID() {
|
|
1525
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid");
|
|
1526
|
+
}
|
|
1527
|
+
get isCUID2() {
|
|
1528
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid2");
|
|
1529
|
+
}
|
|
1530
|
+
get isULID() {
|
|
1531
|
+
return !!this._def.checks.find((ch) => ch.kind === "ulid");
|
|
1532
|
+
}
|
|
1533
|
+
get isIP() {
|
|
1534
|
+
return !!this._def.checks.find((ch) => ch.kind === "ip");
|
|
1535
|
+
}
|
|
1536
|
+
get isCIDR() {
|
|
1537
|
+
return !!this._def.checks.find((ch) => ch.kind === "cidr");
|
|
1538
|
+
}
|
|
1539
|
+
get isBase64() {
|
|
1540
|
+
return !!this._def.checks.find((ch) => ch.kind === "base64");
|
|
1541
|
+
}
|
|
1542
|
+
get isBase64url() {
|
|
1543
|
+
return !!this._def.checks.find((ch) => ch.kind === "base64url");
|
|
1544
|
+
}
|
|
1545
|
+
get minLength() {
|
|
1546
|
+
let min = null;
|
|
1547
|
+
for (const ch of this._def.checks) {
|
|
1548
|
+
if (ch.kind === "min") {
|
|
1549
|
+
if (min === null || ch.value > min)
|
|
1550
|
+
min = ch.value;
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
return min;
|
|
1554
|
+
}
|
|
1555
|
+
get maxLength() {
|
|
1556
|
+
let max = null;
|
|
1557
|
+
for (const ch of this._def.checks) {
|
|
1558
|
+
if (ch.kind === "max") {
|
|
1559
|
+
if (max === null || ch.value < max)
|
|
1560
|
+
max = ch.value;
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1563
|
+
return max;
|
|
1564
|
+
}
|
|
1565
|
+
};
|
|
1566
|
+
ZodString.create = (params) => {
|
|
1567
|
+
return new ZodString({
|
|
1568
|
+
checks: [],
|
|
1569
|
+
typeName: ZodFirstPartyTypeKind.ZodString,
|
|
1570
|
+
coerce: params?.coerce ?? false,
|
|
1571
|
+
...processCreateParams(params)
|
|
1572
|
+
});
|
|
1573
|
+
};
|
|
1574
|
+
function floatSafeRemainder(val, step) {
|
|
1575
|
+
const valDecCount = (val.toString().split(".")[1] || "").length;
|
|
1576
|
+
const stepDecCount = (step.toString().split(".")[1] || "").length;
|
|
1577
|
+
const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
|
|
1578
|
+
const valInt = Number.parseInt(val.toFixed(decCount).replace(".", ""));
|
|
1579
|
+
const stepInt = Number.parseInt(step.toFixed(decCount).replace(".", ""));
|
|
1580
|
+
return valInt % stepInt / 10 ** decCount;
|
|
1581
|
+
}
|
|
1582
|
+
var ZodNumber = class _ZodNumber extends ZodType {
|
|
1583
|
+
constructor() {
|
|
1584
|
+
super(...arguments);
|
|
1585
|
+
this.min = this.gte;
|
|
1586
|
+
this.max = this.lte;
|
|
1587
|
+
this.step = this.multipleOf;
|
|
1588
|
+
}
|
|
1589
|
+
_parse(input) {
|
|
1590
|
+
if (this._def.coerce) {
|
|
1591
|
+
input.data = Number(input.data);
|
|
1592
|
+
}
|
|
1593
|
+
const parsedType = this._getType(input);
|
|
1594
|
+
if (parsedType !== ZodParsedType.number) {
|
|
1595
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
1596
|
+
addIssueToContext(ctx2, {
|
|
1597
|
+
code: ZodIssueCode.invalid_type,
|
|
1598
|
+
expected: ZodParsedType.number,
|
|
1599
|
+
received: ctx2.parsedType
|
|
1600
|
+
});
|
|
1601
|
+
return INVALID;
|
|
1602
|
+
}
|
|
1603
|
+
let ctx = void 0;
|
|
1604
|
+
const status = new ParseStatus();
|
|
1605
|
+
for (const check of this._def.checks) {
|
|
1606
|
+
if (check.kind === "int") {
|
|
1607
|
+
if (!util.isInteger(input.data)) {
|
|
1608
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1609
|
+
addIssueToContext(ctx, {
|
|
1610
|
+
code: ZodIssueCode.invalid_type,
|
|
1611
|
+
expected: "integer",
|
|
1612
|
+
received: "float",
|
|
1613
|
+
message: check.message
|
|
1614
|
+
});
|
|
1615
|
+
status.dirty();
|
|
1616
|
+
}
|
|
1617
|
+
} else if (check.kind === "min") {
|
|
1618
|
+
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
1619
|
+
if (tooSmall) {
|
|
1620
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1621
|
+
addIssueToContext(ctx, {
|
|
1622
|
+
code: ZodIssueCode.too_small,
|
|
1623
|
+
minimum: check.value,
|
|
1624
|
+
type: "number",
|
|
1625
|
+
inclusive: check.inclusive,
|
|
1626
|
+
exact: false,
|
|
1627
|
+
message: check.message
|
|
1628
|
+
});
|
|
1629
|
+
status.dirty();
|
|
1630
|
+
}
|
|
1631
|
+
} else if (check.kind === "max") {
|
|
1632
|
+
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
1633
|
+
if (tooBig) {
|
|
1634
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1635
|
+
addIssueToContext(ctx, {
|
|
1636
|
+
code: ZodIssueCode.too_big,
|
|
1637
|
+
maximum: check.value,
|
|
1638
|
+
type: "number",
|
|
1639
|
+
inclusive: check.inclusive,
|
|
1640
|
+
exact: false,
|
|
1641
|
+
message: check.message
|
|
1642
|
+
});
|
|
1643
|
+
status.dirty();
|
|
1644
|
+
}
|
|
1645
|
+
} else if (check.kind === "multipleOf") {
|
|
1646
|
+
if (floatSafeRemainder(input.data, check.value) !== 0) {
|
|
1647
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1648
|
+
addIssueToContext(ctx, {
|
|
1649
|
+
code: ZodIssueCode.not_multiple_of,
|
|
1650
|
+
multipleOf: check.value,
|
|
1651
|
+
message: check.message
|
|
1652
|
+
});
|
|
1653
|
+
status.dirty();
|
|
1654
|
+
}
|
|
1655
|
+
} else if (check.kind === "finite") {
|
|
1656
|
+
if (!Number.isFinite(input.data)) {
|
|
1657
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1658
|
+
addIssueToContext(ctx, {
|
|
1659
|
+
code: ZodIssueCode.not_finite,
|
|
1660
|
+
message: check.message
|
|
1661
|
+
});
|
|
1662
|
+
status.dirty();
|
|
1663
|
+
}
|
|
1664
|
+
} else {
|
|
1665
|
+
util.assertNever(check);
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
return { status: status.value, value: input.data };
|
|
1669
|
+
}
|
|
1670
|
+
gte(value, message) {
|
|
1671
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
1672
|
+
}
|
|
1673
|
+
gt(value, message) {
|
|
1674
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
1675
|
+
}
|
|
1676
|
+
lte(value, message) {
|
|
1677
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
1678
|
+
}
|
|
1679
|
+
lt(value, message) {
|
|
1680
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
1681
|
+
}
|
|
1682
|
+
setLimit(kind, value, inclusive, message) {
|
|
1683
|
+
return new _ZodNumber({
|
|
1684
|
+
...this._def,
|
|
1685
|
+
checks: [
|
|
1686
|
+
...this._def.checks,
|
|
1687
|
+
{
|
|
1688
|
+
kind,
|
|
1689
|
+
value,
|
|
1690
|
+
inclusive,
|
|
1691
|
+
message: errorUtil.toString(message)
|
|
1692
|
+
}
|
|
1693
|
+
]
|
|
1694
|
+
});
|
|
1695
|
+
}
|
|
1696
|
+
_addCheck(check) {
|
|
1697
|
+
return new _ZodNumber({
|
|
1698
|
+
...this._def,
|
|
1699
|
+
checks: [...this._def.checks, check]
|
|
1700
|
+
});
|
|
1701
|
+
}
|
|
1702
|
+
int(message) {
|
|
1703
|
+
return this._addCheck({
|
|
1704
|
+
kind: "int",
|
|
1705
|
+
message: errorUtil.toString(message)
|
|
1706
|
+
});
|
|
1707
|
+
}
|
|
1708
|
+
positive(message) {
|
|
1709
|
+
return this._addCheck({
|
|
1710
|
+
kind: "min",
|
|
1711
|
+
value: 0,
|
|
1712
|
+
inclusive: false,
|
|
1713
|
+
message: errorUtil.toString(message)
|
|
1714
|
+
});
|
|
1715
|
+
}
|
|
1716
|
+
negative(message) {
|
|
1717
|
+
return this._addCheck({
|
|
1718
|
+
kind: "max",
|
|
1719
|
+
value: 0,
|
|
1720
|
+
inclusive: false,
|
|
1721
|
+
message: errorUtil.toString(message)
|
|
1722
|
+
});
|
|
1723
|
+
}
|
|
1724
|
+
nonpositive(message) {
|
|
1725
|
+
return this._addCheck({
|
|
1726
|
+
kind: "max",
|
|
1727
|
+
value: 0,
|
|
1728
|
+
inclusive: true,
|
|
1729
|
+
message: errorUtil.toString(message)
|
|
1730
|
+
});
|
|
1731
|
+
}
|
|
1732
|
+
nonnegative(message) {
|
|
1733
|
+
return this._addCheck({
|
|
1734
|
+
kind: "min",
|
|
1735
|
+
value: 0,
|
|
1736
|
+
inclusive: true,
|
|
1737
|
+
message: errorUtil.toString(message)
|
|
1738
|
+
});
|
|
1739
|
+
}
|
|
1740
|
+
multipleOf(value, message) {
|
|
1741
|
+
return this._addCheck({
|
|
1742
|
+
kind: "multipleOf",
|
|
1743
|
+
value,
|
|
1744
|
+
message: errorUtil.toString(message)
|
|
1745
|
+
});
|
|
1746
|
+
}
|
|
1747
|
+
finite(message) {
|
|
1748
|
+
return this._addCheck({
|
|
1749
|
+
kind: "finite",
|
|
1750
|
+
message: errorUtil.toString(message)
|
|
1751
|
+
});
|
|
1752
|
+
}
|
|
1753
|
+
safe(message) {
|
|
1754
|
+
return this._addCheck({
|
|
1755
|
+
kind: "min",
|
|
1756
|
+
inclusive: true,
|
|
1757
|
+
value: Number.MIN_SAFE_INTEGER,
|
|
1758
|
+
message: errorUtil.toString(message)
|
|
1759
|
+
})._addCheck({
|
|
1760
|
+
kind: "max",
|
|
1761
|
+
inclusive: true,
|
|
1762
|
+
value: Number.MAX_SAFE_INTEGER,
|
|
1763
|
+
message: errorUtil.toString(message)
|
|
1764
|
+
});
|
|
1765
|
+
}
|
|
1766
|
+
get minValue() {
|
|
1767
|
+
let min = null;
|
|
1768
|
+
for (const ch of this._def.checks) {
|
|
1769
|
+
if (ch.kind === "min") {
|
|
1770
|
+
if (min === null || ch.value > min)
|
|
1771
|
+
min = ch.value;
|
|
1772
|
+
}
|
|
1773
|
+
}
|
|
1774
|
+
return min;
|
|
1775
|
+
}
|
|
1776
|
+
get maxValue() {
|
|
1777
|
+
let max = null;
|
|
1778
|
+
for (const ch of this._def.checks) {
|
|
1779
|
+
if (ch.kind === "max") {
|
|
1780
|
+
if (max === null || ch.value < max)
|
|
1781
|
+
max = ch.value;
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
return max;
|
|
1785
|
+
}
|
|
1786
|
+
get isInt() {
|
|
1787
|
+
return !!this._def.checks.find((ch) => ch.kind === "int" || ch.kind === "multipleOf" && util.isInteger(ch.value));
|
|
1788
|
+
}
|
|
1789
|
+
get isFinite() {
|
|
1790
|
+
let max = null;
|
|
1791
|
+
let min = null;
|
|
1792
|
+
for (const ch of this._def.checks) {
|
|
1793
|
+
if (ch.kind === "finite" || ch.kind === "int" || ch.kind === "multipleOf") {
|
|
1794
|
+
return true;
|
|
1795
|
+
} else if (ch.kind === "min") {
|
|
1796
|
+
if (min === null || ch.value > min)
|
|
1797
|
+
min = ch.value;
|
|
1798
|
+
} else if (ch.kind === "max") {
|
|
1799
|
+
if (max === null || ch.value < max)
|
|
1800
|
+
max = ch.value;
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
return Number.isFinite(min) && Number.isFinite(max);
|
|
1804
|
+
}
|
|
1805
|
+
};
|
|
1806
|
+
ZodNumber.create = (params) => {
|
|
1807
|
+
return new ZodNumber({
|
|
1808
|
+
checks: [],
|
|
1809
|
+
typeName: ZodFirstPartyTypeKind.ZodNumber,
|
|
1810
|
+
coerce: params?.coerce || false,
|
|
1811
|
+
...processCreateParams(params)
|
|
1812
|
+
});
|
|
1813
|
+
};
|
|
1814
|
+
var ZodBigInt = class _ZodBigInt extends ZodType {
|
|
1815
|
+
constructor() {
|
|
1816
|
+
super(...arguments);
|
|
1817
|
+
this.min = this.gte;
|
|
1818
|
+
this.max = this.lte;
|
|
1819
|
+
}
|
|
1820
|
+
_parse(input) {
|
|
1821
|
+
if (this._def.coerce) {
|
|
1822
|
+
try {
|
|
1823
|
+
input.data = BigInt(input.data);
|
|
1824
|
+
} catch {
|
|
1825
|
+
return this._getInvalidInput(input);
|
|
1826
|
+
}
|
|
1827
|
+
}
|
|
1828
|
+
const parsedType = this._getType(input);
|
|
1829
|
+
if (parsedType !== ZodParsedType.bigint) {
|
|
1830
|
+
return this._getInvalidInput(input);
|
|
1831
|
+
}
|
|
1832
|
+
let ctx = void 0;
|
|
1833
|
+
const status = new ParseStatus();
|
|
1834
|
+
for (const check of this._def.checks) {
|
|
1835
|
+
if (check.kind === "min") {
|
|
1836
|
+
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
1837
|
+
if (tooSmall) {
|
|
1838
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1839
|
+
addIssueToContext(ctx, {
|
|
1840
|
+
code: ZodIssueCode.too_small,
|
|
1841
|
+
type: "bigint",
|
|
1842
|
+
minimum: check.value,
|
|
1843
|
+
inclusive: check.inclusive,
|
|
1844
|
+
message: check.message
|
|
1845
|
+
});
|
|
1846
|
+
status.dirty();
|
|
1847
|
+
}
|
|
1848
|
+
} else if (check.kind === "max") {
|
|
1849
|
+
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
1850
|
+
if (tooBig) {
|
|
1851
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1852
|
+
addIssueToContext(ctx, {
|
|
1853
|
+
code: ZodIssueCode.too_big,
|
|
1854
|
+
type: "bigint",
|
|
1855
|
+
maximum: check.value,
|
|
1856
|
+
inclusive: check.inclusive,
|
|
1857
|
+
message: check.message
|
|
1858
|
+
});
|
|
1859
|
+
status.dirty();
|
|
1860
|
+
}
|
|
1861
|
+
} else if (check.kind === "multipleOf") {
|
|
1862
|
+
if (input.data % check.value !== BigInt(0)) {
|
|
1863
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1864
|
+
addIssueToContext(ctx, {
|
|
1865
|
+
code: ZodIssueCode.not_multiple_of,
|
|
1866
|
+
multipleOf: check.value,
|
|
1867
|
+
message: check.message
|
|
1868
|
+
});
|
|
1869
|
+
status.dirty();
|
|
1870
|
+
}
|
|
1871
|
+
} else {
|
|
1872
|
+
util.assertNever(check);
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
return { status: status.value, value: input.data };
|
|
1876
|
+
}
|
|
1877
|
+
_getInvalidInput(input) {
|
|
1878
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1879
|
+
addIssueToContext(ctx, {
|
|
1880
|
+
code: ZodIssueCode.invalid_type,
|
|
1881
|
+
expected: ZodParsedType.bigint,
|
|
1882
|
+
received: ctx.parsedType
|
|
1883
|
+
});
|
|
1884
|
+
return INVALID;
|
|
1885
|
+
}
|
|
1886
|
+
gte(value, message) {
|
|
1887
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
1888
|
+
}
|
|
1889
|
+
gt(value, message) {
|
|
1890
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
1891
|
+
}
|
|
1892
|
+
lte(value, message) {
|
|
1893
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
1894
|
+
}
|
|
1895
|
+
lt(value, message) {
|
|
1896
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
1897
|
+
}
|
|
1898
|
+
setLimit(kind, value, inclusive, message) {
|
|
1899
|
+
return new _ZodBigInt({
|
|
1900
|
+
...this._def,
|
|
1901
|
+
checks: [
|
|
1902
|
+
...this._def.checks,
|
|
1903
|
+
{
|
|
1904
|
+
kind,
|
|
1905
|
+
value,
|
|
1906
|
+
inclusive,
|
|
1907
|
+
message: errorUtil.toString(message)
|
|
1908
|
+
}
|
|
1909
|
+
]
|
|
1910
|
+
});
|
|
1911
|
+
}
|
|
1912
|
+
_addCheck(check) {
|
|
1913
|
+
return new _ZodBigInt({
|
|
1914
|
+
...this._def,
|
|
1915
|
+
checks: [...this._def.checks, check]
|
|
1916
|
+
});
|
|
1917
|
+
}
|
|
1918
|
+
positive(message) {
|
|
1919
|
+
return this._addCheck({
|
|
1920
|
+
kind: "min",
|
|
1921
|
+
value: BigInt(0),
|
|
1922
|
+
inclusive: false,
|
|
1923
|
+
message: errorUtil.toString(message)
|
|
1924
|
+
});
|
|
1925
|
+
}
|
|
1926
|
+
negative(message) {
|
|
1927
|
+
return this._addCheck({
|
|
1928
|
+
kind: "max",
|
|
1929
|
+
value: BigInt(0),
|
|
1930
|
+
inclusive: false,
|
|
1931
|
+
message: errorUtil.toString(message)
|
|
1932
|
+
});
|
|
1933
|
+
}
|
|
1934
|
+
nonpositive(message) {
|
|
1935
|
+
return this._addCheck({
|
|
1936
|
+
kind: "max",
|
|
1937
|
+
value: BigInt(0),
|
|
1938
|
+
inclusive: true,
|
|
1939
|
+
message: errorUtil.toString(message)
|
|
1940
|
+
});
|
|
1941
|
+
}
|
|
1942
|
+
nonnegative(message) {
|
|
1943
|
+
return this._addCheck({
|
|
1944
|
+
kind: "min",
|
|
1945
|
+
value: BigInt(0),
|
|
1946
|
+
inclusive: true,
|
|
1947
|
+
message: errorUtil.toString(message)
|
|
1948
|
+
});
|
|
1949
|
+
}
|
|
1950
|
+
multipleOf(value, message) {
|
|
1951
|
+
return this._addCheck({
|
|
1952
|
+
kind: "multipleOf",
|
|
1953
|
+
value,
|
|
1954
|
+
message: errorUtil.toString(message)
|
|
1955
|
+
});
|
|
1956
|
+
}
|
|
1957
|
+
get minValue() {
|
|
1958
|
+
let min = null;
|
|
1959
|
+
for (const ch of this._def.checks) {
|
|
1960
|
+
if (ch.kind === "min") {
|
|
1961
|
+
if (min === null || ch.value > min)
|
|
1962
|
+
min = ch.value;
|
|
1963
|
+
}
|
|
1964
|
+
}
|
|
1965
|
+
return min;
|
|
1966
|
+
}
|
|
1967
|
+
get maxValue() {
|
|
1968
|
+
let max = null;
|
|
1969
|
+
for (const ch of this._def.checks) {
|
|
1970
|
+
if (ch.kind === "max") {
|
|
1971
|
+
if (max === null || ch.value < max)
|
|
1972
|
+
max = ch.value;
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
return max;
|
|
1976
|
+
}
|
|
1977
|
+
};
|
|
1978
|
+
ZodBigInt.create = (params) => {
|
|
1979
|
+
return new ZodBigInt({
|
|
1980
|
+
checks: [],
|
|
1981
|
+
typeName: ZodFirstPartyTypeKind.ZodBigInt,
|
|
1982
|
+
coerce: params?.coerce ?? false,
|
|
1983
|
+
...processCreateParams(params)
|
|
1984
|
+
});
|
|
1985
|
+
};
|
|
1986
|
+
var ZodBoolean = class extends ZodType {
|
|
1987
|
+
_parse(input) {
|
|
1988
|
+
if (this._def.coerce) {
|
|
1989
|
+
input.data = Boolean(input.data);
|
|
1990
|
+
}
|
|
1991
|
+
const parsedType = this._getType(input);
|
|
1992
|
+
if (parsedType !== ZodParsedType.boolean) {
|
|
1993
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1994
|
+
addIssueToContext(ctx, {
|
|
1995
|
+
code: ZodIssueCode.invalid_type,
|
|
1996
|
+
expected: ZodParsedType.boolean,
|
|
1997
|
+
received: ctx.parsedType
|
|
1998
|
+
});
|
|
1999
|
+
return INVALID;
|
|
2000
|
+
}
|
|
2001
|
+
return OK(input.data);
|
|
2002
|
+
}
|
|
2003
|
+
};
|
|
2004
|
+
ZodBoolean.create = (params) => {
|
|
2005
|
+
return new ZodBoolean({
|
|
2006
|
+
typeName: ZodFirstPartyTypeKind.ZodBoolean,
|
|
2007
|
+
coerce: params?.coerce || false,
|
|
2008
|
+
...processCreateParams(params)
|
|
2009
|
+
});
|
|
2010
|
+
};
|
|
2011
|
+
var ZodDate = class _ZodDate extends ZodType {
|
|
2012
|
+
_parse(input) {
|
|
2013
|
+
if (this._def.coerce) {
|
|
2014
|
+
input.data = new Date(input.data);
|
|
2015
|
+
}
|
|
2016
|
+
const parsedType = this._getType(input);
|
|
2017
|
+
if (parsedType !== ZodParsedType.date) {
|
|
2018
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
2019
|
+
addIssueToContext(ctx2, {
|
|
2020
|
+
code: ZodIssueCode.invalid_type,
|
|
2021
|
+
expected: ZodParsedType.date,
|
|
2022
|
+
received: ctx2.parsedType
|
|
2023
|
+
});
|
|
2024
|
+
return INVALID;
|
|
2025
|
+
}
|
|
2026
|
+
if (Number.isNaN(input.data.getTime())) {
|
|
2027
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
2028
|
+
addIssueToContext(ctx2, {
|
|
2029
|
+
code: ZodIssueCode.invalid_date
|
|
2030
|
+
});
|
|
2031
|
+
return INVALID;
|
|
2032
|
+
}
|
|
2033
|
+
const status = new ParseStatus();
|
|
2034
|
+
let ctx = void 0;
|
|
2035
|
+
for (const check of this._def.checks) {
|
|
2036
|
+
if (check.kind === "min") {
|
|
2037
|
+
if (input.data.getTime() < check.value) {
|
|
2038
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2039
|
+
addIssueToContext(ctx, {
|
|
2040
|
+
code: ZodIssueCode.too_small,
|
|
2041
|
+
message: check.message,
|
|
2042
|
+
inclusive: true,
|
|
2043
|
+
exact: false,
|
|
2044
|
+
minimum: check.value,
|
|
2045
|
+
type: "date"
|
|
2046
|
+
});
|
|
2047
|
+
status.dirty();
|
|
2048
|
+
}
|
|
2049
|
+
} else if (check.kind === "max") {
|
|
2050
|
+
if (input.data.getTime() > check.value) {
|
|
2051
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2052
|
+
addIssueToContext(ctx, {
|
|
2053
|
+
code: ZodIssueCode.too_big,
|
|
2054
|
+
message: check.message,
|
|
2055
|
+
inclusive: true,
|
|
2056
|
+
exact: false,
|
|
2057
|
+
maximum: check.value,
|
|
2058
|
+
type: "date"
|
|
2059
|
+
});
|
|
2060
|
+
status.dirty();
|
|
2061
|
+
}
|
|
2062
|
+
} else {
|
|
2063
|
+
util.assertNever(check);
|
|
2064
|
+
}
|
|
2065
|
+
}
|
|
2066
|
+
return {
|
|
2067
|
+
status: status.value,
|
|
2068
|
+
value: new Date(input.data.getTime())
|
|
2069
|
+
};
|
|
2070
|
+
}
|
|
2071
|
+
_addCheck(check) {
|
|
2072
|
+
return new _ZodDate({
|
|
2073
|
+
...this._def,
|
|
2074
|
+
checks: [...this._def.checks, check]
|
|
2075
|
+
});
|
|
2076
|
+
}
|
|
2077
|
+
min(minDate, message) {
|
|
2078
|
+
return this._addCheck({
|
|
2079
|
+
kind: "min",
|
|
2080
|
+
value: minDate.getTime(),
|
|
2081
|
+
message: errorUtil.toString(message)
|
|
2082
|
+
});
|
|
2083
|
+
}
|
|
2084
|
+
max(maxDate, message) {
|
|
2085
|
+
return this._addCheck({
|
|
2086
|
+
kind: "max",
|
|
2087
|
+
value: maxDate.getTime(),
|
|
2088
|
+
message: errorUtil.toString(message)
|
|
2089
|
+
});
|
|
2090
|
+
}
|
|
2091
|
+
get minDate() {
|
|
2092
|
+
let min = null;
|
|
2093
|
+
for (const ch of this._def.checks) {
|
|
2094
|
+
if (ch.kind === "min") {
|
|
2095
|
+
if (min === null || ch.value > min)
|
|
2096
|
+
min = ch.value;
|
|
2097
|
+
}
|
|
2098
|
+
}
|
|
2099
|
+
return min != null ? new Date(min) : null;
|
|
2100
|
+
}
|
|
2101
|
+
get maxDate() {
|
|
2102
|
+
let max = null;
|
|
2103
|
+
for (const ch of this._def.checks) {
|
|
2104
|
+
if (ch.kind === "max") {
|
|
2105
|
+
if (max === null || ch.value < max)
|
|
2106
|
+
max = ch.value;
|
|
2107
|
+
}
|
|
2108
|
+
}
|
|
2109
|
+
return max != null ? new Date(max) : null;
|
|
2110
|
+
}
|
|
2111
|
+
};
|
|
2112
|
+
ZodDate.create = (params) => {
|
|
2113
|
+
return new ZodDate({
|
|
2114
|
+
checks: [],
|
|
2115
|
+
coerce: params?.coerce || false,
|
|
2116
|
+
typeName: ZodFirstPartyTypeKind.ZodDate,
|
|
2117
|
+
...processCreateParams(params)
|
|
2118
|
+
});
|
|
2119
|
+
};
|
|
2120
|
+
var ZodSymbol = class extends ZodType {
|
|
2121
|
+
_parse(input) {
|
|
2122
|
+
const parsedType = this._getType(input);
|
|
2123
|
+
if (parsedType !== ZodParsedType.symbol) {
|
|
2124
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2125
|
+
addIssueToContext(ctx, {
|
|
2126
|
+
code: ZodIssueCode.invalid_type,
|
|
2127
|
+
expected: ZodParsedType.symbol,
|
|
2128
|
+
received: ctx.parsedType
|
|
2129
|
+
});
|
|
2130
|
+
return INVALID;
|
|
2131
|
+
}
|
|
2132
|
+
return OK(input.data);
|
|
2133
|
+
}
|
|
2134
|
+
};
|
|
2135
|
+
ZodSymbol.create = (params) => {
|
|
2136
|
+
return new ZodSymbol({
|
|
2137
|
+
typeName: ZodFirstPartyTypeKind.ZodSymbol,
|
|
2138
|
+
...processCreateParams(params)
|
|
2139
|
+
});
|
|
2140
|
+
};
|
|
2141
|
+
var ZodUndefined = class extends ZodType {
|
|
2142
|
+
_parse(input) {
|
|
2143
|
+
const parsedType = this._getType(input);
|
|
2144
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
2145
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2146
|
+
addIssueToContext(ctx, {
|
|
2147
|
+
code: ZodIssueCode.invalid_type,
|
|
2148
|
+
expected: ZodParsedType.undefined,
|
|
2149
|
+
received: ctx.parsedType
|
|
2150
|
+
});
|
|
2151
|
+
return INVALID;
|
|
2152
|
+
}
|
|
2153
|
+
return OK(input.data);
|
|
2154
|
+
}
|
|
2155
|
+
};
|
|
2156
|
+
ZodUndefined.create = (params) => {
|
|
2157
|
+
return new ZodUndefined({
|
|
2158
|
+
typeName: ZodFirstPartyTypeKind.ZodUndefined,
|
|
2159
|
+
...processCreateParams(params)
|
|
2160
|
+
});
|
|
2161
|
+
};
|
|
2162
|
+
var ZodNull = class extends ZodType {
|
|
2163
|
+
_parse(input) {
|
|
2164
|
+
const parsedType = this._getType(input);
|
|
2165
|
+
if (parsedType !== ZodParsedType.null) {
|
|
2166
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2167
|
+
addIssueToContext(ctx, {
|
|
2168
|
+
code: ZodIssueCode.invalid_type,
|
|
2169
|
+
expected: ZodParsedType.null,
|
|
2170
|
+
received: ctx.parsedType
|
|
2171
|
+
});
|
|
2172
|
+
return INVALID;
|
|
2173
|
+
}
|
|
2174
|
+
return OK(input.data);
|
|
2175
|
+
}
|
|
2176
|
+
};
|
|
2177
|
+
ZodNull.create = (params) => {
|
|
2178
|
+
return new ZodNull({
|
|
2179
|
+
typeName: ZodFirstPartyTypeKind.ZodNull,
|
|
2180
|
+
...processCreateParams(params)
|
|
2181
|
+
});
|
|
2182
|
+
};
|
|
2183
|
+
var ZodAny = class extends ZodType {
|
|
2184
|
+
constructor() {
|
|
2185
|
+
super(...arguments);
|
|
2186
|
+
this._any = true;
|
|
2187
|
+
}
|
|
2188
|
+
_parse(input) {
|
|
2189
|
+
return OK(input.data);
|
|
2190
|
+
}
|
|
2191
|
+
};
|
|
2192
|
+
ZodAny.create = (params) => {
|
|
2193
|
+
return new ZodAny({
|
|
2194
|
+
typeName: ZodFirstPartyTypeKind.ZodAny,
|
|
2195
|
+
...processCreateParams(params)
|
|
2196
|
+
});
|
|
2197
|
+
};
|
|
2198
|
+
var ZodUnknown = class extends ZodType {
|
|
2199
|
+
constructor() {
|
|
2200
|
+
super(...arguments);
|
|
2201
|
+
this._unknown = true;
|
|
2202
|
+
}
|
|
2203
|
+
_parse(input) {
|
|
2204
|
+
return OK(input.data);
|
|
2205
|
+
}
|
|
2206
|
+
};
|
|
2207
|
+
ZodUnknown.create = (params) => {
|
|
2208
|
+
return new ZodUnknown({
|
|
2209
|
+
typeName: ZodFirstPartyTypeKind.ZodUnknown,
|
|
2210
|
+
...processCreateParams(params)
|
|
2211
|
+
});
|
|
2212
|
+
};
|
|
2213
|
+
var ZodNever = class extends ZodType {
|
|
2214
|
+
_parse(input) {
|
|
2215
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2216
|
+
addIssueToContext(ctx, {
|
|
2217
|
+
code: ZodIssueCode.invalid_type,
|
|
2218
|
+
expected: ZodParsedType.never,
|
|
2219
|
+
received: ctx.parsedType
|
|
2220
|
+
});
|
|
2221
|
+
return INVALID;
|
|
2222
|
+
}
|
|
2223
|
+
};
|
|
2224
|
+
ZodNever.create = (params) => {
|
|
2225
|
+
return new ZodNever({
|
|
2226
|
+
typeName: ZodFirstPartyTypeKind.ZodNever,
|
|
2227
|
+
...processCreateParams(params)
|
|
2228
|
+
});
|
|
2229
|
+
};
|
|
2230
|
+
var ZodVoid = class extends ZodType {
|
|
2231
|
+
_parse(input) {
|
|
2232
|
+
const parsedType = this._getType(input);
|
|
2233
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
2234
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2235
|
+
addIssueToContext(ctx, {
|
|
2236
|
+
code: ZodIssueCode.invalid_type,
|
|
2237
|
+
expected: ZodParsedType.void,
|
|
2238
|
+
received: ctx.parsedType
|
|
2239
|
+
});
|
|
2240
|
+
return INVALID;
|
|
2241
|
+
}
|
|
2242
|
+
return OK(input.data);
|
|
2243
|
+
}
|
|
2244
|
+
};
|
|
2245
|
+
ZodVoid.create = (params) => {
|
|
2246
|
+
return new ZodVoid({
|
|
2247
|
+
typeName: ZodFirstPartyTypeKind.ZodVoid,
|
|
2248
|
+
...processCreateParams(params)
|
|
2249
|
+
});
|
|
2250
|
+
};
|
|
2251
|
+
var ZodArray = class _ZodArray extends ZodType {
|
|
2252
|
+
_parse(input) {
|
|
2253
|
+
const { ctx, status } = this._processInputParams(input);
|
|
2254
|
+
const def = this._def;
|
|
2255
|
+
if (ctx.parsedType !== ZodParsedType.array) {
|
|
2256
|
+
addIssueToContext(ctx, {
|
|
2257
|
+
code: ZodIssueCode.invalid_type,
|
|
2258
|
+
expected: ZodParsedType.array,
|
|
2259
|
+
received: ctx.parsedType
|
|
2260
|
+
});
|
|
2261
|
+
return INVALID;
|
|
2262
|
+
}
|
|
2263
|
+
if (def.exactLength !== null) {
|
|
2264
|
+
const tooBig = ctx.data.length > def.exactLength.value;
|
|
2265
|
+
const tooSmall = ctx.data.length < def.exactLength.value;
|
|
2266
|
+
if (tooBig || tooSmall) {
|
|
2267
|
+
addIssueToContext(ctx, {
|
|
2268
|
+
code: tooBig ? ZodIssueCode.too_big : ZodIssueCode.too_small,
|
|
2269
|
+
minimum: tooSmall ? def.exactLength.value : void 0,
|
|
2270
|
+
maximum: tooBig ? def.exactLength.value : void 0,
|
|
2271
|
+
type: "array",
|
|
2272
|
+
inclusive: true,
|
|
2273
|
+
exact: true,
|
|
2274
|
+
message: def.exactLength.message
|
|
2275
|
+
});
|
|
2276
|
+
status.dirty();
|
|
2277
|
+
}
|
|
2278
|
+
}
|
|
2279
|
+
if (def.minLength !== null) {
|
|
2280
|
+
if (ctx.data.length < def.minLength.value) {
|
|
2281
|
+
addIssueToContext(ctx, {
|
|
2282
|
+
code: ZodIssueCode.too_small,
|
|
2283
|
+
minimum: def.minLength.value,
|
|
2284
|
+
type: "array",
|
|
2285
|
+
inclusive: true,
|
|
2286
|
+
exact: false,
|
|
2287
|
+
message: def.minLength.message
|
|
2288
|
+
});
|
|
2289
|
+
status.dirty();
|
|
2290
|
+
}
|
|
2291
|
+
}
|
|
2292
|
+
if (def.maxLength !== null) {
|
|
2293
|
+
if (ctx.data.length > def.maxLength.value) {
|
|
2294
|
+
addIssueToContext(ctx, {
|
|
2295
|
+
code: ZodIssueCode.too_big,
|
|
2296
|
+
maximum: def.maxLength.value,
|
|
2297
|
+
type: "array",
|
|
2298
|
+
inclusive: true,
|
|
2299
|
+
exact: false,
|
|
2300
|
+
message: def.maxLength.message
|
|
2301
|
+
});
|
|
2302
|
+
status.dirty();
|
|
2303
|
+
}
|
|
2304
|
+
}
|
|
2305
|
+
if (ctx.common.async) {
|
|
2306
|
+
return Promise.all([...ctx.data].map((item, i) => {
|
|
2307
|
+
return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
2308
|
+
})).then((result2) => {
|
|
2309
|
+
return ParseStatus.mergeArray(status, result2);
|
|
2310
|
+
});
|
|
2311
|
+
}
|
|
2312
|
+
const result = [...ctx.data].map((item, i) => {
|
|
2313
|
+
return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
2314
|
+
});
|
|
2315
|
+
return ParseStatus.mergeArray(status, result);
|
|
2316
|
+
}
|
|
2317
|
+
get element() {
|
|
2318
|
+
return this._def.type;
|
|
2319
|
+
}
|
|
2320
|
+
min(minLength, message) {
|
|
2321
|
+
return new _ZodArray({
|
|
2322
|
+
...this._def,
|
|
2323
|
+
minLength: { value: minLength, message: errorUtil.toString(message) }
|
|
2324
|
+
});
|
|
2325
|
+
}
|
|
2326
|
+
max(maxLength, message) {
|
|
2327
|
+
return new _ZodArray({
|
|
2328
|
+
...this._def,
|
|
2329
|
+
maxLength: { value: maxLength, message: errorUtil.toString(message) }
|
|
2330
|
+
});
|
|
2331
|
+
}
|
|
2332
|
+
length(len, message) {
|
|
2333
|
+
return new _ZodArray({
|
|
2334
|
+
...this._def,
|
|
2335
|
+
exactLength: { value: len, message: errorUtil.toString(message) }
|
|
2336
|
+
});
|
|
2337
|
+
}
|
|
2338
|
+
nonempty(message) {
|
|
2339
|
+
return this.min(1, message);
|
|
2340
|
+
}
|
|
2341
|
+
};
|
|
2342
|
+
ZodArray.create = (schema, params) => {
|
|
2343
|
+
return new ZodArray({
|
|
2344
|
+
type: schema,
|
|
2345
|
+
minLength: null,
|
|
2346
|
+
maxLength: null,
|
|
2347
|
+
exactLength: null,
|
|
2348
|
+
typeName: ZodFirstPartyTypeKind.ZodArray,
|
|
2349
|
+
...processCreateParams(params)
|
|
2350
|
+
});
|
|
2351
|
+
};
|
|
2352
|
+
function deepPartialify(schema) {
|
|
2353
|
+
if (schema instanceof ZodObject) {
|
|
2354
|
+
const newShape = {};
|
|
2355
|
+
for (const key in schema.shape) {
|
|
2356
|
+
const fieldSchema = schema.shape[key];
|
|
2357
|
+
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema));
|
|
2358
|
+
}
|
|
2359
|
+
return new ZodObject({
|
|
2360
|
+
...schema._def,
|
|
2361
|
+
shape: () => newShape
|
|
2362
|
+
});
|
|
2363
|
+
} else if (schema instanceof ZodArray) {
|
|
2364
|
+
return new ZodArray({
|
|
2365
|
+
...schema._def,
|
|
2366
|
+
type: deepPartialify(schema.element)
|
|
2367
|
+
});
|
|
2368
|
+
} else if (schema instanceof ZodOptional) {
|
|
2369
|
+
return ZodOptional.create(deepPartialify(schema.unwrap()));
|
|
2370
|
+
} else if (schema instanceof ZodNullable) {
|
|
2371
|
+
return ZodNullable.create(deepPartialify(schema.unwrap()));
|
|
2372
|
+
} else if (schema instanceof ZodTuple) {
|
|
2373
|
+
return ZodTuple.create(schema.items.map((item) => deepPartialify(item)));
|
|
2374
|
+
} else {
|
|
2375
|
+
return schema;
|
|
2376
|
+
}
|
|
2377
|
+
}
|
|
2378
|
+
var ZodObject = class _ZodObject extends ZodType {
|
|
2379
|
+
constructor() {
|
|
2380
|
+
super(...arguments);
|
|
2381
|
+
this._cached = null;
|
|
2382
|
+
this.nonstrict = this.passthrough;
|
|
2383
|
+
this.augment = this.extend;
|
|
2384
|
+
}
|
|
2385
|
+
_getCached() {
|
|
2386
|
+
if (this._cached !== null)
|
|
2387
|
+
return this._cached;
|
|
2388
|
+
const shape = this._def.shape();
|
|
2389
|
+
const keys = util.objectKeys(shape);
|
|
2390
|
+
this._cached = { shape, keys };
|
|
2391
|
+
return this._cached;
|
|
2392
|
+
}
|
|
2393
|
+
_parse(input) {
|
|
2394
|
+
const parsedType = this._getType(input);
|
|
2395
|
+
if (parsedType !== ZodParsedType.object) {
|
|
2396
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
2397
|
+
addIssueToContext(ctx2, {
|
|
2398
|
+
code: ZodIssueCode.invalid_type,
|
|
2399
|
+
expected: ZodParsedType.object,
|
|
2400
|
+
received: ctx2.parsedType
|
|
2401
|
+
});
|
|
2402
|
+
return INVALID;
|
|
2403
|
+
}
|
|
2404
|
+
const { status, ctx } = this._processInputParams(input);
|
|
2405
|
+
const { shape, keys: shapeKeys } = this._getCached();
|
|
2406
|
+
const extraKeys = [];
|
|
2407
|
+
if (!(this._def.catchall instanceof ZodNever && this._def.unknownKeys === "strip")) {
|
|
2408
|
+
for (const key in ctx.data) {
|
|
2409
|
+
if (!shapeKeys.includes(key)) {
|
|
2410
|
+
extraKeys.push(key);
|
|
2411
|
+
}
|
|
2412
|
+
}
|
|
2413
|
+
}
|
|
2414
|
+
const pairs = [];
|
|
2415
|
+
for (const key of shapeKeys) {
|
|
2416
|
+
const keyValidator = shape[key];
|
|
2417
|
+
const value = ctx.data[key];
|
|
2418
|
+
pairs.push({
|
|
2419
|
+
key: { status: "valid", value: key },
|
|
2420
|
+
value: keyValidator._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
|
|
2421
|
+
alwaysSet: key in ctx.data
|
|
2422
|
+
});
|
|
2423
|
+
}
|
|
2424
|
+
if (this._def.catchall instanceof ZodNever) {
|
|
2425
|
+
const unknownKeys = this._def.unknownKeys;
|
|
2426
|
+
if (unknownKeys === "passthrough") {
|
|
2427
|
+
for (const key of extraKeys) {
|
|
2428
|
+
pairs.push({
|
|
2429
|
+
key: { status: "valid", value: key },
|
|
2430
|
+
value: { status: "valid", value: ctx.data[key] }
|
|
2431
|
+
});
|
|
2432
|
+
}
|
|
2433
|
+
} else if (unknownKeys === "strict") {
|
|
2434
|
+
if (extraKeys.length > 0) {
|
|
2435
|
+
addIssueToContext(ctx, {
|
|
2436
|
+
code: ZodIssueCode.unrecognized_keys,
|
|
2437
|
+
keys: extraKeys
|
|
2438
|
+
});
|
|
2439
|
+
status.dirty();
|
|
2440
|
+
}
|
|
2441
|
+
} else if (unknownKeys === "strip") {
|
|
2442
|
+
} else {
|
|
2443
|
+
throw new Error(`Internal ZodObject error: invalid unknownKeys value.`);
|
|
2444
|
+
}
|
|
2445
|
+
} else {
|
|
2446
|
+
const catchall = this._def.catchall;
|
|
2447
|
+
for (const key of extraKeys) {
|
|
2448
|
+
const value = ctx.data[key];
|
|
2449
|
+
pairs.push({
|
|
2450
|
+
key: { status: "valid", value: key },
|
|
2451
|
+
value: catchall._parse(
|
|
2452
|
+
new ParseInputLazyPath(ctx, value, ctx.path, key)
|
|
2453
|
+
//, ctx.child(key), value, getParsedType(value)
|
|
2454
|
+
),
|
|
2455
|
+
alwaysSet: key in ctx.data
|
|
2456
|
+
});
|
|
2457
|
+
}
|
|
2458
|
+
}
|
|
2459
|
+
if (ctx.common.async) {
|
|
2460
|
+
return Promise.resolve().then(async () => {
|
|
2461
|
+
const syncPairs = [];
|
|
2462
|
+
for (const pair of pairs) {
|
|
2463
|
+
const key = await pair.key;
|
|
2464
|
+
const value = await pair.value;
|
|
2465
|
+
syncPairs.push({
|
|
2466
|
+
key,
|
|
2467
|
+
value,
|
|
2468
|
+
alwaysSet: pair.alwaysSet
|
|
2469
|
+
});
|
|
2470
|
+
}
|
|
2471
|
+
return syncPairs;
|
|
2472
|
+
}).then((syncPairs) => {
|
|
2473
|
+
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
2474
|
+
});
|
|
2475
|
+
} else {
|
|
2476
|
+
return ParseStatus.mergeObjectSync(status, pairs);
|
|
2477
|
+
}
|
|
2478
|
+
}
|
|
2479
|
+
get shape() {
|
|
2480
|
+
return this._def.shape();
|
|
2481
|
+
}
|
|
2482
|
+
strict(message) {
|
|
2483
|
+
errorUtil.errToObj;
|
|
2484
|
+
return new _ZodObject({
|
|
2485
|
+
...this._def,
|
|
2486
|
+
unknownKeys: "strict",
|
|
2487
|
+
...message !== void 0 ? {
|
|
2488
|
+
errorMap: (issue, ctx) => {
|
|
2489
|
+
const defaultError = this._def.errorMap?.(issue, ctx).message ?? ctx.defaultError;
|
|
2490
|
+
if (issue.code === "unrecognized_keys")
|
|
2491
|
+
return {
|
|
2492
|
+
message: errorUtil.errToObj(message).message ?? defaultError
|
|
2493
|
+
};
|
|
2494
|
+
return {
|
|
2495
|
+
message: defaultError
|
|
2496
|
+
};
|
|
2497
|
+
}
|
|
2498
|
+
} : {}
|
|
2499
|
+
});
|
|
2500
|
+
}
|
|
2501
|
+
strip() {
|
|
2502
|
+
return new _ZodObject({
|
|
2503
|
+
...this._def,
|
|
2504
|
+
unknownKeys: "strip"
|
|
2505
|
+
});
|
|
2506
|
+
}
|
|
2507
|
+
passthrough() {
|
|
2508
|
+
return new _ZodObject({
|
|
2509
|
+
...this._def,
|
|
2510
|
+
unknownKeys: "passthrough"
|
|
2511
|
+
});
|
|
2512
|
+
}
|
|
2513
|
+
// const AugmentFactory =
|
|
2514
|
+
// <Def extends ZodObjectDef>(def: Def) =>
|
|
2515
|
+
// <Augmentation extends ZodRawShape>(
|
|
2516
|
+
// augmentation: Augmentation
|
|
2517
|
+
// ): ZodObject<
|
|
2518
|
+
// extendShape<ReturnType<Def["shape"]>, Augmentation>,
|
|
2519
|
+
// Def["unknownKeys"],
|
|
2520
|
+
// Def["catchall"]
|
|
2521
|
+
// > => {
|
|
2522
|
+
// return new ZodObject({
|
|
2523
|
+
// ...def,
|
|
2524
|
+
// shape: () => ({
|
|
2525
|
+
// ...def.shape(),
|
|
2526
|
+
// ...augmentation,
|
|
2527
|
+
// }),
|
|
2528
|
+
// }) as any;
|
|
2529
|
+
// };
|
|
2530
|
+
extend(augmentation) {
|
|
2531
|
+
return new _ZodObject({
|
|
2532
|
+
...this._def,
|
|
2533
|
+
shape: () => ({
|
|
2534
|
+
...this._def.shape(),
|
|
2535
|
+
...augmentation
|
|
2536
|
+
})
|
|
2537
|
+
});
|
|
2538
|
+
}
|
|
2539
|
+
/**
|
|
2540
|
+
* Prior to zod@1.0.12 there was a bug in the
|
|
2541
|
+
* inferred type of merged objects. Please
|
|
2542
|
+
* upgrade if you are experiencing issues.
|
|
2543
|
+
*/
|
|
2544
|
+
merge(merging) {
|
|
2545
|
+
const merged = new _ZodObject({
|
|
2546
|
+
unknownKeys: merging._def.unknownKeys,
|
|
2547
|
+
catchall: merging._def.catchall,
|
|
2548
|
+
shape: () => ({
|
|
2549
|
+
...this._def.shape(),
|
|
2550
|
+
...merging._def.shape()
|
|
2551
|
+
}),
|
|
2552
|
+
typeName: ZodFirstPartyTypeKind.ZodObject
|
|
2553
|
+
});
|
|
2554
|
+
return merged;
|
|
2555
|
+
}
|
|
2556
|
+
// merge<
|
|
2557
|
+
// Incoming extends AnyZodObject,
|
|
2558
|
+
// Augmentation extends Incoming["shape"],
|
|
2559
|
+
// NewOutput extends {
|
|
2560
|
+
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
|
|
2561
|
+
// ? Augmentation[k]["_output"]
|
|
2562
|
+
// : k extends keyof Output
|
|
2563
|
+
// ? Output[k]
|
|
2564
|
+
// : never;
|
|
2565
|
+
// },
|
|
2566
|
+
// NewInput extends {
|
|
2567
|
+
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
|
|
2568
|
+
// ? Augmentation[k]["_input"]
|
|
2569
|
+
// : k extends keyof Input
|
|
2570
|
+
// ? Input[k]
|
|
2571
|
+
// : never;
|
|
2572
|
+
// }
|
|
2573
|
+
// >(
|
|
2574
|
+
// merging: Incoming
|
|
2575
|
+
// ): ZodObject<
|
|
2576
|
+
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
2577
|
+
// Incoming["_def"]["unknownKeys"],
|
|
2578
|
+
// Incoming["_def"]["catchall"],
|
|
2579
|
+
// NewOutput,
|
|
2580
|
+
// NewInput
|
|
2581
|
+
// > {
|
|
2582
|
+
// const merged: any = new ZodObject({
|
|
2583
|
+
// unknownKeys: merging._def.unknownKeys,
|
|
2584
|
+
// catchall: merging._def.catchall,
|
|
2585
|
+
// shape: () =>
|
|
2586
|
+
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
2587
|
+
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2588
|
+
// }) as any;
|
|
2589
|
+
// return merged;
|
|
2590
|
+
// }
|
|
2591
|
+
setKey(key, schema) {
|
|
2592
|
+
return this.augment({ [key]: schema });
|
|
2593
|
+
}
|
|
2594
|
+
// merge<Incoming extends AnyZodObject>(
|
|
2595
|
+
// merging: Incoming
|
|
2596
|
+
// ): //ZodObject<T & Incoming["_shape"], UnknownKeys, Catchall> = (merging) => {
|
|
2597
|
+
// ZodObject<
|
|
2598
|
+
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
2599
|
+
// Incoming["_def"]["unknownKeys"],
|
|
2600
|
+
// Incoming["_def"]["catchall"]
|
|
2601
|
+
// > {
|
|
2602
|
+
// // const mergedShape = objectUtil.mergeShapes(
|
|
2603
|
+
// // this._def.shape(),
|
|
2604
|
+
// // merging._def.shape()
|
|
2605
|
+
// // );
|
|
2606
|
+
// const merged: any = new ZodObject({
|
|
2607
|
+
// unknownKeys: merging._def.unknownKeys,
|
|
2608
|
+
// catchall: merging._def.catchall,
|
|
2609
|
+
// shape: () =>
|
|
2610
|
+
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
2611
|
+
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2612
|
+
// }) as any;
|
|
2613
|
+
// return merged;
|
|
2614
|
+
// }
|
|
2615
|
+
catchall(index) {
|
|
2616
|
+
return new _ZodObject({
|
|
2617
|
+
...this._def,
|
|
2618
|
+
catchall: index
|
|
2619
|
+
});
|
|
2620
|
+
}
|
|
2621
|
+
pick(mask) {
|
|
2622
|
+
const shape = {};
|
|
2623
|
+
for (const key of util.objectKeys(mask)) {
|
|
2624
|
+
if (mask[key] && this.shape[key]) {
|
|
2625
|
+
shape[key] = this.shape[key];
|
|
2626
|
+
}
|
|
2627
|
+
}
|
|
2628
|
+
return new _ZodObject({
|
|
2629
|
+
...this._def,
|
|
2630
|
+
shape: () => shape
|
|
2631
|
+
});
|
|
2632
|
+
}
|
|
2633
|
+
omit(mask) {
|
|
2634
|
+
const shape = {};
|
|
2635
|
+
for (const key of util.objectKeys(this.shape)) {
|
|
2636
|
+
if (!mask[key]) {
|
|
2637
|
+
shape[key] = this.shape[key];
|
|
2638
|
+
}
|
|
2639
|
+
}
|
|
2640
|
+
return new _ZodObject({
|
|
2641
|
+
...this._def,
|
|
2642
|
+
shape: () => shape
|
|
2643
|
+
});
|
|
2644
|
+
}
|
|
2645
|
+
/**
|
|
2646
|
+
* @deprecated
|
|
2647
|
+
*/
|
|
2648
|
+
deepPartial() {
|
|
2649
|
+
return deepPartialify(this);
|
|
2650
|
+
}
|
|
2651
|
+
partial(mask) {
|
|
2652
|
+
const newShape = {};
|
|
2653
|
+
for (const key of util.objectKeys(this.shape)) {
|
|
2654
|
+
const fieldSchema = this.shape[key];
|
|
2655
|
+
if (mask && !mask[key]) {
|
|
2656
|
+
newShape[key] = fieldSchema;
|
|
2657
|
+
} else {
|
|
2658
|
+
newShape[key] = fieldSchema.optional();
|
|
2659
|
+
}
|
|
2660
|
+
}
|
|
2661
|
+
return new _ZodObject({
|
|
2662
|
+
...this._def,
|
|
2663
|
+
shape: () => newShape
|
|
2664
|
+
});
|
|
2665
|
+
}
|
|
2666
|
+
required(mask) {
|
|
2667
|
+
const newShape = {};
|
|
2668
|
+
for (const key of util.objectKeys(this.shape)) {
|
|
2669
|
+
if (mask && !mask[key]) {
|
|
2670
|
+
newShape[key] = this.shape[key];
|
|
2671
|
+
} else {
|
|
2672
|
+
const fieldSchema = this.shape[key];
|
|
2673
|
+
let newField = fieldSchema;
|
|
2674
|
+
while (newField instanceof ZodOptional) {
|
|
2675
|
+
newField = newField._def.innerType;
|
|
2676
|
+
}
|
|
2677
|
+
newShape[key] = newField;
|
|
2678
|
+
}
|
|
2679
|
+
}
|
|
2680
|
+
return new _ZodObject({
|
|
2681
|
+
...this._def,
|
|
2682
|
+
shape: () => newShape
|
|
2683
|
+
});
|
|
2684
|
+
}
|
|
2685
|
+
keyof() {
|
|
2686
|
+
return createZodEnum(util.objectKeys(this.shape));
|
|
2687
|
+
}
|
|
2688
|
+
};
|
|
2689
|
+
ZodObject.create = (shape, params) => {
|
|
2690
|
+
return new ZodObject({
|
|
2691
|
+
shape: () => shape,
|
|
2692
|
+
unknownKeys: "strip",
|
|
2693
|
+
catchall: ZodNever.create(),
|
|
2694
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2695
|
+
...processCreateParams(params)
|
|
2696
|
+
});
|
|
2697
|
+
};
|
|
2698
|
+
ZodObject.strictCreate = (shape, params) => {
|
|
2699
|
+
return new ZodObject({
|
|
2700
|
+
shape: () => shape,
|
|
2701
|
+
unknownKeys: "strict",
|
|
2702
|
+
catchall: ZodNever.create(),
|
|
2703
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2704
|
+
...processCreateParams(params)
|
|
2705
|
+
});
|
|
2706
|
+
};
|
|
2707
|
+
ZodObject.lazycreate = (shape, params) => {
|
|
2708
|
+
return new ZodObject({
|
|
2709
|
+
shape,
|
|
2710
|
+
unknownKeys: "strip",
|
|
2711
|
+
catchall: ZodNever.create(),
|
|
2712
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2713
|
+
...processCreateParams(params)
|
|
2714
|
+
});
|
|
2715
|
+
};
|
|
2716
|
+
var ZodUnion = class extends ZodType {
|
|
2717
|
+
_parse(input) {
|
|
2718
|
+
const { ctx } = this._processInputParams(input);
|
|
2719
|
+
const options = this._def.options;
|
|
2720
|
+
function handleResults(results) {
|
|
2721
|
+
for (const result of results) {
|
|
2722
|
+
if (result.result.status === "valid") {
|
|
2723
|
+
return result.result;
|
|
2724
|
+
}
|
|
2725
|
+
}
|
|
2726
|
+
for (const result of results) {
|
|
2727
|
+
if (result.result.status === "dirty") {
|
|
2728
|
+
ctx.common.issues.push(...result.ctx.common.issues);
|
|
2729
|
+
return result.result;
|
|
2730
|
+
}
|
|
2731
|
+
}
|
|
2732
|
+
const unionErrors = results.map((result) => new ZodError(result.ctx.common.issues));
|
|
2733
|
+
addIssueToContext(ctx, {
|
|
2734
|
+
code: ZodIssueCode.invalid_union,
|
|
2735
|
+
unionErrors
|
|
2736
|
+
});
|
|
2737
|
+
return INVALID;
|
|
2738
|
+
}
|
|
2739
|
+
if (ctx.common.async) {
|
|
2740
|
+
return Promise.all(options.map(async (option) => {
|
|
2741
|
+
const childCtx = {
|
|
2742
|
+
...ctx,
|
|
2743
|
+
common: {
|
|
2744
|
+
...ctx.common,
|
|
2745
|
+
issues: []
|
|
2746
|
+
},
|
|
2747
|
+
parent: null
|
|
2748
|
+
};
|
|
2749
|
+
return {
|
|
2750
|
+
result: await option._parseAsync({
|
|
2751
|
+
data: ctx.data,
|
|
2752
|
+
path: ctx.path,
|
|
2753
|
+
parent: childCtx
|
|
2754
|
+
}),
|
|
2755
|
+
ctx: childCtx
|
|
2756
|
+
};
|
|
2757
|
+
})).then(handleResults);
|
|
2758
|
+
} else {
|
|
2759
|
+
let dirty = void 0;
|
|
2760
|
+
const issues = [];
|
|
2761
|
+
for (const option of options) {
|
|
2762
|
+
const childCtx = {
|
|
2763
|
+
...ctx,
|
|
2764
|
+
common: {
|
|
2765
|
+
...ctx.common,
|
|
2766
|
+
issues: []
|
|
2767
|
+
},
|
|
2768
|
+
parent: null
|
|
2769
|
+
};
|
|
2770
|
+
const result = option._parseSync({
|
|
2771
|
+
data: ctx.data,
|
|
2772
|
+
path: ctx.path,
|
|
2773
|
+
parent: childCtx
|
|
2774
|
+
});
|
|
2775
|
+
if (result.status === "valid") {
|
|
2776
|
+
return result;
|
|
2777
|
+
} else if (result.status === "dirty" && !dirty) {
|
|
2778
|
+
dirty = { result, ctx: childCtx };
|
|
2779
|
+
}
|
|
2780
|
+
if (childCtx.common.issues.length) {
|
|
2781
|
+
issues.push(childCtx.common.issues);
|
|
2782
|
+
}
|
|
2783
|
+
}
|
|
2784
|
+
if (dirty) {
|
|
2785
|
+
ctx.common.issues.push(...dirty.ctx.common.issues);
|
|
2786
|
+
return dirty.result;
|
|
2787
|
+
}
|
|
2788
|
+
const unionErrors = issues.map((issues2) => new ZodError(issues2));
|
|
2789
|
+
addIssueToContext(ctx, {
|
|
2790
|
+
code: ZodIssueCode.invalid_union,
|
|
2791
|
+
unionErrors
|
|
2792
|
+
});
|
|
2793
|
+
return INVALID;
|
|
2794
|
+
}
|
|
2795
|
+
}
|
|
2796
|
+
get options() {
|
|
2797
|
+
return this._def.options;
|
|
2798
|
+
}
|
|
2799
|
+
};
|
|
2800
|
+
ZodUnion.create = (types, params) => {
|
|
2801
|
+
return new ZodUnion({
|
|
2802
|
+
options: types,
|
|
2803
|
+
typeName: ZodFirstPartyTypeKind.ZodUnion,
|
|
2804
|
+
...processCreateParams(params)
|
|
2805
|
+
});
|
|
2806
|
+
};
|
|
2807
|
+
var getDiscriminator = (type) => {
|
|
2808
|
+
if (type instanceof ZodLazy) {
|
|
2809
|
+
return getDiscriminator(type.schema);
|
|
2810
|
+
} else if (type instanceof ZodEffects) {
|
|
2811
|
+
return getDiscriminator(type.innerType());
|
|
2812
|
+
} else if (type instanceof ZodLiteral) {
|
|
2813
|
+
return [type.value];
|
|
2814
|
+
} else if (type instanceof ZodEnum) {
|
|
2815
|
+
return type.options;
|
|
2816
|
+
} else if (type instanceof ZodNativeEnum) {
|
|
2817
|
+
return util.objectValues(type.enum);
|
|
2818
|
+
} else if (type instanceof ZodDefault) {
|
|
2819
|
+
return getDiscriminator(type._def.innerType);
|
|
2820
|
+
} else if (type instanceof ZodUndefined) {
|
|
2821
|
+
return [void 0];
|
|
2822
|
+
} else if (type instanceof ZodNull) {
|
|
2823
|
+
return [null];
|
|
2824
|
+
} else if (type instanceof ZodOptional) {
|
|
2825
|
+
return [void 0, ...getDiscriminator(type.unwrap())];
|
|
2826
|
+
} else if (type instanceof ZodNullable) {
|
|
2827
|
+
return [null, ...getDiscriminator(type.unwrap())];
|
|
2828
|
+
} else if (type instanceof ZodBranded) {
|
|
2829
|
+
return getDiscriminator(type.unwrap());
|
|
2830
|
+
} else if (type instanceof ZodReadonly) {
|
|
2831
|
+
return getDiscriminator(type.unwrap());
|
|
2832
|
+
} else if (type instanceof ZodCatch) {
|
|
2833
|
+
return getDiscriminator(type._def.innerType);
|
|
2834
|
+
} else {
|
|
2835
|
+
return [];
|
|
2836
|
+
}
|
|
2837
|
+
};
|
|
2838
|
+
var ZodDiscriminatedUnion = class _ZodDiscriminatedUnion extends ZodType {
|
|
2839
|
+
_parse(input) {
|
|
2840
|
+
const { ctx } = this._processInputParams(input);
|
|
2841
|
+
if (ctx.parsedType !== ZodParsedType.object) {
|
|
2842
|
+
addIssueToContext(ctx, {
|
|
2843
|
+
code: ZodIssueCode.invalid_type,
|
|
2844
|
+
expected: ZodParsedType.object,
|
|
2845
|
+
received: ctx.parsedType
|
|
2846
|
+
});
|
|
2847
|
+
return INVALID;
|
|
2848
|
+
}
|
|
2849
|
+
const discriminator = this.discriminator;
|
|
2850
|
+
const discriminatorValue = ctx.data[discriminator];
|
|
2851
|
+
const option = this.optionsMap.get(discriminatorValue);
|
|
2852
|
+
if (!option) {
|
|
2853
|
+
addIssueToContext(ctx, {
|
|
2854
|
+
code: ZodIssueCode.invalid_union_discriminator,
|
|
2855
|
+
options: Array.from(this.optionsMap.keys()),
|
|
2856
|
+
path: [discriminator]
|
|
2857
|
+
});
|
|
2858
|
+
return INVALID;
|
|
2859
|
+
}
|
|
2860
|
+
if (ctx.common.async) {
|
|
2861
|
+
return option._parseAsync({
|
|
2862
|
+
data: ctx.data,
|
|
2863
|
+
path: ctx.path,
|
|
2864
|
+
parent: ctx
|
|
2865
|
+
});
|
|
2866
|
+
} else {
|
|
2867
|
+
return option._parseSync({
|
|
2868
|
+
data: ctx.data,
|
|
2869
|
+
path: ctx.path,
|
|
2870
|
+
parent: ctx
|
|
2871
|
+
});
|
|
2872
|
+
}
|
|
2873
|
+
}
|
|
2874
|
+
get discriminator() {
|
|
2875
|
+
return this._def.discriminator;
|
|
2876
|
+
}
|
|
2877
|
+
get options() {
|
|
2878
|
+
return this._def.options;
|
|
2879
|
+
}
|
|
2880
|
+
get optionsMap() {
|
|
2881
|
+
return this._def.optionsMap;
|
|
2882
|
+
}
|
|
2883
|
+
/**
|
|
2884
|
+
* The constructor of the discriminated union schema. Its behaviour is very similar to that of the normal z.union() constructor.
|
|
2885
|
+
* However, it only allows a union of objects, all of which need to share a discriminator property. This property must
|
|
2886
|
+
* have a different value for each object in the union.
|
|
2887
|
+
* @param discriminator the name of the discriminator property
|
|
2888
|
+
* @param types an array of object schemas
|
|
2889
|
+
* @param params
|
|
2890
|
+
*/
|
|
2891
|
+
static create(discriminator, options, params) {
|
|
2892
|
+
const optionsMap = /* @__PURE__ */ new Map();
|
|
2893
|
+
for (const type of options) {
|
|
2894
|
+
const discriminatorValues = getDiscriminator(type.shape[discriminator]);
|
|
2895
|
+
if (!discriminatorValues.length) {
|
|
2896
|
+
throw new Error(`A discriminator value for key \`${discriminator}\` could not be extracted from all schema options`);
|
|
2897
|
+
}
|
|
2898
|
+
for (const value of discriminatorValues) {
|
|
2899
|
+
if (optionsMap.has(value)) {
|
|
2900
|
+
throw new Error(`Discriminator property ${String(discriminator)} has duplicate value ${String(value)}`);
|
|
2901
|
+
}
|
|
2902
|
+
optionsMap.set(value, type);
|
|
2903
|
+
}
|
|
2904
|
+
}
|
|
2905
|
+
return new _ZodDiscriminatedUnion({
|
|
2906
|
+
typeName: ZodFirstPartyTypeKind.ZodDiscriminatedUnion,
|
|
2907
|
+
discriminator,
|
|
2908
|
+
options,
|
|
2909
|
+
optionsMap,
|
|
2910
|
+
...processCreateParams(params)
|
|
2911
|
+
});
|
|
2912
|
+
}
|
|
2913
|
+
};
|
|
2914
|
+
function mergeValues(a, b) {
|
|
2915
|
+
const aType = getParsedType(a);
|
|
2916
|
+
const bType = getParsedType(b);
|
|
2917
|
+
if (a === b) {
|
|
2918
|
+
return { valid: true, data: a };
|
|
2919
|
+
} else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
|
|
2920
|
+
const bKeys = util.objectKeys(b);
|
|
2921
|
+
const sharedKeys = util.objectKeys(a).filter((key) => bKeys.indexOf(key) !== -1);
|
|
2922
|
+
const newObj = { ...a, ...b };
|
|
2923
|
+
for (const key of sharedKeys) {
|
|
2924
|
+
const sharedValue = mergeValues(a[key], b[key]);
|
|
2925
|
+
if (!sharedValue.valid) {
|
|
2926
|
+
return { valid: false };
|
|
2927
|
+
}
|
|
2928
|
+
newObj[key] = sharedValue.data;
|
|
2929
|
+
}
|
|
2930
|
+
return { valid: true, data: newObj };
|
|
2931
|
+
} else if (aType === ZodParsedType.array && bType === ZodParsedType.array) {
|
|
2932
|
+
if (a.length !== b.length) {
|
|
2933
|
+
return { valid: false };
|
|
2934
|
+
}
|
|
2935
|
+
const newArray = [];
|
|
2936
|
+
for (let index = 0; index < a.length; index++) {
|
|
2937
|
+
const itemA = a[index];
|
|
2938
|
+
const itemB = b[index];
|
|
2939
|
+
const sharedValue = mergeValues(itemA, itemB);
|
|
2940
|
+
if (!sharedValue.valid) {
|
|
2941
|
+
return { valid: false };
|
|
2942
|
+
}
|
|
2943
|
+
newArray.push(sharedValue.data);
|
|
2944
|
+
}
|
|
2945
|
+
return { valid: true, data: newArray };
|
|
2946
|
+
} else if (aType === ZodParsedType.date && bType === ZodParsedType.date && +a === +b) {
|
|
2947
|
+
return { valid: true, data: a };
|
|
2948
|
+
} else {
|
|
2949
|
+
return { valid: false };
|
|
2950
|
+
}
|
|
2951
|
+
}
|
|
2952
|
+
var ZodIntersection = class extends ZodType {
|
|
2953
|
+
_parse(input) {
|
|
2954
|
+
const { status, ctx } = this._processInputParams(input);
|
|
2955
|
+
const handleParsed = (parsedLeft, parsedRight) => {
|
|
2956
|
+
if (isAborted(parsedLeft) || isAborted(parsedRight)) {
|
|
2957
|
+
return INVALID;
|
|
2958
|
+
}
|
|
2959
|
+
const merged = mergeValues(parsedLeft.value, parsedRight.value);
|
|
2960
|
+
if (!merged.valid) {
|
|
2961
|
+
addIssueToContext(ctx, {
|
|
2962
|
+
code: ZodIssueCode.invalid_intersection_types
|
|
2963
|
+
});
|
|
2964
|
+
return INVALID;
|
|
2965
|
+
}
|
|
2966
|
+
if (isDirty(parsedLeft) || isDirty(parsedRight)) {
|
|
2967
|
+
status.dirty();
|
|
2968
|
+
}
|
|
2969
|
+
return { status: status.value, value: merged.data };
|
|
2970
|
+
};
|
|
2971
|
+
if (ctx.common.async) {
|
|
2972
|
+
return Promise.all([
|
|
2973
|
+
this._def.left._parseAsync({
|
|
2974
|
+
data: ctx.data,
|
|
2975
|
+
path: ctx.path,
|
|
2976
|
+
parent: ctx
|
|
2977
|
+
}),
|
|
2978
|
+
this._def.right._parseAsync({
|
|
2979
|
+
data: ctx.data,
|
|
2980
|
+
path: ctx.path,
|
|
2981
|
+
parent: ctx
|
|
2982
|
+
})
|
|
2983
|
+
]).then(([left, right]) => handleParsed(left, right));
|
|
2984
|
+
} else {
|
|
2985
|
+
return handleParsed(this._def.left._parseSync({
|
|
2986
|
+
data: ctx.data,
|
|
2987
|
+
path: ctx.path,
|
|
2988
|
+
parent: ctx
|
|
2989
|
+
}), this._def.right._parseSync({
|
|
2990
|
+
data: ctx.data,
|
|
2991
|
+
path: ctx.path,
|
|
2992
|
+
parent: ctx
|
|
2993
|
+
}));
|
|
2994
|
+
}
|
|
2995
|
+
}
|
|
2996
|
+
};
|
|
2997
|
+
ZodIntersection.create = (left, right, params) => {
|
|
2998
|
+
return new ZodIntersection({
|
|
2999
|
+
left,
|
|
3000
|
+
right,
|
|
3001
|
+
typeName: ZodFirstPartyTypeKind.ZodIntersection,
|
|
3002
|
+
...processCreateParams(params)
|
|
3003
|
+
});
|
|
3004
|
+
};
|
|
3005
|
+
var ZodTuple = class _ZodTuple extends ZodType {
|
|
3006
|
+
_parse(input) {
|
|
3007
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3008
|
+
if (ctx.parsedType !== ZodParsedType.array) {
|
|
3009
|
+
addIssueToContext(ctx, {
|
|
3010
|
+
code: ZodIssueCode.invalid_type,
|
|
3011
|
+
expected: ZodParsedType.array,
|
|
3012
|
+
received: ctx.parsedType
|
|
3013
|
+
});
|
|
3014
|
+
return INVALID;
|
|
3015
|
+
}
|
|
3016
|
+
if (ctx.data.length < this._def.items.length) {
|
|
3017
|
+
addIssueToContext(ctx, {
|
|
3018
|
+
code: ZodIssueCode.too_small,
|
|
3019
|
+
minimum: this._def.items.length,
|
|
3020
|
+
inclusive: true,
|
|
3021
|
+
exact: false,
|
|
3022
|
+
type: "array"
|
|
3023
|
+
});
|
|
3024
|
+
return INVALID;
|
|
3025
|
+
}
|
|
3026
|
+
const rest = this._def.rest;
|
|
3027
|
+
if (!rest && ctx.data.length > this._def.items.length) {
|
|
3028
|
+
addIssueToContext(ctx, {
|
|
3029
|
+
code: ZodIssueCode.too_big,
|
|
3030
|
+
maximum: this._def.items.length,
|
|
3031
|
+
inclusive: true,
|
|
3032
|
+
exact: false,
|
|
3033
|
+
type: "array"
|
|
3034
|
+
});
|
|
3035
|
+
status.dirty();
|
|
3036
|
+
}
|
|
3037
|
+
const items = [...ctx.data].map((item, itemIndex) => {
|
|
3038
|
+
const schema = this._def.items[itemIndex] || this._def.rest;
|
|
3039
|
+
if (!schema)
|
|
3040
|
+
return null;
|
|
3041
|
+
return schema._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
|
|
3042
|
+
}).filter((x) => !!x);
|
|
3043
|
+
if (ctx.common.async) {
|
|
3044
|
+
return Promise.all(items).then((results) => {
|
|
3045
|
+
return ParseStatus.mergeArray(status, results);
|
|
3046
|
+
});
|
|
3047
|
+
} else {
|
|
3048
|
+
return ParseStatus.mergeArray(status, items);
|
|
3049
|
+
}
|
|
3050
|
+
}
|
|
3051
|
+
get items() {
|
|
3052
|
+
return this._def.items;
|
|
3053
|
+
}
|
|
3054
|
+
rest(rest) {
|
|
3055
|
+
return new _ZodTuple({
|
|
3056
|
+
...this._def,
|
|
3057
|
+
rest
|
|
3058
|
+
});
|
|
3059
|
+
}
|
|
3060
|
+
};
|
|
3061
|
+
ZodTuple.create = (schemas, params) => {
|
|
3062
|
+
if (!Array.isArray(schemas)) {
|
|
3063
|
+
throw new Error("You must pass an array of schemas to z.tuple([ ... ])");
|
|
3064
|
+
}
|
|
3065
|
+
return new ZodTuple({
|
|
3066
|
+
items: schemas,
|
|
3067
|
+
typeName: ZodFirstPartyTypeKind.ZodTuple,
|
|
3068
|
+
rest: null,
|
|
3069
|
+
...processCreateParams(params)
|
|
3070
|
+
});
|
|
3071
|
+
};
|
|
3072
|
+
var ZodRecord = class _ZodRecord extends ZodType {
|
|
3073
|
+
get keySchema() {
|
|
3074
|
+
return this._def.keyType;
|
|
3075
|
+
}
|
|
3076
|
+
get valueSchema() {
|
|
3077
|
+
return this._def.valueType;
|
|
3078
|
+
}
|
|
3079
|
+
_parse(input) {
|
|
3080
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3081
|
+
if (ctx.parsedType !== ZodParsedType.object) {
|
|
3082
|
+
addIssueToContext(ctx, {
|
|
3083
|
+
code: ZodIssueCode.invalid_type,
|
|
3084
|
+
expected: ZodParsedType.object,
|
|
3085
|
+
received: ctx.parsedType
|
|
3086
|
+
});
|
|
3087
|
+
return INVALID;
|
|
3088
|
+
}
|
|
3089
|
+
const pairs = [];
|
|
3090
|
+
const keyType = this._def.keyType;
|
|
3091
|
+
const valueType = this._def.valueType;
|
|
3092
|
+
for (const key in ctx.data) {
|
|
3093
|
+
pairs.push({
|
|
3094
|
+
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
|
|
3095
|
+
value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
|
|
3096
|
+
alwaysSet: key in ctx.data
|
|
3097
|
+
});
|
|
3098
|
+
}
|
|
3099
|
+
if (ctx.common.async) {
|
|
3100
|
+
return ParseStatus.mergeObjectAsync(status, pairs);
|
|
3101
|
+
} else {
|
|
3102
|
+
return ParseStatus.mergeObjectSync(status, pairs);
|
|
3103
|
+
}
|
|
3104
|
+
}
|
|
3105
|
+
get element() {
|
|
3106
|
+
return this._def.valueType;
|
|
3107
|
+
}
|
|
3108
|
+
static create(first, second, third) {
|
|
3109
|
+
if (second instanceof ZodType) {
|
|
3110
|
+
return new _ZodRecord({
|
|
3111
|
+
keyType: first,
|
|
3112
|
+
valueType: second,
|
|
3113
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
3114
|
+
...processCreateParams(third)
|
|
3115
|
+
});
|
|
3116
|
+
}
|
|
3117
|
+
return new _ZodRecord({
|
|
3118
|
+
keyType: ZodString.create(),
|
|
3119
|
+
valueType: first,
|
|
3120
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
3121
|
+
...processCreateParams(second)
|
|
3122
|
+
});
|
|
3123
|
+
}
|
|
3124
|
+
};
|
|
3125
|
+
var ZodMap = class extends ZodType {
|
|
3126
|
+
get keySchema() {
|
|
3127
|
+
return this._def.keyType;
|
|
3128
|
+
}
|
|
3129
|
+
get valueSchema() {
|
|
3130
|
+
return this._def.valueType;
|
|
3131
|
+
}
|
|
3132
|
+
_parse(input) {
|
|
3133
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3134
|
+
if (ctx.parsedType !== ZodParsedType.map) {
|
|
3135
|
+
addIssueToContext(ctx, {
|
|
3136
|
+
code: ZodIssueCode.invalid_type,
|
|
3137
|
+
expected: ZodParsedType.map,
|
|
3138
|
+
received: ctx.parsedType
|
|
3139
|
+
});
|
|
3140
|
+
return INVALID;
|
|
3141
|
+
}
|
|
3142
|
+
const keyType = this._def.keyType;
|
|
3143
|
+
const valueType = this._def.valueType;
|
|
3144
|
+
const pairs = [...ctx.data.entries()].map(([key, value], index) => {
|
|
3145
|
+
return {
|
|
3146
|
+
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, [index, "key"])),
|
|
3147
|
+
value: valueType._parse(new ParseInputLazyPath(ctx, value, ctx.path, [index, "value"]))
|
|
3148
|
+
};
|
|
3149
|
+
});
|
|
3150
|
+
if (ctx.common.async) {
|
|
3151
|
+
const finalMap = /* @__PURE__ */ new Map();
|
|
3152
|
+
return Promise.resolve().then(async () => {
|
|
3153
|
+
for (const pair of pairs) {
|
|
3154
|
+
const key = await pair.key;
|
|
3155
|
+
const value = await pair.value;
|
|
3156
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
3157
|
+
return INVALID;
|
|
3158
|
+
}
|
|
3159
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
3160
|
+
status.dirty();
|
|
3161
|
+
}
|
|
3162
|
+
finalMap.set(key.value, value.value);
|
|
3163
|
+
}
|
|
3164
|
+
return { status: status.value, value: finalMap };
|
|
3165
|
+
});
|
|
3166
|
+
} else {
|
|
3167
|
+
const finalMap = /* @__PURE__ */ new Map();
|
|
3168
|
+
for (const pair of pairs) {
|
|
3169
|
+
const key = pair.key;
|
|
3170
|
+
const value = pair.value;
|
|
3171
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
3172
|
+
return INVALID;
|
|
3173
|
+
}
|
|
3174
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
3175
|
+
status.dirty();
|
|
3176
|
+
}
|
|
3177
|
+
finalMap.set(key.value, value.value);
|
|
3178
|
+
}
|
|
3179
|
+
return { status: status.value, value: finalMap };
|
|
3180
|
+
}
|
|
3181
|
+
}
|
|
3182
|
+
};
|
|
3183
|
+
ZodMap.create = (keyType, valueType, params) => {
|
|
3184
|
+
return new ZodMap({
|
|
3185
|
+
valueType,
|
|
3186
|
+
keyType,
|
|
3187
|
+
typeName: ZodFirstPartyTypeKind.ZodMap,
|
|
3188
|
+
...processCreateParams(params)
|
|
3189
|
+
});
|
|
3190
|
+
};
|
|
3191
|
+
var ZodSet = class _ZodSet extends ZodType {
|
|
3192
|
+
_parse(input) {
|
|
3193
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3194
|
+
if (ctx.parsedType !== ZodParsedType.set) {
|
|
3195
|
+
addIssueToContext(ctx, {
|
|
3196
|
+
code: ZodIssueCode.invalid_type,
|
|
3197
|
+
expected: ZodParsedType.set,
|
|
3198
|
+
received: ctx.parsedType
|
|
3199
|
+
});
|
|
3200
|
+
return INVALID;
|
|
3201
|
+
}
|
|
3202
|
+
const def = this._def;
|
|
3203
|
+
if (def.minSize !== null) {
|
|
3204
|
+
if (ctx.data.size < def.minSize.value) {
|
|
3205
|
+
addIssueToContext(ctx, {
|
|
3206
|
+
code: ZodIssueCode.too_small,
|
|
3207
|
+
minimum: def.minSize.value,
|
|
3208
|
+
type: "set",
|
|
3209
|
+
inclusive: true,
|
|
3210
|
+
exact: false,
|
|
3211
|
+
message: def.minSize.message
|
|
3212
|
+
});
|
|
3213
|
+
status.dirty();
|
|
3214
|
+
}
|
|
3215
|
+
}
|
|
3216
|
+
if (def.maxSize !== null) {
|
|
3217
|
+
if (ctx.data.size > def.maxSize.value) {
|
|
3218
|
+
addIssueToContext(ctx, {
|
|
3219
|
+
code: ZodIssueCode.too_big,
|
|
3220
|
+
maximum: def.maxSize.value,
|
|
3221
|
+
type: "set",
|
|
3222
|
+
inclusive: true,
|
|
3223
|
+
exact: false,
|
|
3224
|
+
message: def.maxSize.message
|
|
3225
|
+
});
|
|
3226
|
+
status.dirty();
|
|
3227
|
+
}
|
|
3228
|
+
}
|
|
3229
|
+
const valueType = this._def.valueType;
|
|
3230
|
+
function finalizeSet(elements2) {
|
|
3231
|
+
const parsedSet = /* @__PURE__ */ new Set();
|
|
3232
|
+
for (const element of elements2) {
|
|
3233
|
+
if (element.status === "aborted")
|
|
3234
|
+
return INVALID;
|
|
3235
|
+
if (element.status === "dirty")
|
|
3236
|
+
status.dirty();
|
|
3237
|
+
parsedSet.add(element.value);
|
|
3238
|
+
}
|
|
3239
|
+
return { status: status.value, value: parsedSet };
|
|
3240
|
+
}
|
|
3241
|
+
const elements = [...ctx.data.values()].map((item, i) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i)));
|
|
3242
|
+
if (ctx.common.async) {
|
|
3243
|
+
return Promise.all(elements).then((elements2) => finalizeSet(elements2));
|
|
3244
|
+
} else {
|
|
3245
|
+
return finalizeSet(elements);
|
|
3246
|
+
}
|
|
3247
|
+
}
|
|
3248
|
+
min(minSize, message) {
|
|
3249
|
+
return new _ZodSet({
|
|
3250
|
+
...this._def,
|
|
3251
|
+
minSize: { value: minSize, message: errorUtil.toString(message) }
|
|
3252
|
+
});
|
|
3253
|
+
}
|
|
3254
|
+
max(maxSize, message) {
|
|
3255
|
+
return new _ZodSet({
|
|
3256
|
+
...this._def,
|
|
3257
|
+
maxSize: { value: maxSize, message: errorUtil.toString(message) }
|
|
3258
|
+
});
|
|
3259
|
+
}
|
|
3260
|
+
size(size, message) {
|
|
3261
|
+
return this.min(size, message).max(size, message);
|
|
3262
|
+
}
|
|
3263
|
+
nonempty(message) {
|
|
3264
|
+
return this.min(1, message);
|
|
3265
|
+
}
|
|
3266
|
+
};
|
|
3267
|
+
ZodSet.create = (valueType, params) => {
|
|
3268
|
+
return new ZodSet({
|
|
3269
|
+
valueType,
|
|
3270
|
+
minSize: null,
|
|
3271
|
+
maxSize: null,
|
|
3272
|
+
typeName: ZodFirstPartyTypeKind.ZodSet,
|
|
3273
|
+
...processCreateParams(params)
|
|
3274
|
+
});
|
|
3275
|
+
};
|
|
3276
|
+
var ZodFunction = class _ZodFunction extends ZodType {
|
|
3277
|
+
constructor() {
|
|
3278
|
+
super(...arguments);
|
|
3279
|
+
this.validate = this.implement;
|
|
3280
|
+
}
|
|
3281
|
+
_parse(input) {
|
|
3282
|
+
const { ctx } = this._processInputParams(input);
|
|
3283
|
+
if (ctx.parsedType !== ZodParsedType.function) {
|
|
3284
|
+
addIssueToContext(ctx, {
|
|
3285
|
+
code: ZodIssueCode.invalid_type,
|
|
3286
|
+
expected: ZodParsedType.function,
|
|
3287
|
+
received: ctx.parsedType
|
|
3288
|
+
});
|
|
3289
|
+
return INVALID;
|
|
3290
|
+
}
|
|
3291
|
+
function makeArgsIssue(args, error) {
|
|
3292
|
+
return makeIssue({
|
|
3293
|
+
data: args,
|
|
3294
|
+
path: ctx.path,
|
|
3295
|
+
errorMaps: [ctx.common.contextualErrorMap, ctx.schemaErrorMap, getErrorMap(), en_default].filter((x) => !!x),
|
|
3296
|
+
issueData: {
|
|
3297
|
+
code: ZodIssueCode.invalid_arguments,
|
|
3298
|
+
argumentsError: error
|
|
3299
|
+
}
|
|
3300
|
+
});
|
|
3301
|
+
}
|
|
3302
|
+
function makeReturnsIssue(returns, error) {
|
|
3303
|
+
return makeIssue({
|
|
3304
|
+
data: returns,
|
|
3305
|
+
path: ctx.path,
|
|
3306
|
+
errorMaps: [ctx.common.contextualErrorMap, ctx.schemaErrorMap, getErrorMap(), en_default].filter((x) => !!x),
|
|
3307
|
+
issueData: {
|
|
3308
|
+
code: ZodIssueCode.invalid_return_type,
|
|
3309
|
+
returnTypeError: error
|
|
3310
|
+
}
|
|
3311
|
+
});
|
|
3312
|
+
}
|
|
3313
|
+
const params = { errorMap: ctx.common.contextualErrorMap };
|
|
3314
|
+
const fn = ctx.data;
|
|
3315
|
+
if (this._def.returns instanceof ZodPromise) {
|
|
3316
|
+
const me = this;
|
|
3317
|
+
return OK(async function(...args) {
|
|
3318
|
+
const error = new ZodError([]);
|
|
3319
|
+
const parsedArgs = await me._def.args.parseAsync(args, params).catch((e) => {
|
|
3320
|
+
error.addIssue(makeArgsIssue(args, e));
|
|
3321
|
+
throw error;
|
|
3322
|
+
});
|
|
3323
|
+
const result = await Reflect.apply(fn, this, parsedArgs);
|
|
3324
|
+
const parsedReturns = await me._def.returns._def.type.parseAsync(result, params).catch((e) => {
|
|
3325
|
+
error.addIssue(makeReturnsIssue(result, e));
|
|
3326
|
+
throw error;
|
|
3327
|
+
});
|
|
3328
|
+
return parsedReturns;
|
|
3329
|
+
});
|
|
3330
|
+
} else {
|
|
3331
|
+
const me = this;
|
|
3332
|
+
return OK(function(...args) {
|
|
3333
|
+
const parsedArgs = me._def.args.safeParse(args, params);
|
|
3334
|
+
if (!parsedArgs.success) {
|
|
3335
|
+
throw new ZodError([makeArgsIssue(args, parsedArgs.error)]);
|
|
3336
|
+
}
|
|
3337
|
+
const result = Reflect.apply(fn, this, parsedArgs.data);
|
|
3338
|
+
const parsedReturns = me._def.returns.safeParse(result, params);
|
|
3339
|
+
if (!parsedReturns.success) {
|
|
3340
|
+
throw new ZodError([makeReturnsIssue(result, parsedReturns.error)]);
|
|
3341
|
+
}
|
|
3342
|
+
return parsedReturns.data;
|
|
3343
|
+
});
|
|
3344
|
+
}
|
|
3345
|
+
}
|
|
3346
|
+
parameters() {
|
|
3347
|
+
return this._def.args;
|
|
3348
|
+
}
|
|
3349
|
+
returnType() {
|
|
3350
|
+
return this._def.returns;
|
|
3351
|
+
}
|
|
3352
|
+
args(...items) {
|
|
3353
|
+
return new _ZodFunction({
|
|
3354
|
+
...this._def,
|
|
3355
|
+
args: ZodTuple.create(items).rest(ZodUnknown.create())
|
|
3356
|
+
});
|
|
3357
|
+
}
|
|
3358
|
+
returns(returnType) {
|
|
3359
|
+
return new _ZodFunction({
|
|
3360
|
+
...this._def,
|
|
3361
|
+
returns: returnType
|
|
3362
|
+
});
|
|
3363
|
+
}
|
|
3364
|
+
implement(func) {
|
|
3365
|
+
const validatedFunc = this.parse(func);
|
|
3366
|
+
return validatedFunc;
|
|
3367
|
+
}
|
|
3368
|
+
strictImplement(func) {
|
|
3369
|
+
const validatedFunc = this.parse(func);
|
|
3370
|
+
return validatedFunc;
|
|
3371
|
+
}
|
|
3372
|
+
static create(args, returns, params) {
|
|
3373
|
+
return new _ZodFunction({
|
|
3374
|
+
args: args ? args : ZodTuple.create([]).rest(ZodUnknown.create()),
|
|
3375
|
+
returns: returns || ZodUnknown.create(),
|
|
3376
|
+
typeName: ZodFirstPartyTypeKind.ZodFunction,
|
|
3377
|
+
...processCreateParams(params)
|
|
3378
|
+
});
|
|
3379
|
+
}
|
|
3380
|
+
};
|
|
3381
|
+
var ZodLazy = class extends ZodType {
|
|
3382
|
+
get schema() {
|
|
3383
|
+
return this._def.getter();
|
|
3384
|
+
}
|
|
3385
|
+
_parse(input) {
|
|
3386
|
+
const { ctx } = this._processInputParams(input);
|
|
3387
|
+
const lazySchema = this._def.getter();
|
|
3388
|
+
return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx });
|
|
3389
|
+
}
|
|
3390
|
+
};
|
|
3391
|
+
ZodLazy.create = (getter, params) => {
|
|
3392
|
+
return new ZodLazy({
|
|
3393
|
+
getter,
|
|
3394
|
+
typeName: ZodFirstPartyTypeKind.ZodLazy,
|
|
3395
|
+
...processCreateParams(params)
|
|
3396
|
+
});
|
|
3397
|
+
};
|
|
3398
|
+
var ZodLiteral = class extends ZodType {
|
|
3399
|
+
_parse(input) {
|
|
3400
|
+
if (input.data !== this._def.value) {
|
|
3401
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3402
|
+
addIssueToContext(ctx, {
|
|
3403
|
+
received: ctx.data,
|
|
3404
|
+
code: ZodIssueCode.invalid_literal,
|
|
3405
|
+
expected: this._def.value
|
|
3406
|
+
});
|
|
3407
|
+
return INVALID;
|
|
3408
|
+
}
|
|
3409
|
+
return { status: "valid", value: input.data };
|
|
3410
|
+
}
|
|
3411
|
+
get value() {
|
|
3412
|
+
return this._def.value;
|
|
3413
|
+
}
|
|
3414
|
+
};
|
|
3415
|
+
ZodLiteral.create = (value, params) => {
|
|
3416
|
+
return new ZodLiteral({
|
|
3417
|
+
value,
|
|
3418
|
+
typeName: ZodFirstPartyTypeKind.ZodLiteral,
|
|
3419
|
+
...processCreateParams(params)
|
|
3420
|
+
});
|
|
3421
|
+
};
|
|
3422
|
+
function createZodEnum(values, params) {
|
|
3423
|
+
return new ZodEnum({
|
|
3424
|
+
values,
|
|
3425
|
+
typeName: ZodFirstPartyTypeKind.ZodEnum,
|
|
3426
|
+
...processCreateParams(params)
|
|
3427
|
+
});
|
|
3428
|
+
}
|
|
3429
|
+
var ZodEnum = class _ZodEnum extends ZodType {
|
|
3430
|
+
_parse(input) {
|
|
3431
|
+
if (typeof input.data !== "string") {
|
|
3432
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3433
|
+
const expectedValues = this._def.values;
|
|
3434
|
+
addIssueToContext(ctx, {
|
|
3435
|
+
expected: util.joinValues(expectedValues),
|
|
3436
|
+
received: ctx.parsedType,
|
|
3437
|
+
code: ZodIssueCode.invalid_type
|
|
3438
|
+
});
|
|
3439
|
+
return INVALID;
|
|
3440
|
+
}
|
|
3441
|
+
if (!this._cache) {
|
|
3442
|
+
this._cache = new Set(this._def.values);
|
|
3443
|
+
}
|
|
3444
|
+
if (!this._cache.has(input.data)) {
|
|
3445
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3446
|
+
const expectedValues = this._def.values;
|
|
3447
|
+
addIssueToContext(ctx, {
|
|
3448
|
+
received: ctx.data,
|
|
3449
|
+
code: ZodIssueCode.invalid_enum_value,
|
|
3450
|
+
options: expectedValues
|
|
3451
|
+
});
|
|
3452
|
+
return INVALID;
|
|
3453
|
+
}
|
|
3454
|
+
return OK(input.data);
|
|
3455
|
+
}
|
|
3456
|
+
get options() {
|
|
3457
|
+
return this._def.values;
|
|
3458
|
+
}
|
|
3459
|
+
get enum() {
|
|
3460
|
+
const enumValues = {};
|
|
3461
|
+
for (const val of this._def.values) {
|
|
3462
|
+
enumValues[val] = val;
|
|
3463
|
+
}
|
|
3464
|
+
return enumValues;
|
|
3465
|
+
}
|
|
3466
|
+
get Values() {
|
|
3467
|
+
const enumValues = {};
|
|
3468
|
+
for (const val of this._def.values) {
|
|
3469
|
+
enumValues[val] = val;
|
|
3470
|
+
}
|
|
3471
|
+
return enumValues;
|
|
3472
|
+
}
|
|
3473
|
+
get Enum() {
|
|
3474
|
+
const enumValues = {};
|
|
3475
|
+
for (const val of this._def.values) {
|
|
3476
|
+
enumValues[val] = val;
|
|
3477
|
+
}
|
|
3478
|
+
return enumValues;
|
|
3479
|
+
}
|
|
3480
|
+
extract(values, newDef = this._def) {
|
|
3481
|
+
return _ZodEnum.create(values, {
|
|
3482
|
+
...this._def,
|
|
3483
|
+
...newDef
|
|
3484
|
+
});
|
|
3485
|
+
}
|
|
3486
|
+
exclude(values, newDef = this._def) {
|
|
3487
|
+
return _ZodEnum.create(this.options.filter((opt) => !values.includes(opt)), {
|
|
3488
|
+
...this._def,
|
|
3489
|
+
...newDef
|
|
3490
|
+
});
|
|
3491
|
+
}
|
|
3492
|
+
};
|
|
3493
|
+
ZodEnum.create = createZodEnum;
|
|
3494
|
+
var ZodNativeEnum = class extends ZodType {
|
|
3495
|
+
_parse(input) {
|
|
3496
|
+
const nativeEnumValues = util.getValidEnumValues(this._def.values);
|
|
3497
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3498
|
+
if (ctx.parsedType !== ZodParsedType.string && ctx.parsedType !== ZodParsedType.number) {
|
|
3499
|
+
const expectedValues = util.objectValues(nativeEnumValues);
|
|
3500
|
+
addIssueToContext(ctx, {
|
|
3501
|
+
expected: util.joinValues(expectedValues),
|
|
3502
|
+
received: ctx.parsedType,
|
|
3503
|
+
code: ZodIssueCode.invalid_type
|
|
3504
|
+
});
|
|
3505
|
+
return INVALID;
|
|
3506
|
+
}
|
|
3507
|
+
if (!this._cache) {
|
|
3508
|
+
this._cache = new Set(util.getValidEnumValues(this._def.values));
|
|
3509
|
+
}
|
|
3510
|
+
if (!this._cache.has(input.data)) {
|
|
3511
|
+
const expectedValues = util.objectValues(nativeEnumValues);
|
|
3512
|
+
addIssueToContext(ctx, {
|
|
3513
|
+
received: ctx.data,
|
|
3514
|
+
code: ZodIssueCode.invalid_enum_value,
|
|
3515
|
+
options: expectedValues
|
|
3516
|
+
});
|
|
3517
|
+
return INVALID;
|
|
3518
|
+
}
|
|
3519
|
+
return OK(input.data);
|
|
3520
|
+
}
|
|
3521
|
+
get enum() {
|
|
3522
|
+
return this._def.values;
|
|
3523
|
+
}
|
|
3524
|
+
};
|
|
3525
|
+
ZodNativeEnum.create = (values, params) => {
|
|
3526
|
+
return new ZodNativeEnum({
|
|
3527
|
+
values,
|
|
3528
|
+
typeName: ZodFirstPartyTypeKind.ZodNativeEnum,
|
|
3529
|
+
...processCreateParams(params)
|
|
3530
|
+
});
|
|
3531
|
+
};
|
|
3532
|
+
var ZodPromise = class extends ZodType {
|
|
3533
|
+
unwrap() {
|
|
3534
|
+
return this._def.type;
|
|
3535
|
+
}
|
|
3536
|
+
_parse(input) {
|
|
3537
|
+
const { ctx } = this._processInputParams(input);
|
|
3538
|
+
if (ctx.parsedType !== ZodParsedType.promise && ctx.common.async === false) {
|
|
3539
|
+
addIssueToContext(ctx, {
|
|
3540
|
+
code: ZodIssueCode.invalid_type,
|
|
3541
|
+
expected: ZodParsedType.promise,
|
|
3542
|
+
received: ctx.parsedType
|
|
3543
|
+
});
|
|
3544
|
+
return INVALID;
|
|
3545
|
+
}
|
|
3546
|
+
const promisified = ctx.parsedType === ZodParsedType.promise ? ctx.data : Promise.resolve(ctx.data);
|
|
3547
|
+
return OK(promisified.then((data) => {
|
|
3548
|
+
return this._def.type.parseAsync(data, {
|
|
3549
|
+
path: ctx.path,
|
|
3550
|
+
errorMap: ctx.common.contextualErrorMap
|
|
3551
|
+
});
|
|
3552
|
+
}));
|
|
3553
|
+
}
|
|
3554
|
+
};
|
|
3555
|
+
ZodPromise.create = (schema, params) => {
|
|
3556
|
+
return new ZodPromise({
|
|
3557
|
+
type: schema,
|
|
3558
|
+
typeName: ZodFirstPartyTypeKind.ZodPromise,
|
|
3559
|
+
...processCreateParams(params)
|
|
3560
|
+
});
|
|
3561
|
+
};
|
|
3562
|
+
var ZodEffects = class extends ZodType {
|
|
3563
|
+
innerType() {
|
|
3564
|
+
return this._def.schema;
|
|
3565
|
+
}
|
|
3566
|
+
sourceType() {
|
|
3567
|
+
return this._def.schema._def.typeName === ZodFirstPartyTypeKind.ZodEffects ? this._def.schema.sourceType() : this._def.schema;
|
|
3568
|
+
}
|
|
3569
|
+
_parse(input) {
|
|
3570
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3571
|
+
const effect = this._def.effect || null;
|
|
3572
|
+
const checkCtx = {
|
|
3573
|
+
addIssue: (arg) => {
|
|
3574
|
+
addIssueToContext(ctx, arg);
|
|
3575
|
+
if (arg.fatal) {
|
|
3576
|
+
status.abort();
|
|
3577
|
+
} else {
|
|
3578
|
+
status.dirty();
|
|
3579
|
+
}
|
|
3580
|
+
},
|
|
3581
|
+
get path() {
|
|
3582
|
+
return ctx.path;
|
|
3583
|
+
}
|
|
3584
|
+
};
|
|
3585
|
+
checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
|
|
3586
|
+
if (effect.type === "preprocess") {
|
|
3587
|
+
const processed = effect.transform(ctx.data, checkCtx);
|
|
3588
|
+
if (ctx.common.async) {
|
|
3589
|
+
return Promise.resolve(processed).then(async (processed2) => {
|
|
3590
|
+
if (status.value === "aborted")
|
|
3591
|
+
return INVALID;
|
|
3592
|
+
const result = await this._def.schema._parseAsync({
|
|
3593
|
+
data: processed2,
|
|
3594
|
+
path: ctx.path,
|
|
3595
|
+
parent: ctx
|
|
3596
|
+
});
|
|
3597
|
+
if (result.status === "aborted")
|
|
3598
|
+
return INVALID;
|
|
3599
|
+
if (result.status === "dirty")
|
|
3600
|
+
return DIRTY(result.value);
|
|
3601
|
+
if (status.value === "dirty")
|
|
3602
|
+
return DIRTY(result.value);
|
|
3603
|
+
return result;
|
|
3604
|
+
});
|
|
3605
|
+
} else {
|
|
3606
|
+
if (status.value === "aborted")
|
|
3607
|
+
return INVALID;
|
|
3608
|
+
const result = this._def.schema._parseSync({
|
|
3609
|
+
data: processed,
|
|
3610
|
+
path: ctx.path,
|
|
3611
|
+
parent: ctx
|
|
3612
|
+
});
|
|
3613
|
+
if (result.status === "aborted")
|
|
3614
|
+
return INVALID;
|
|
3615
|
+
if (result.status === "dirty")
|
|
3616
|
+
return DIRTY(result.value);
|
|
3617
|
+
if (status.value === "dirty")
|
|
3618
|
+
return DIRTY(result.value);
|
|
3619
|
+
return result;
|
|
3620
|
+
}
|
|
3621
|
+
}
|
|
3622
|
+
if (effect.type === "refinement") {
|
|
3623
|
+
const executeRefinement = (acc) => {
|
|
3624
|
+
const result = effect.refinement(acc, checkCtx);
|
|
3625
|
+
if (ctx.common.async) {
|
|
3626
|
+
return Promise.resolve(result);
|
|
3627
|
+
}
|
|
3628
|
+
if (result instanceof Promise) {
|
|
3629
|
+
throw new Error("Async refinement encountered during synchronous parse operation. Use .parseAsync instead.");
|
|
3630
|
+
}
|
|
3631
|
+
return acc;
|
|
3632
|
+
};
|
|
3633
|
+
if (ctx.common.async === false) {
|
|
3634
|
+
const inner = this._def.schema._parseSync({
|
|
3635
|
+
data: ctx.data,
|
|
3636
|
+
path: ctx.path,
|
|
3637
|
+
parent: ctx
|
|
3638
|
+
});
|
|
3639
|
+
if (inner.status === "aborted")
|
|
3640
|
+
return INVALID;
|
|
3641
|
+
if (inner.status === "dirty")
|
|
3642
|
+
status.dirty();
|
|
3643
|
+
executeRefinement(inner.value);
|
|
3644
|
+
return { status: status.value, value: inner.value };
|
|
3645
|
+
} else {
|
|
3646
|
+
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((inner) => {
|
|
3647
|
+
if (inner.status === "aborted")
|
|
3648
|
+
return INVALID;
|
|
3649
|
+
if (inner.status === "dirty")
|
|
3650
|
+
status.dirty();
|
|
3651
|
+
return executeRefinement(inner.value).then(() => {
|
|
3652
|
+
return { status: status.value, value: inner.value };
|
|
3653
|
+
});
|
|
3654
|
+
});
|
|
3655
|
+
}
|
|
3656
|
+
}
|
|
3657
|
+
if (effect.type === "transform") {
|
|
3658
|
+
if (ctx.common.async === false) {
|
|
3659
|
+
const base = this._def.schema._parseSync({
|
|
3660
|
+
data: ctx.data,
|
|
3661
|
+
path: ctx.path,
|
|
3662
|
+
parent: ctx
|
|
3663
|
+
});
|
|
3664
|
+
if (!isValid(base))
|
|
3665
|
+
return INVALID;
|
|
3666
|
+
const result = effect.transform(base.value, checkCtx);
|
|
3667
|
+
if (result instanceof Promise) {
|
|
3668
|
+
throw new Error(`Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.`);
|
|
3669
|
+
}
|
|
3670
|
+
return { status: status.value, value: result };
|
|
3671
|
+
} else {
|
|
3672
|
+
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((base) => {
|
|
3673
|
+
if (!isValid(base))
|
|
3674
|
+
return INVALID;
|
|
3675
|
+
return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({
|
|
3676
|
+
status: status.value,
|
|
3677
|
+
value: result
|
|
3678
|
+
}));
|
|
3679
|
+
});
|
|
3680
|
+
}
|
|
3681
|
+
}
|
|
3682
|
+
util.assertNever(effect);
|
|
3683
|
+
}
|
|
3684
|
+
};
|
|
3685
|
+
ZodEffects.create = (schema, effect, params) => {
|
|
3686
|
+
return new ZodEffects({
|
|
3687
|
+
schema,
|
|
3688
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
3689
|
+
effect,
|
|
3690
|
+
...processCreateParams(params)
|
|
3691
|
+
});
|
|
3692
|
+
};
|
|
3693
|
+
ZodEffects.createWithPreprocess = (preprocess, schema, params) => {
|
|
3694
|
+
return new ZodEffects({
|
|
3695
|
+
schema,
|
|
3696
|
+
effect: { type: "preprocess", transform: preprocess },
|
|
3697
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
3698
|
+
...processCreateParams(params)
|
|
3699
|
+
});
|
|
3700
|
+
};
|
|
3701
|
+
var ZodOptional = class extends ZodType {
|
|
3702
|
+
_parse(input) {
|
|
3703
|
+
const parsedType = this._getType(input);
|
|
3704
|
+
if (parsedType === ZodParsedType.undefined) {
|
|
3705
|
+
return OK(void 0);
|
|
3706
|
+
}
|
|
3707
|
+
return this._def.innerType._parse(input);
|
|
3708
|
+
}
|
|
3709
|
+
unwrap() {
|
|
3710
|
+
return this._def.innerType;
|
|
3711
|
+
}
|
|
3712
|
+
};
|
|
3713
|
+
ZodOptional.create = (type, params) => {
|
|
3714
|
+
return new ZodOptional({
|
|
3715
|
+
innerType: type,
|
|
3716
|
+
typeName: ZodFirstPartyTypeKind.ZodOptional,
|
|
3717
|
+
...processCreateParams(params)
|
|
3718
|
+
});
|
|
3719
|
+
};
|
|
3720
|
+
var ZodNullable = class extends ZodType {
|
|
3721
|
+
_parse(input) {
|
|
3722
|
+
const parsedType = this._getType(input);
|
|
3723
|
+
if (parsedType === ZodParsedType.null) {
|
|
3724
|
+
return OK(null);
|
|
3725
|
+
}
|
|
3726
|
+
return this._def.innerType._parse(input);
|
|
3727
|
+
}
|
|
3728
|
+
unwrap() {
|
|
3729
|
+
return this._def.innerType;
|
|
3730
|
+
}
|
|
3731
|
+
};
|
|
3732
|
+
ZodNullable.create = (type, params) => {
|
|
3733
|
+
return new ZodNullable({
|
|
3734
|
+
innerType: type,
|
|
3735
|
+
typeName: ZodFirstPartyTypeKind.ZodNullable,
|
|
3736
|
+
...processCreateParams(params)
|
|
3737
|
+
});
|
|
3738
|
+
};
|
|
3739
|
+
var ZodDefault = class extends ZodType {
|
|
3740
|
+
_parse(input) {
|
|
3741
|
+
const { ctx } = this._processInputParams(input);
|
|
3742
|
+
let data = ctx.data;
|
|
3743
|
+
if (ctx.parsedType === ZodParsedType.undefined) {
|
|
3744
|
+
data = this._def.defaultValue();
|
|
3745
|
+
}
|
|
3746
|
+
return this._def.innerType._parse({
|
|
3747
|
+
data,
|
|
3748
|
+
path: ctx.path,
|
|
3749
|
+
parent: ctx
|
|
3750
|
+
});
|
|
3751
|
+
}
|
|
3752
|
+
removeDefault() {
|
|
3753
|
+
return this._def.innerType;
|
|
3754
|
+
}
|
|
3755
|
+
};
|
|
3756
|
+
ZodDefault.create = (type, params) => {
|
|
3757
|
+
return new ZodDefault({
|
|
3758
|
+
innerType: type,
|
|
3759
|
+
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
3760
|
+
defaultValue: typeof params.default === "function" ? params.default : () => params.default,
|
|
3761
|
+
...processCreateParams(params)
|
|
3762
|
+
});
|
|
3763
|
+
};
|
|
3764
|
+
var ZodCatch = class extends ZodType {
|
|
3765
|
+
_parse(input) {
|
|
3766
|
+
const { ctx } = this._processInputParams(input);
|
|
3767
|
+
const newCtx = {
|
|
3768
|
+
...ctx,
|
|
3769
|
+
common: {
|
|
3770
|
+
...ctx.common,
|
|
3771
|
+
issues: []
|
|
3772
|
+
}
|
|
3773
|
+
};
|
|
3774
|
+
const result = this._def.innerType._parse({
|
|
3775
|
+
data: newCtx.data,
|
|
3776
|
+
path: newCtx.path,
|
|
3777
|
+
parent: {
|
|
3778
|
+
...newCtx
|
|
3779
|
+
}
|
|
3780
|
+
});
|
|
3781
|
+
if (isAsync(result)) {
|
|
3782
|
+
return result.then((result2) => {
|
|
3783
|
+
return {
|
|
3784
|
+
status: "valid",
|
|
3785
|
+
value: result2.status === "valid" ? result2.value : this._def.catchValue({
|
|
3786
|
+
get error() {
|
|
3787
|
+
return new ZodError(newCtx.common.issues);
|
|
3788
|
+
},
|
|
3789
|
+
input: newCtx.data
|
|
3790
|
+
})
|
|
3791
|
+
};
|
|
3792
|
+
});
|
|
3793
|
+
} else {
|
|
3794
|
+
return {
|
|
3795
|
+
status: "valid",
|
|
3796
|
+
value: result.status === "valid" ? result.value : this._def.catchValue({
|
|
3797
|
+
get error() {
|
|
3798
|
+
return new ZodError(newCtx.common.issues);
|
|
3799
|
+
},
|
|
3800
|
+
input: newCtx.data
|
|
3801
|
+
})
|
|
3802
|
+
};
|
|
3803
|
+
}
|
|
3804
|
+
}
|
|
3805
|
+
removeCatch() {
|
|
3806
|
+
return this._def.innerType;
|
|
3807
|
+
}
|
|
3808
|
+
};
|
|
3809
|
+
ZodCatch.create = (type, params) => {
|
|
3810
|
+
return new ZodCatch({
|
|
3811
|
+
innerType: type,
|
|
3812
|
+
typeName: ZodFirstPartyTypeKind.ZodCatch,
|
|
3813
|
+
catchValue: typeof params.catch === "function" ? params.catch : () => params.catch,
|
|
3814
|
+
...processCreateParams(params)
|
|
3815
|
+
});
|
|
3816
|
+
};
|
|
3817
|
+
var ZodNaN = class extends ZodType {
|
|
3818
|
+
_parse(input) {
|
|
3819
|
+
const parsedType = this._getType(input);
|
|
3820
|
+
if (parsedType !== ZodParsedType.nan) {
|
|
3821
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3822
|
+
addIssueToContext(ctx, {
|
|
3823
|
+
code: ZodIssueCode.invalid_type,
|
|
3824
|
+
expected: ZodParsedType.nan,
|
|
3825
|
+
received: ctx.parsedType
|
|
3826
|
+
});
|
|
3827
|
+
return INVALID;
|
|
3828
|
+
}
|
|
3829
|
+
return { status: "valid", value: input.data };
|
|
3830
|
+
}
|
|
3831
|
+
};
|
|
3832
|
+
ZodNaN.create = (params) => {
|
|
3833
|
+
return new ZodNaN({
|
|
3834
|
+
typeName: ZodFirstPartyTypeKind.ZodNaN,
|
|
3835
|
+
...processCreateParams(params)
|
|
3836
|
+
});
|
|
3837
|
+
};
|
|
3838
|
+
var BRAND = Symbol("zod_brand");
|
|
3839
|
+
var ZodBranded = class extends ZodType {
|
|
3840
|
+
_parse(input) {
|
|
3841
|
+
const { ctx } = this._processInputParams(input);
|
|
3842
|
+
const data = ctx.data;
|
|
3843
|
+
return this._def.type._parse({
|
|
3844
|
+
data,
|
|
3845
|
+
path: ctx.path,
|
|
3846
|
+
parent: ctx
|
|
3847
|
+
});
|
|
3848
|
+
}
|
|
3849
|
+
unwrap() {
|
|
3850
|
+
return this._def.type;
|
|
3851
|
+
}
|
|
3852
|
+
};
|
|
3853
|
+
var ZodPipeline = class _ZodPipeline extends ZodType {
|
|
3854
|
+
_parse(input) {
|
|
3855
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3856
|
+
if (ctx.common.async) {
|
|
3857
|
+
const handleAsync = async () => {
|
|
3858
|
+
const inResult = await this._def.in._parseAsync({
|
|
3859
|
+
data: ctx.data,
|
|
3860
|
+
path: ctx.path,
|
|
3861
|
+
parent: ctx
|
|
3862
|
+
});
|
|
3863
|
+
if (inResult.status === "aborted")
|
|
3864
|
+
return INVALID;
|
|
3865
|
+
if (inResult.status === "dirty") {
|
|
3866
|
+
status.dirty();
|
|
3867
|
+
return DIRTY(inResult.value);
|
|
3868
|
+
} else {
|
|
3869
|
+
return this._def.out._parseAsync({
|
|
3870
|
+
data: inResult.value,
|
|
3871
|
+
path: ctx.path,
|
|
3872
|
+
parent: ctx
|
|
3873
|
+
});
|
|
3874
|
+
}
|
|
3875
|
+
};
|
|
3876
|
+
return handleAsync();
|
|
3877
|
+
} else {
|
|
3878
|
+
const inResult = this._def.in._parseSync({
|
|
3879
|
+
data: ctx.data,
|
|
3880
|
+
path: ctx.path,
|
|
3881
|
+
parent: ctx
|
|
3882
|
+
});
|
|
3883
|
+
if (inResult.status === "aborted")
|
|
3884
|
+
return INVALID;
|
|
3885
|
+
if (inResult.status === "dirty") {
|
|
3886
|
+
status.dirty();
|
|
3887
|
+
return {
|
|
3888
|
+
status: "dirty",
|
|
3889
|
+
value: inResult.value
|
|
3890
|
+
};
|
|
3891
|
+
} else {
|
|
3892
|
+
return this._def.out._parseSync({
|
|
3893
|
+
data: inResult.value,
|
|
3894
|
+
path: ctx.path,
|
|
3895
|
+
parent: ctx
|
|
3896
|
+
});
|
|
3897
|
+
}
|
|
3898
|
+
}
|
|
3899
|
+
}
|
|
3900
|
+
static create(a, b) {
|
|
3901
|
+
return new _ZodPipeline({
|
|
3902
|
+
in: a,
|
|
3903
|
+
out: b,
|
|
3904
|
+
typeName: ZodFirstPartyTypeKind.ZodPipeline
|
|
3905
|
+
});
|
|
3906
|
+
}
|
|
3907
|
+
};
|
|
3908
|
+
var ZodReadonly = class extends ZodType {
|
|
3909
|
+
_parse(input) {
|
|
3910
|
+
const result = this._def.innerType._parse(input);
|
|
3911
|
+
const freeze = (data) => {
|
|
3912
|
+
if (isValid(data)) {
|
|
3913
|
+
data.value = Object.freeze(data.value);
|
|
3914
|
+
}
|
|
3915
|
+
return data;
|
|
3916
|
+
};
|
|
3917
|
+
return isAsync(result) ? result.then((data) => freeze(data)) : freeze(result);
|
|
3918
|
+
}
|
|
3919
|
+
unwrap() {
|
|
3920
|
+
return this._def.innerType;
|
|
3921
|
+
}
|
|
3922
|
+
};
|
|
3923
|
+
ZodReadonly.create = (type, params) => {
|
|
3924
|
+
return new ZodReadonly({
|
|
3925
|
+
innerType: type,
|
|
3926
|
+
typeName: ZodFirstPartyTypeKind.ZodReadonly,
|
|
3927
|
+
...processCreateParams(params)
|
|
3928
|
+
});
|
|
3929
|
+
};
|
|
3930
|
+
function cleanParams(params, data) {
|
|
3931
|
+
const p = typeof params === "function" ? params(data) : typeof params === "string" ? { message: params } : params;
|
|
3932
|
+
const p2 = typeof p === "string" ? { message: p } : p;
|
|
3933
|
+
return p2;
|
|
3934
|
+
}
|
|
3935
|
+
function custom(check, _params = {}, fatal) {
|
|
3936
|
+
if (check)
|
|
3937
|
+
return ZodAny.create().superRefine((data, ctx) => {
|
|
3938
|
+
const r = check(data);
|
|
3939
|
+
if (r instanceof Promise) {
|
|
3940
|
+
return r.then((r2) => {
|
|
3941
|
+
if (!r2) {
|
|
3942
|
+
const params = cleanParams(_params, data);
|
|
3943
|
+
const _fatal = params.fatal ?? fatal ?? true;
|
|
3944
|
+
ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
|
|
3945
|
+
}
|
|
3946
|
+
});
|
|
3947
|
+
}
|
|
3948
|
+
if (!r) {
|
|
3949
|
+
const params = cleanParams(_params, data);
|
|
3950
|
+
const _fatal = params.fatal ?? fatal ?? true;
|
|
3951
|
+
ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
|
|
3952
|
+
}
|
|
3953
|
+
return;
|
|
3954
|
+
});
|
|
3955
|
+
return ZodAny.create();
|
|
3956
|
+
}
|
|
3957
|
+
var late = {
|
|
3958
|
+
object: ZodObject.lazycreate
|
|
3959
|
+
};
|
|
3960
|
+
var ZodFirstPartyTypeKind;
|
|
3961
|
+
(function(ZodFirstPartyTypeKind2) {
|
|
3962
|
+
ZodFirstPartyTypeKind2["ZodString"] = "ZodString";
|
|
3963
|
+
ZodFirstPartyTypeKind2["ZodNumber"] = "ZodNumber";
|
|
3964
|
+
ZodFirstPartyTypeKind2["ZodNaN"] = "ZodNaN";
|
|
3965
|
+
ZodFirstPartyTypeKind2["ZodBigInt"] = "ZodBigInt";
|
|
3966
|
+
ZodFirstPartyTypeKind2["ZodBoolean"] = "ZodBoolean";
|
|
3967
|
+
ZodFirstPartyTypeKind2["ZodDate"] = "ZodDate";
|
|
3968
|
+
ZodFirstPartyTypeKind2["ZodSymbol"] = "ZodSymbol";
|
|
3969
|
+
ZodFirstPartyTypeKind2["ZodUndefined"] = "ZodUndefined";
|
|
3970
|
+
ZodFirstPartyTypeKind2["ZodNull"] = "ZodNull";
|
|
3971
|
+
ZodFirstPartyTypeKind2["ZodAny"] = "ZodAny";
|
|
3972
|
+
ZodFirstPartyTypeKind2["ZodUnknown"] = "ZodUnknown";
|
|
3973
|
+
ZodFirstPartyTypeKind2["ZodNever"] = "ZodNever";
|
|
3974
|
+
ZodFirstPartyTypeKind2["ZodVoid"] = "ZodVoid";
|
|
3975
|
+
ZodFirstPartyTypeKind2["ZodArray"] = "ZodArray";
|
|
3976
|
+
ZodFirstPartyTypeKind2["ZodObject"] = "ZodObject";
|
|
3977
|
+
ZodFirstPartyTypeKind2["ZodUnion"] = "ZodUnion";
|
|
3978
|
+
ZodFirstPartyTypeKind2["ZodDiscriminatedUnion"] = "ZodDiscriminatedUnion";
|
|
3979
|
+
ZodFirstPartyTypeKind2["ZodIntersection"] = "ZodIntersection";
|
|
3980
|
+
ZodFirstPartyTypeKind2["ZodTuple"] = "ZodTuple";
|
|
3981
|
+
ZodFirstPartyTypeKind2["ZodRecord"] = "ZodRecord";
|
|
3982
|
+
ZodFirstPartyTypeKind2["ZodMap"] = "ZodMap";
|
|
3983
|
+
ZodFirstPartyTypeKind2["ZodSet"] = "ZodSet";
|
|
3984
|
+
ZodFirstPartyTypeKind2["ZodFunction"] = "ZodFunction";
|
|
3985
|
+
ZodFirstPartyTypeKind2["ZodLazy"] = "ZodLazy";
|
|
3986
|
+
ZodFirstPartyTypeKind2["ZodLiteral"] = "ZodLiteral";
|
|
3987
|
+
ZodFirstPartyTypeKind2["ZodEnum"] = "ZodEnum";
|
|
3988
|
+
ZodFirstPartyTypeKind2["ZodEffects"] = "ZodEffects";
|
|
3989
|
+
ZodFirstPartyTypeKind2["ZodNativeEnum"] = "ZodNativeEnum";
|
|
3990
|
+
ZodFirstPartyTypeKind2["ZodOptional"] = "ZodOptional";
|
|
3991
|
+
ZodFirstPartyTypeKind2["ZodNullable"] = "ZodNullable";
|
|
3992
|
+
ZodFirstPartyTypeKind2["ZodDefault"] = "ZodDefault";
|
|
3993
|
+
ZodFirstPartyTypeKind2["ZodCatch"] = "ZodCatch";
|
|
3994
|
+
ZodFirstPartyTypeKind2["ZodPromise"] = "ZodPromise";
|
|
3995
|
+
ZodFirstPartyTypeKind2["ZodBranded"] = "ZodBranded";
|
|
3996
|
+
ZodFirstPartyTypeKind2["ZodPipeline"] = "ZodPipeline";
|
|
3997
|
+
ZodFirstPartyTypeKind2["ZodReadonly"] = "ZodReadonly";
|
|
3998
|
+
})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
|
|
3999
|
+
var instanceOfType = (cls, params = {
|
|
4000
|
+
message: `Input not instance of ${cls.name}`
|
|
4001
|
+
}) => custom((data) => data instanceof cls, params);
|
|
4002
|
+
var stringType = ZodString.create;
|
|
4003
|
+
var numberType = ZodNumber.create;
|
|
4004
|
+
var nanType = ZodNaN.create;
|
|
4005
|
+
var bigIntType = ZodBigInt.create;
|
|
4006
|
+
var booleanType = ZodBoolean.create;
|
|
4007
|
+
var dateType = ZodDate.create;
|
|
4008
|
+
var symbolType = ZodSymbol.create;
|
|
4009
|
+
var undefinedType = ZodUndefined.create;
|
|
4010
|
+
var nullType = ZodNull.create;
|
|
4011
|
+
var anyType = ZodAny.create;
|
|
4012
|
+
var unknownType = ZodUnknown.create;
|
|
4013
|
+
var neverType = ZodNever.create;
|
|
4014
|
+
var voidType = ZodVoid.create;
|
|
4015
|
+
var arrayType = ZodArray.create;
|
|
4016
|
+
var objectType = ZodObject.create;
|
|
4017
|
+
var strictObjectType = ZodObject.strictCreate;
|
|
4018
|
+
var unionType = ZodUnion.create;
|
|
4019
|
+
var discriminatedUnionType = ZodDiscriminatedUnion.create;
|
|
4020
|
+
var intersectionType = ZodIntersection.create;
|
|
4021
|
+
var tupleType = ZodTuple.create;
|
|
4022
|
+
var recordType = ZodRecord.create;
|
|
4023
|
+
var mapType = ZodMap.create;
|
|
4024
|
+
var setType = ZodSet.create;
|
|
4025
|
+
var functionType = ZodFunction.create;
|
|
4026
|
+
var lazyType = ZodLazy.create;
|
|
4027
|
+
var literalType = ZodLiteral.create;
|
|
4028
|
+
var enumType = ZodEnum.create;
|
|
4029
|
+
var nativeEnumType = ZodNativeEnum.create;
|
|
4030
|
+
var promiseType = ZodPromise.create;
|
|
4031
|
+
var effectsType = ZodEffects.create;
|
|
4032
|
+
var optionalType = ZodOptional.create;
|
|
4033
|
+
var nullableType = ZodNullable.create;
|
|
4034
|
+
var preprocessType = ZodEffects.createWithPreprocess;
|
|
4035
|
+
var pipelineType = ZodPipeline.create;
|
|
4036
|
+
var ostring = () => stringType().optional();
|
|
4037
|
+
var onumber = () => numberType().optional();
|
|
4038
|
+
var oboolean = () => booleanType().optional();
|
|
4039
|
+
var coerce = {
|
|
4040
|
+
string: ((arg) => ZodString.create({ ...arg, coerce: true })),
|
|
4041
|
+
number: ((arg) => ZodNumber.create({ ...arg, coerce: true })),
|
|
4042
|
+
boolean: ((arg) => ZodBoolean.create({
|
|
4043
|
+
...arg,
|
|
4044
|
+
coerce: true
|
|
4045
|
+
})),
|
|
4046
|
+
bigint: ((arg) => ZodBigInt.create({ ...arg, coerce: true })),
|
|
4047
|
+
date: ((arg) => ZodDate.create({ ...arg, coerce: true }))
|
|
4048
|
+
};
|
|
4049
|
+
var NEVER = INVALID;
|
|
4050
|
+
|
|
4051
|
+
// src/core/OIMDevBridgeMessageReader.ts
|
|
4052
|
+
var _OIMDevBridgeMessageReader = class _OIMDevBridgeMessageReader {
|
|
4053
|
+
read(raw) {
|
|
4054
|
+
let parsed;
|
|
4055
|
+
try {
|
|
4056
|
+
parsed = JSON.parse(raw);
|
|
4057
|
+
} catch {
|
|
4058
|
+
return null;
|
|
4059
|
+
}
|
|
4060
|
+
const result = _OIMDevBridgeMessageReader.SCHEMA.safeParse(parsed);
|
|
4061
|
+
if (!result.success) return null;
|
|
4062
|
+
return result.data;
|
|
4063
|
+
}
|
|
4064
|
+
};
|
|
4065
|
+
_OIMDevBridgeMessageReader.SCHEMA = external_exports.discriminatedUnion("type", [
|
|
4066
|
+
external_exports.object({
|
|
4067
|
+
type: external_exports.literal("hello"),
|
|
4068
|
+
url: external_exports.string(),
|
|
4069
|
+
title: external_exports.string(),
|
|
4070
|
+
timestamp: external_exports.number()
|
|
4071
|
+
}),
|
|
4072
|
+
external_exports.object({
|
|
4073
|
+
type: external_exports.literal("response"),
|
|
4074
|
+
id: external_exports.string(),
|
|
4075
|
+
data: external_exports.unknown(),
|
|
4076
|
+
error: external_exports.string().optional()
|
|
4077
|
+
})
|
|
4078
|
+
]);
|
|
4079
|
+
var OIMDevBridgeMessageReader = _OIMDevBridgeMessageReader;
|
|
4080
|
+
|
|
4081
|
+
// src/core/OIMDevLoopbackPolicy.ts
|
|
4082
|
+
var _OIMDevLoopbackPolicy = class _OIMDevLoopbackPolicy {
|
|
4083
|
+
static isLoopbackHost(host) {
|
|
4084
|
+
return _OIMDevLoopbackPolicy.HOSTS.has(host) || host.endsWith(".localhost");
|
|
4085
|
+
}
|
|
4086
|
+
static isLoopbackAuthority(authority) {
|
|
4087
|
+
const host = authority.startsWith("[") ? authority.slice(0, authority.indexOf("]") + 1) : authority.split(":", 1)[0];
|
|
4088
|
+
return _OIMDevLoopbackPolicy.isLoopbackHost(host);
|
|
4089
|
+
}
|
|
4090
|
+
// The literal string `null` is NOT loopback: a sandboxed iframe on any site
|
|
4091
|
+
// in the world sends exactly that.
|
|
4092
|
+
static isLoopbackOrigin(origin) {
|
|
4093
|
+
try {
|
|
4094
|
+
return _OIMDevLoopbackPolicy.isLoopbackHost(new URL(origin).hostname);
|
|
4095
|
+
} catch {
|
|
4096
|
+
return false;
|
|
4097
|
+
}
|
|
4098
|
+
}
|
|
4099
|
+
};
|
|
4100
|
+
_OIMDevLoopbackPolicy.HOSTS = /* @__PURE__ */ new Set([
|
|
4101
|
+
"localhost",
|
|
4102
|
+
"127.0.0.1",
|
|
4103
|
+
"::1",
|
|
4104
|
+
"[::1]"
|
|
4105
|
+
]);
|
|
4106
|
+
var OIMDevLoopbackPolicy = _OIMDevLoopbackPolicy;
|
|
4107
|
+
|
|
4108
|
+
// src/core/OIMDevBridge.ts
|
|
4109
|
+
var OIM_REQUEST_TIMEOUT_MS = 5e3;
|
|
4110
|
+
var OIMDevBridge = class {
|
|
4111
|
+
constructor(bridgeOptions) {
|
|
4112
|
+
this.bridgeOptions = bridgeOptions;
|
|
4113
|
+
this.messageReader = new OIMDevBridgeMessageReader();
|
|
4114
|
+
this.pending = /* @__PURE__ */ new Map();
|
|
4115
|
+
this.webSocketServer = null;
|
|
4116
|
+
this.devConnectedBrowser = null;
|
|
4117
|
+
}
|
|
4118
|
+
get connectedBrowser() {
|
|
4119
|
+
return this.devConnectedBrowser;
|
|
4120
|
+
}
|
|
4121
|
+
/**
|
|
4122
|
+
* May a page use this bridge?
|
|
4123
|
+
*
|
|
4124
|
+
* From this machine, or named by whoever started the server. An ABSENT
|
|
4125
|
+
* origin is a non-browser client — a CLI, a test — and is allowed; the
|
|
4126
|
+
* literal `'null'` is not, because a sandboxed iframe on any site can send
|
|
4127
|
+
* exactly that.
|
|
4128
|
+
*/
|
|
4129
|
+
isOriginAllowed(origin) {
|
|
4130
|
+
if (origin === void 0 || origin === "") return true;
|
|
4131
|
+
if (this.bridgeOptions.allowedOrigins.has(origin)) return true;
|
|
4132
|
+
return OIMDevLoopbackPolicy.isLoopbackOrigin(origin);
|
|
4133
|
+
}
|
|
4134
|
+
request(action) {
|
|
4135
|
+
const browser = this.devConnectedBrowser;
|
|
4136
|
+
if (browser === null) {
|
|
4137
|
+
return Promise.reject(
|
|
4138
|
+
new Error(this.bridgeOptions.requestTimeoutMessage)
|
|
4139
|
+
);
|
|
4140
|
+
}
|
|
4141
|
+
return new Promise((resolve, reject) => {
|
|
4142
|
+
const id = Math.random().toString(36).slice(2);
|
|
4143
|
+
const timer = setTimeout(() => {
|
|
4144
|
+
this.pending.delete(id);
|
|
4145
|
+
reject(new Error(this.bridgeOptions.requestTimeoutMessage));
|
|
4146
|
+
}, OIM_REQUEST_TIMEOUT_MS);
|
|
4147
|
+
this.pending.set(id, { resolve, reject, timer });
|
|
4148
|
+
browser.ws.send(JSON.stringify({ type: "request", id, action }));
|
|
4149
|
+
});
|
|
4150
|
+
}
|
|
4151
|
+
start() {
|
|
4152
|
+
this.webSocketServer = new WebSocketServer({
|
|
4153
|
+
port: this.bridgeOptions.wsPort,
|
|
4154
|
+
host: this.bridgeOptions.host,
|
|
4155
|
+
verifyClient: (info) => this.isOriginAllowed(info.origin)
|
|
4156
|
+
});
|
|
4157
|
+
this.webSocketServer.on("error", (err) => {
|
|
4158
|
+
if (err.code === "EADDRINUSE") {
|
|
4159
|
+
this.write(
|
|
4160
|
+
`Port ${this.bridgeOptions.wsPort} is already in use.
|
|
4161
|
+
`
|
|
4162
|
+
);
|
|
4163
|
+
process.stderr.write(
|
|
4164
|
+
`Set a different port: OIMDB_WS_PORT=7433 ${this.bridgeOptions.portInUseHint}
|
|
4165
|
+
`
|
|
4166
|
+
);
|
|
4167
|
+
process.exit(1);
|
|
4168
|
+
}
|
|
4169
|
+
this.write(`Bridge error: ${err.message}
|
|
4170
|
+
`);
|
|
4171
|
+
process.exit(1);
|
|
4172
|
+
});
|
|
4173
|
+
this.webSocketServer.on("connection", (ws) => {
|
|
4174
|
+
ws.on("error", (err) => {
|
|
4175
|
+
this.write(`Bridge socket error: ${err.message}
|
|
4176
|
+
`);
|
|
4177
|
+
});
|
|
4178
|
+
ws.on("message", (raw) => this.readFrame(ws, raw.toString()));
|
|
4179
|
+
ws.on("close", () => {
|
|
4180
|
+
if (this.devConnectedBrowser?.ws === ws) {
|
|
4181
|
+
this.devConnectedBrowser = null;
|
|
4182
|
+
this.write("Browser disconnected\n");
|
|
4183
|
+
}
|
|
4184
|
+
});
|
|
4185
|
+
});
|
|
4186
|
+
this.write(
|
|
4187
|
+
`Bridge ready on ws://localhost:${this.bridgeOptions.wsPort} (bound to ${this.bridgeOptions.host})
|
|
4188
|
+
`
|
|
4189
|
+
);
|
|
4190
|
+
}
|
|
4191
|
+
readFrame(ws, raw) {
|
|
4192
|
+
const devBridgeMessage = this.messageReader.read(raw);
|
|
4193
|
+
if (devBridgeMessage === null) {
|
|
4194
|
+
this.write("Dropping a client that sent a malformed frame\n");
|
|
4195
|
+
ws.close(1007, "Malformed bridge message");
|
|
4196
|
+
return;
|
|
4197
|
+
}
|
|
4198
|
+
if (devBridgeMessage.type === "hello") {
|
|
4199
|
+
this.adoptBrowser(ws, devBridgeMessage.url, devBridgeMessage.title);
|
|
4200
|
+
return;
|
|
4201
|
+
}
|
|
4202
|
+
if (this.devConnectedBrowser?.ws !== ws) return;
|
|
4203
|
+
const devPendingRequest = this.pending.get(devBridgeMessage.id);
|
|
4204
|
+
if (!devPendingRequest) return;
|
|
4205
|
+
this.pending.delete(devBridgeMessage.id);
|
|
4206
|
+
clearTimeout(devPendingRequest.timer);
|
|
4207
|
+
if (devBridgeMessage.error) devPendingRequest.reject(new Error(devBridgeMessage.error));
|
|
4208
|
+
else devPendingRequest.resolve(devBridgeMessage.data);
|
|
4209
|
+
}
|
|
4210
|
+
/**
|
|
4211
|
+
* Newest tab wins — a reload is the ordinary reason for a second `hello` —
|
|
4212
|
+
* but the tab it displaces is CLOSED and the swap is printed. Overwriting the
|
|
4213
|
+
* field in place leaves the displaced socket open for ever and lets a second
|
|
4214
|
+
* connection take the bridge over in silence.
|
|
4215
|
+
*/
|
|
4216
|
+
adoptBrowser(ws, url, title) {
|
|
4217
|
+
const previousDevConnectedBrowser = this.devConnectedBrowser;
|
|
4218
|
+
this.devConnectedBrowser = { ws, url, title };
|
|
4219
|
+
if (previousDevConnectedBrowser !== null && previousDevConnectedBrowser.ws !== ws) {
|
|
4220
|
+
this.write(
|
|
4221
|
+
`Bridge taken over by "${title}" (${url}); dropping "${previousDevConnectedBrowser.title}"
|
|
4222
|
+
`
|
|
4223
|
+
);
|
|
4224
|
+
previousDevConnectedBrowser.ws.close(1e3, "Replaced by a newer OIMDB DevTools tab");
|
|
4225
|
+
return;
|
|
4226
|
+
}
|
|
4227
|
+
this.write(`Browser connected: ${title} (${url})
|
|
4228
|
+
`);
|
|
4229
|
+
}
|
|
4230
|
+
write(line) {
|
|
4231
|
+
process.stderr.write(`${this.bridgeOptions.logPrefix} ${line}`);
|
|
4232
|
+
}
|
|
4233
|
+
};
|
|
4234
|
+
|
|
4235
|
+
// src/core/OIMDevUIServer.ts
|
|
4236
|
+
import { createServer } from "http";
|
|
4237
|
+
import { exec } from "child_process";
|
|
4238
|
+
|
|
4239
|
+
// src/core/OIMDevUIAssetCatalog.ts
|
|
4240
|
+
import { readFileSync } from "fs";
|
|
4241
|
+
import { join } from "path";
|
|
4242
|
+
var _OIMDevUIAssetCatalog = class _OIMDevUIAssetCatalog {
|
|
4243
|
+
constructor(uiDir) {
|
|
4244
|
+
this.uiDir = uiDir;
|
|
4245
|
+
}
|
|
4246
|
+
resolve(requestUrl) {
|
|
4247
|
+
const path = requestUrl.split("?", 1)[0].split("#", 1)[0];
|
|
4248
|
+
return _OIMDevUIAssetCatalog.ASSETS.get(path) ?? null;
|
|
4249
|
+
}
|
|
4250
|
+
// Per request, so the UI stays editable; the constant file name is what
|
|
4251
|
+
// makes that safe, not the timing.
|
|
4252
|
+
read(asset) {
|
|
4253
|
+
return readFileSync(join(this.uiDir, asset.fileName));
|
|
4254
|
+
}
|
|
4255
|
+
};
|
|
4256
|
+
_OIMDevUIAssetCatalog.ASSETS = /* @__PURE__ */ new Map([
|
|
4257
|
+
["/", { fileName: "index.html", contentType: "text/html; charset=utf-8" }],
|
|
4258
|
+
["/index.html", { fileName: "index.html", contentType: "text/html; charset=utf-8" }],
|
|
4259
|
+
["/app.js", { fileName: "app.js", contentType: "text/javascript; charset=utf-8" }],
|
|
4260
|
+
["/app.css", { fileName: "app.css", contentType: "text/css; charset=utf-8" }]
|
|
4261
|
+
]);
|
|
4262
|
+
var OIMDevUIAssetCatalog = _OIMDevUIAssetCatalog;
|
|
4263
|
+
|
|
4264
|
+
// src/core/OIMDevUIServer.ts
|
|
4265
|
+
var OIMDevUIServer = class {
|
|
4266
|
+
constructor(options) {
|
|
4267
|
+
this.uiPort = options.uiPort ?? 7433;
|
|
4268
|
+
this.shouldOpen = options.open ?? true;
|
|
4269
|
+
this.host = options.host ?? "127.0.0.1";
|
|
4270
|
+
this.allowedOrigins = new Set(options.allowedOrigins ?? []);
|
|
4271
|
+
this.devUIAssetCatalog = new OIMDevUIAssetCatalog(options.uiDir);
|
|
4272
|
+
this.bridge = new OIMDevBridge({
|
|
4273
|
+
wsPort: options.wsPort ?? 7432,
|
|
4274
|
+
host: this.host,
|
|
4275
|
+
allowedOrigins: this.allowedOrigins,
|
|
4276
|
+
logPrefix: "[OIMDB DevTools]",
|
|
4277
|
+
portInUseHint: "npx @oimdb/cli serve",
|
|
4278
|
+
requestTimeoutMessage: "timeout"
|
|
4279
|
+
});
|
|
4280
|
+
}
|
|
4281
|
+
/**
|
|
4282
|
+
* While bound to loopback, answer only what was addressed to a loopback
|
|
4283
|
+
* name.
|
|
4284
|
+
*
|
|
4285
|
+
* Without this, a page on any site can point a hostname it controls at
|
|
4286
|
+
* 127.0.0.1 and become same-origin with this server. The `Host` header is
|
|
4287
|
+
* the part of that a browser cannot forge. Binding elsewhere is an explicit
|
|
4288
|
+
* decision, so the check stands down there.
|
|
4289
|
+
*/
|
|
4290
|
+
isRequestAddressedHere(req) {
|
|
4291
|
+
if (!OIMDevLoopbackPolicy.isLoopbackHost(this.host)) return true;
|
|
4292
|
+
const host = req.headers.host;
|
|
4293
|
+
return host !== void 0 && OIMDevLoopbackPolicy.isLoopbackAuthority(host);
|
|
4294
|
+
}
|
|
4295
|
+
requestBrowser(action) {
|
|
4296
|
+
if (this.bridge.connectedBrowser === null) return Promise.resolve(null);
|
|
4297
|
+
return this.bridge.request(action);
|
|
4298
|
+
}
|
|
4299
|
+
setupHTTP() {
|
|
4300
|
+
const server = createServer((req, res) => {
|
|
4301
|
+
if (!this.isRequestAddressedHere(req)) {
|
|
4302
|
+
res.writeHead(421);
|
|
4303
|
+
res.end("Misdirected request");
|
|
4304
|
+
return;
|
|
4305
|
+
}
|
|
4306
|
+
const origin = req.headers.origin;
|
|
4307
|
+
if (origin !== void 0 && this.allowedOrigins.has(origin)) {
|
|
4308
|
+
res.setHeader("Access-Control-Allow-Origin", origin);
|
|
4309
|
+
res.setHeader("Vary", "Origin");
|
|
4310
|
+
}
|
|
4311
|
+
const url = req.url ?? "/";
|
|
4312
|
+
if (url.split("?", 1)[0] === "/api/inspect") {
|
|
4313
|
+
this.requestBrowser("inspect").then((data) => {
|
|
4314
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
4315
|
+
const connectedBrowser = this.bridge.connectedBrowser;
|
|
4316
|
+
res.end(JSON.stringify({
|
|
4317
|
+
connected: connectedBrowser !== null,
|
|
4318
|
+
tab: connectedBrowser === null ? null : { url: connectedBrowser.url, title: connectedBrowser.title },
|
|
4319
|
+
data
|
|
4320
|
+
}));
|
|
4321
|
+
}).catch(() => {
|
|
4322
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
4323
|
+
res.end(JSON.stringify({ connected: false, tab: null, data: null }));
|
|
4324
|
+
});
|
|
4325
|
+
return;
|
|
4326
|
+
}
|
|
4327
|
+
if (url.split("?", 1)[0] === "/api/dump") {
|
|
4328
|
+
this.requestBrowser("dump").then((data) => {
|
|
4329
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
4330
|
+
res.end(JSON.stringify({ connected: this.bridge.connectedBrowser !== null, data }));
|
|
4331
|
+
}).catch(() => {
|
|
4332
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
4333
|
+
res.end(JSON.stringify({ connected: false, data: null }));
|
|
4334
|
+
});
|
|
4335
|
+
return;
|
|
4336
|
+
}
|
|
4337
|
+
const asset = this.devUIAssetCatalog.resolve(url);
|
|
4338
|
+
if (asset === null) {
|
|
4339
|
+
res.writeHead(404);
|
|
4340
|
+
res.end("Not found");
|
|
4341
|
+
return;
|
|
4342
|
+
}
|
|
4343
|
+
try {
|
|
4344
|
+
res.writeHead(200, { "Content-Type": asset.contentType });
|
|
4345
|
+
res.end(this.devUIAssetCatalog.read(asset));
|
|
4346
|
+
} catch {
|
|
4347
|
+
res.writeHead(404);
|
|
4348
|
+
res.end("Not found");
|
|
4349
|
+
}
|
|
4350
|
+
});
|
|
4351
|
+
server.on("error", (err) => {
|
|
4352
|
+
if (err.code === "EADDRINUSE") {
|
|
4353
|
+
process.stderr.write(
|
|
4354
|
+
`[OIMDB DevTools] UI port ${this.uiPort} is already in use.
|
|
4355
|
+
Set a different port: OIMDB_UI_PORT=7434 npx @oimdb/cli serve
|
|
4356
|
+
`
|
|
4357
|
+
);
|
|
4358
|
+
process.exit(1);
|
|
4359
|
+
}
|
|
4360
|
+
});
|
|
4361
|
+
server.listen(this.uiPort, this.host, () => {
|
|
4362
|
+
const uiUrl = `http://localhost:${this.uiPort}`;
|
|
4363
|
+
process.stderr.write(`[OIMDB DevTools] UI ready \u2192 ${uiUrl} (bound to ${this.host})
|
|
4364
|
+
`);
|
|
4365
|
+
if (this.shouldOpen) this.openURL(uiUrl);
|
|
4366
|
+
});
|
|
4367
|
+
}
|
|
4368
|
+
openURL(url) {
|
|
4369
|
+
const cmd = process.platform === "win32" ? `start "" "${url}"` : process.platform === "darwin" ? `open "${url}"` : `xdg-open "${url}"`;
|
|
4370
|
+
exec(cmd, (err) => {
|
|
4371
|
+
if (err) process.stderr.write(`[OIMDB DevTools] Could not open browser: ${err.message}
|
|
4372
|
+
`);
|
|
4373
|
+
});
|
|
4374
|
+
}
|
|
4375
|
+
start() {
|
|
4376
|
+
this.bridge.start();
|
|
4377
|
+
this.setupHTTP();
|
|
4378
|
+
process.stderr.write(
|
|
4379
|
+
`[OIMDB DevTools] Add to debug.ts: oimDevRegistry.connect();
|
|
4380
|
+
`
|
|
4381
|
+
);
|
|
4382
|
+
}
|
|
4383
|
+
};
|
|
4384
|
+
|
|
4385
|
+
// src/core/OIMDevStateReader.ts
|
|
4386
|
+
var OIMDevStateReader = class {
|
|
4387
|
+
constructor(devStateSource) {
|
|
4388
|
+
this.devStateSource = devStateSource;
|
|
4389
|
+
}
|
|
4390
|
+
async readInspectText() {
|
|
4391
|
+
return JSON.stringify(await this.devStateSource.readInspect(), null, 2);
|
|
4392
|
+
}
|
|
4393
|
+
readDumpText() {
|
|
4394
|
+
return this.devStateSource.readDump();
|
|
4395
|
+
}
|
|
4396
|
+
async readCollectionText(name) {
|
|
4397
|
+
const { collections } = await this.devStateSource.readInspect();
|
|
4398
|
+
const collection = collections[name];
|
|
4399
|
+
if (collection === void 0) return `Collection '${name}' not found. Registered: ${this.namesOf(collections)}`;
|
|
4400
|
+
return JSON.stringify(collection, null, 2);
|
|
4401
|
+
}
|
|
4402
|
+
async readComputedText(name) {
|
|
4403
|
+
const { computeds } = await this.devStateSource.readInspect();
|
|
4404
|
+
const computed = computeds[name];
|
|
4405
|
+
if (computed === void 0) return `Computed '${name}' not found. Registered: ${this.namesOf(computeds)}`;
|
|
4406
|
+
return JSON.stringify(computed, null, 2);
|
|
4407
|
+
}
|
|
4408
|
+
namesOf(entries) {
|
|
4409
|
+
return Object.keys(entries).join(", ") || "(none registered)";
|
|
4410
|
+
}
|
|
4411
|
+
};
|
|
4412
|
+
|
|
4413
|
+
// src/core/OIMDevRegistryStateSource.ts
|
|
4414
|
+
var OIMDevRegistryStateSource = class {
|
|
4415
|
+
constructor(devRegistry) {
|
|
4416
|
+
this.devRegistry = devRegistry;
|
|
4417
|
+
}
|
|
4418
|
+
readInspect() {
|
|
4419
|
+
return Promise.resolve(this.devRegistry.inspect());
|
|
4420
|
+
}
|
|
4421
|
+
readDump() {
|
|
4422
|
+
return Promise.resolve(this.devRegistry.dumpString());
|
|
4423
|
+
}
|
|
4424
|
+
};
|
|
4425
|
+
|
|
4426
|
+
// src/core/OIMDevModuleStateSource.ts
|
|
4427
|
+
import { pathToFileURL } from "url";
|
|
4428
|
+
import { resolve as resolvePath } from "path";
|
|
4429
|
+
var OIMDevModuleStateSource = class {
|
|
4430
|
+
constructor(modelModulePath) {
|
|
4431
|
+
this.modelModulePath = modelModulePath;
|
|
4432
|
+
this.devRegistry = void 0;
|
|
4433
|
+
}
|
|
4434
|
+
async readInspect() {
|
|
4435
|
+
return (await this.loadDevRegistry()).inspect();
|
|
4436
|
+
}
|
|
4437
|
+
async readDump() {
|
|
4438
|
+
return (await this.loadDevRegistry()).dumpString();
|
|
4439
|
+
}
|
|
4440
|
+
loadDevRegistry() {
|
|
4441
|
+
this.devRegistry ?? (this.devRegistry = this.importDevRegistry());
|
|
4442
|
+
return this.devRegistry;
|
|
4443
|
+
}
|
|
4444
|
+
async importDevRegistry() {
|
|
4445
|
+
const url = pathToFileURL(resolvePath(this.modelModulePath)).href;
|
|
4446
|
+
const exported = await import(url);
|
|
4447
|
+
let candidate = exported.oimDevRegistry ?? exported.default ?? exported;
|
|
4448
|
+
if (typeof candidate === "function") candidate = await candidate();
|
|
4449
|
+
const devRegistry = candidate;
|
|
4450
|
+
if (devRegistry === void 0 || typeof devRegistry.inspect !== "function" || typeof devRegistry.dumpString !== "function") {
|
|
4451
|
+
throw new Error(
|
|
4452
|
+
`[OIMDB] Module "${this.modelModulePath}" must export a registry exposing inspect() and dumpString() (as an "oimDevRegistry" or default export, or a zero-arg factory returning one).`
|
|
4453
|
+
);
|
|
4454
|
+
}
|
|
4455
|
+
return devRegistry;
|
|
4456
|
+
}
|
|
4457
|
+
};
|
|
4458
|
+
|
|
4459
|
+
// src/core/OIMDevBridgeStateSource.ts
|
|
4460
|
+
var OIMDevBridgeStateSource = class {
|
|
4461
|
+
constructor(devBridge, wsPort) {
|
|
4462
|
+
this.devBridge = devBridge;
|
|
4463
|
+
this.wsPort = wsPort;
|
|
4464
|
+
}
|
|
4465
|
+
readInspect() {
|
|
4466
|
+
return this.requestBrowser("inspect");
|
|
4467
|
+
}
|
|
4468
|
+
async readDump() {
|
|
4469
|
+
return await this.requestBrowser("dump");
|
|
4470
|
+
}
|
|
4471
|
+
requestBrowser(action) {
|
|
4472
|
+
if (this.devBridge.connectedBrowser === null) {
|
|
4473
|
+
return Promise.reject(new Error(
|
|
4474
|
+
`No browser tab connected to the OIMDB bridge.
|
|
4475
|
+
|
|
4476
|
+
Live mode \u2014 add to your debug entry point:
|
|
4477
|
+
import { oimDevRegistry } from '@oimdb/devtools';
|
|
4478
|
+
oimDevRegistry.connect(); // connects to ws://localhost:${this.wsPort}
|
|
4479
|
+
Then reload the page.
|
|
4480
|
+
|
|
4481
|
+
Offline mode \u2014 point at a model module instead (it exports an "oimDevRegistry" with inspect()/dumpString()):
|
|
4482
|
+
oimdb inspect --model ./path/to/model.js
|
|
4483
|
+
OIMDB_MODEL_MODULE=./path/to/model.js npx @oimdb/mcp`
|
|
4484
|
+
));
|
|
4485
|
+
}
|
|
4486
|
+
return this.devBridge.request(action);
|
|
4487
|
+
}
|
|
4488
|
+
};
|
|
4489
|
+
|
|
4490
|
+
// src/core/OIMDevHttpStateSource.ts
|
|
4491
|
+
var OIMDevHttpStateSource = class {
|
|
4492
|
+
constructor(serverUrl) {
|
|
4493
|
+
this.serverUrl = serverUrl;
|
|
4494
|
+
}
|
|
4495
|
+
async readInspect() {
|
|
4496
|
+
return (await this.readConnected("/api/inspect")).data;
|
|
4497
|
+
}
|
|
4498
|
+
async readDump() {
|
|
4499
|
+
return (await this.readConnected("/api/dump")).data;
|
|
4500
|
+
}
|
|
4501
|
+
async readConnected(path) {
|
|
4502
|
+
let response;
|
|
4503
|
+
try {
|
|
4504
|
+
response = await fetch(`${this.serverUrl}${path}`);
|
|
4505
|
+
} catch {
|
|
4506
|
+
throw new Error(`[OIMDB] No "oimdb serve" at ${this.serverUrl}. Start it with: npx @oimdb/cli serve`);
|
|
4507
|
+
}
|
|
4508
|
+
const body = await response.json();
|
|
4509
|
+
if (!body.connected) {
|
|
4510
|
+
throw new Error(
|
|
4511
|
+
`[OIMDB] "oimdb serve" at ${this.serverUrl} has no browser tab connected. Add oimDevRegistry.connect() from @oimdb/devtools to the page and reload it.`
|
|
4512
|
+
);
|
|
4513
|
+
}
|
|
4514
|
+
return body;
|
|
4515
|
+
}
|
|
4516
|
+
};
|
|
4517
|
+
|
|
4518
|
+
// src/core/OIMDevCdpStateSource.ts
|
|
4519
|
+
import { WebSocket } from "ws";
|
|
4520
|
+
var OIM_CDP_TIMEOUT_MS = 5e3;
|
|
4521
|
+
var OIMDevCdpStateSource = class {
|
|
4522
|
+
constructor(cdpUrl) {
|
|
4523
|
+
this.cdpUrl = cdpUrl;
|
|
4524
|
+
}
|
|
4525
|
+
readInspect() {
|
|
4526
|
+
return this.evaluate("JSON.parse(JSON.stringify(window.__OIMDB_DEV__.inspect()))");
|
|
4527
|
+
}
|
|
4528
|
+
async readDump() {
|
|
4529
|
+
return await this.evaluate("window.__OIMDB_DEV__.dumpString()");
|
|
4530
|
+
}
|
|
4531
|
+
async evaluate(expression) {
|
|
4532
|
+
let tabs;
|
|
4533
|
+
try {
|
|
4534
|
+
tabs = await (await fetch(`${this.cdpUrl}/json/list`)).json();
|
|
4535
|
+
} catch {
|
|
4536
|
+
throw new Error(`[OIMDB CDP] Cannot reach Chrome at ${this.cdpUrl}. Launch Chrome with --remote-debugging-port=9222.`);
|
|
4537
|
+
}
|
|
4538
|
+
const debuggerUrl = tabs.find((tab) => tab.webSocketDebuggerUrl !== void 0)?.webSocketDebuggerUrl;
|
|
4539
|
+
if (debuggerUrl === void 0) throw new Error("[OIMDB CDP] No debuggable tabs found.");
|
|
4540
|
+
const value = await this.evaluateOnTab(debuggerUrl, `(function(){ if (!window.__OIMDB_DEV__) return null; return ${expression}; })()`);
|
|
4541
|
+
if (value === null || value === void 0) {
|
|
4542
|
+
throw new Error("[OIMDB CDP] __OIMDB_DEV__ not found on this page. Set window.__OIMDB_DEV__ = oimDevRegistry in the debug entry.");
|
|
4543
|
+
}
|
|
4544
|
+
return value;
|
|
4545
|
+
}
|
|
4546
|
+
evaluateOnTab(debuggerUrl, expression) {
|
|
4547
|
+
return new Promise((resolve, reject) => {
|
|
4548
|
+
const webSocket = new WebSocket(debuggerUrl);
|
|
4549
|
+
const timeout = setTimeout(() => {
|
|
4550
|
+
webSocket.close();
|
|
4551
|
+
reject(new Error(`[OIMDB CDP] Timeout (${OIM_CDP_TIMEOUT_MS / 1e3}s).`));
|
|
4552
|
+
}, OIM_CDP_TIMEOUT_MS);
|
|
4553
|
+
webSocket.once("open", () => {
|
|
4554
|
+
webSocket.send(JSON.stringify({ id: 1, method: "Runtime.evaluate", params: { expression, returnByValue: true } }));
|
|
4555
|
+
});
|
|
4556
|
+
webSocket.on("message", (raw) => {
|
|
4557
|
+
const message = JSON.parse(raw.toString());
|
|
4558
|
+
if (message.id !== 1) return;
|
|
4559
|
+
clearTimeout(timeout);
|
|
4560
|
+
webSocket.close();
|
|
4561
|
+
resolve(message.result?.result?.value);
|
|
4562
|
+
});
|
|
4563
|
+
webSocket.once("error", (error) => {
|
|
4564
|
+
clearTimeout(timeout);
|
|
4565
|
+
reject(error);
|
|
4566
|
+
});
|
|
4567
|
+
});
|
|
4568
|
+
}
|
|
4569
|
+
};
|
|
4570
|
+
export {
|
|
4571
|
+
OIMDevBridge,
|
|
4572
|
+
OIMDevBridgeStateSource,
|
|
4573
|
+
OIMDevCdpStateSource,
|
|
4574
|
+
OIMDevHttpStateSource,
|
|
4575
|
+
OIMDevModuleStateSource,
|
|
4576
|
+
OIMDevRegistryStateSource,
|
|
4577
|
+
OIMDevStateReader,
|
|
4578
|
+
OIMDevUIServer
|
|
4579
|
+
};
|