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