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