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