zod 3.13.2 → 3.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -29
- package/lib/ZodError.js +110 -205
- package/lib/benchmarks/discriminatedUnion.js +25 -25
- package/lib/benchmarks/index.js +13 -53
- package/lib/benchmarks/object.js +23 -23
- package/lib/benchmarks/realworld.d.ts +5 -0
- package/lib/benchmarks/realworld.js +56 -0
- package/lib/benchmarks/string.js +16 -16
- package/lib/benchmarks/union.js +25 -25
- package/lib/external.js +5 -1
- package/lib/helpers/errorUtil.js +2 -6
- package/lib/helpers/parseUtil.d.ts +1 -2
- package/lib/helpers/parseUtil.js +82 -242
- package/lib/helpers/util.js +20 -66
- package/lib/index.js +6 -2
- package/lib/index.mjs +1530 -2097
- package/lib/types.d.ts +4 -2
- package/lib/types.js +1319 -1704
- package/package.json +3 -3
package/lib/index.mjs
CHANGED
|
@@ -1,207 +1,53 @@
|
|
|
1
|
-
/*! *****************************************************************************
|
|
2
|
-
Copyright (c) Microsoft Corporation.
|
|
3
|
-
|
|
4
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
-
purpose with or without fee is hereby granted.
|
|
6
|
-
|
|
7
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
8
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
9
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
11
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
12
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
13
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
14
|
-
***************************************************************************** */
|
|
15
|
-
/* global Reflect, Promise */
|
|
16
|
-
|
|
17
|
-
var extendStatics = function(d, b) {
|
|
18
|
-
extendStatics = Object.setPrototypeOf ||
|
|
19
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
20
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
21
|
-
return extendStatics(d, b);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
function __extends(d, b) {
|
|
25
|
-
if (typeof b !== "function" && b !== null)
|
|
26
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
27
|
-
extendStatics(d, b);
|
|
28
|
-
function __() { this.constructor = d; }
|
|
29
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
var __assign = function() {
|
|
33
|
-
__assign = Object.assign || function __assign(t) {
|
|
34
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
35
|
-
s = arguments[i];
|
|
36
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
37
|
-
}
|
|
38
|
-
return t;
|
|
39
|
-
};
|
|
40
|
-
return __assign.apply(this, arguments);
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
44
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
45
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
46
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
47
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
48
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
49
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function __generator(thisArg, body) {
|
|
54
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
55
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
56
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
57
|
-
function step(op) {
|
|
58
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
59
|
-
while (_) try {
|
|
60
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
61
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
62
|
-
switch (op[0]) {
|
|
63
|
-
case 0: case 1: t = op; break;
|
|
64
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
65
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
66
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
67
|
-
default:
|
|
68
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
69
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
70
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
71
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
72
|
-
if (t[2]) _.ops.pop();
|
|
73
|
-
_.trys.pop(); continue;
|
|
74
|
-
}
|
|
75
|
-
op = body.call(thisArg, _);
|
|
76
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
77
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function __values(o) {
|
|
82
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
83
|
-
if (m) return m.call(o);
|
|
84
|
-
if (o && typeof o.length === "number") return {
|
|
85
|
-
next: function () {
|
|
86
|
-
if (o && i >= o.length) o = void 0;
|
|
87
|
-
return { value: o && o[i++], done: !o };
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function __read(o, n) {
|
|
94
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
95
|
-
if (!m) return o;
|
|
96
|
-
var i = m.call(o), r, ar = [], e;
|
|
97
|
-
try {
|
|
98
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
99
|
-
}
|
|
100
|
-
catch (error) { e = { error: error }; }
|
|
101
|
-
finally {
|
|
102
|
-
try {
|
|
103
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
104
|
-
}
|
|
105
|
-
finally { if (e) throw e.error; }
|
|
106
|
-
}
|
|
107
|
-
return ar;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function __spreadArray(to, from, pack) {
|
|
111
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
112
|
-
if (ar || !(i in from)) {
|
|
113
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
114
|
-
ar[i] = from[i];
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
118
|
-
}
|
|
119
|
-
|
|
120
1
|
var util;
|
|
121
2
|
(function (util) {
|
|
122
3
|
function assertNever(_x) {
|
|
123
4
|
throw new Error();
|
|
124
5
|
}
|
|
125
6
|
util.assertNever = assertNever;
|
|
126
|
-
util.arrayToEnum =
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
for (var items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
|
|
131
|
-
var item = items_1_1.value;
|
|
132
|
-
obj[item] = item;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
136
|
-
finally {
|
|
137
|
-
try {
|
|
138
|
-
if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
|
|
139
|
-
}
|
|
140
|
-
finally { if (e_1) throw e_1.error; }
|
|
7
|
+
util.arrayToEnum = (items) => {
|
|
8
|
+
const obj = {};
|
|
9
|
+
for (const item of items) {
|
|
10
|
+
obj[item] = item;
|
|
141
11
|
}
|
|
142
12
|
return obj;
|
|
143
13
|
};
|
|
144
|
-
util.getValidEnumValues =
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
for (var validKeys_1 = __values(validKeys), validKeys_1_1 = validKeys_1.next(); !validKeys_1_1.done; validKeys_1_1 = validKeys_1.next()) {
|
|
150
|
-
var k = validKeys_1_1.value;
|
|
151
|
-
filtered[k] = obj[k];
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
155
|
-
finally {
|
|
156
|
-
try {
|
|
157
|
-
if (validKeys_1_1 && !validKeys_1_1.done && (_a = validKeys_1.return)) _a.call(validKeys_1);
|
|
158
|
-
}
|
|
159
|
-
finally { if (e_2) throw e_2.error; }
|
|
14
|
+
util.getValidEnumValues = (obj) => {
|
|
15
|
+
const validKeys = util.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
|
|
16
|
+
const filtered = {};
|
|
17
|
+
for (const k of validKeys) {
|
|
18
|
+
filtered[k] = obj[k];
|
|
160
19
|
}
|
|
161
20
|
return util.objectValues(filtered);
|
|
162
21
|
};
|
|
163
|
-
util.objectValues =
|
|
22
|
+
util.objectValues = (obj) => {
|
|
164
23
|
return util.objectKeys(obj).map(function (e) {
|
|
165
24
|
return obj[e];
|
|
166
25
|
});
|
|
167
26
|
};
|
|
168
27
|
util.objectKeys = typeof Object.keys === "function" // eslint-disable-line ban/ban
|
|
169
|
-
?
|
|
170
|
-
:
|
|
171
|
-
|
|
172
|
-
for (
|
|
28
|
+
? (obj) => Object.keys(obj) // eslint-disable-line ban/ban
|
|
29
|
+
: (object) => {
|
|
30
|
+
const keys = [];
|
|
31
|
+
for (const key in object) {
|
|
173
32
|
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
174
33
|
keys.push(key);
|
|
175
34
|
}
|
|
176
35
|
}
|
|
177
36
|
return keys;
|
|
178
37
|
};
|
|
179
|
-
util.find =
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
var item = arr_1_1.value;
|
|
184
|
-
if (checker(item))
|
|
185
|
-
return item;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
189
|
-
finally {
|
|
190
|
-
try {
|
|
191
|
-
if (arr_1_1 && !arr_1_1.done && (_a = arr_1.return)) _a.call(arr_1);
|
|
192
|
-
}
|
|
193
|
-
finally { if (e_3) throw e_3.error; }
|
|
38
|
+
util.find = (arr, checker) => {
|
|
39
|
+
for (const item of arr) {
|
|
40
|
+
if (checker(item))
|
|
41
|
+
return item;
|
|
194
42
|
}
|
|
195
43
|
return undefined;
|
|
196
44
|
};
|
|
197
45
|
util.isInteger = typeof Number.isInteger === "function"
|
|
198
|
-
?
|
|
199
|
-
:
|
|
200
|
-
return typeof val === "number" && isFinite(val) && Math.floor(val) === val;
|
|
201
|
-
};
|
|
46
|
+
? (val) => Number.isInteger(val) // eslint-disable-line ban/ban
|
|
47
|
+
: (val) => typeof val === "number" && isFinite(val) && Math.floor(val) === val;
|
|
202
48
|
})(util || (util = {}));
|
|
203
49
|
|
|
204
|
-
|
|
50
|
+
const ZodIssueCode = util.arrayToEnum([
|
|
205
51
|
"invalid_type",
|
|
206
52
|
"custom",
|
|
207
53
|
"invalid_union",
|
|
@@ -217,239 +63,195 @@ var ZodIssueCode = util.arrayToEnum([
|
|
|
217
63
|
"invalid_intersection_types",
|
|
218
64
|
"not_multiple_of",
|
|
219
65
|
]);
|
|
220
|
-
|
|
221
|
-
|
|
66
|
+
const quotelessJson = (obj) => {
|
|
67
|
+
const json = JSON.stringify(obj, null, 2);
|
|
222
68
|
return json.replace(/"([^"]+)":/g, "$1:");
|
|
223
69
|
};
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
while (i < issue.path.length) {
|
|
253
|
-
var el = issue.path[i];
|
|
254
|
-
var terminal = i === issue.path.length - 1;
|
|
255
|
-
if (!terminal) {
|
|
256
|
-
if (typeof el === "string") {
|
|
257
|
-
curr[el] = curr[el] || { _errors: [] };
|
|
258
|
-
}
|
|
259
|
-
else if (typeof el === "number") {
|
|
260
|
-
var errorArray = [];
|
|
261
|
-
errorArray._errors = [];
|
|
262
|
-
curr[el] = curr[el] || errorArray;
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
else {
|
|
70
|
+
class ZodError extends Error {
|
|
71
|
+
constructor(issues) {
|
|
72
|
+
super();
|
|
73
|
+
this.issues = [];
|
|
74
|
+
this.format = () => {
|
|
75
|
+
const fieldErrors = { _errors: [] };
|
|
76
|
+
const processError = (error) => {
|
|
77
|
+
for (const issue of error.issues) {
|
|
78
|
+
if (issue.code === "invalid_union") {
|
|
79
|
+
issue.unionErrors.map(processError);
|
|
80
|
+
}
|
|
81
|
+
else if (issue.code === "invalid_return_type") {
|
|
82
|
+
processError(issue.returnTypeError);
|
|
83
|
+
}
|
|
84
|
+
else if (issue.code === "invalid_arguments") {
|
|
85
|
+
processError(issue.argumentsError);
|
|
86
|
+
}
|
|
87
|
+
else if (issue.path.length === 0) {
|
|
88
|
+
fieldErrors._errors.push(issue.message);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
let curr = fieldErrors;
|
|
92
|
+
let i = 0;
|
|
93
|
+
while (i < issue.path.length) {
|
|
94
|
+
const el = issue.path[i];
|
|
95
|
+
const terminal = i === issue.path.length - 1;
|
|
96
|
+
if (!terminal) {
|
|
97
|
+
if (typeof el === "string") {
|
|
266
98
|
curr[el] = curr[el] || { _errors: [] };
|
|
267
|
-
curr[el]._errors.push(issue.message);
|
|
268
99
|
}
|
|
269
|
-
|
|
270
|
-
|
|
100
|
+
else if (typeof el === "number") {
|
|
101
|
+
const errorArray = [];
|
|
102
|
+
errorArray._errors = [];
|
|
103
|
+
curr[el] = curr[el] || errorArray;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
108
|
+
curr[el]._errors.push(issue.message);
|
|
271
109
|
}
|
|
110
|
+
curr = curr[el];
|
|
111
|
+
i++;
|
|
272
112
|
}
|
|
273
113
|
}
|
|
274
114
|
}
|
|
275
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
276
|
-
finally {
|
|
277
|
-
try {
|
|
278
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
279
|
-
}
|
|
280
|
-
finally { if (e_1) throw e_1.error; }
|
|
281
|
-
}
|
|
282
115
|
};
|
|
283
|
-
processError(
|
|
116
|
+
processError(this);
|
|
284
117
|
return fieldErrors;
|
|
285
118
|
};
|
|
286
|
-
|
|
287
|
-
|
|
119
|
+
this.addIssue = (sub) => {
|
|
120
|
+
this.issues = [...this.issues, sub];
|
|
288
121
|
};
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
_this.issues = __spreadArray(__spreadArray([], __read(_this.issues), false), __read(subs), false);
|
|
122
|
+
this.addIssues = (subs = []) => {
|
|
123
|
+
this.issues = [...this.issues, ...subs];
|
|
292
124
|
};
|
|
293
|
-
|
|
125
|
+
const actualProto = new.target.prototype;
|
|
294
126
|
if (Object.setPrototypeOf) {
|
|
295
127
|
// eslint-disable-next-line ban/ban
|
|
296
|
-
Object.setPrototypeOf(
|
|
128
|
+
Object.setPrototypeOf(this, actualProto);
|
|
297
129
|
}
|
|
298
130
|
else {
|
|
299
|
-
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
},
|
|
309
|
-
enumerable: false,
|
|
310
|
-
configurable: true
|
|
311
|
-
});
|
|
312
|
-
ZodError.prototype.toString = function () {
|
|
131
|
+
this.__proto__ = actualProto;
|
|
132
|
+
}
|
|
133
|
+
this.name = "ZodError";
|
|
134
|
+
this.issues = issues;
|
|
135
|
+
}
|
|
136
|
+
get errors() {
|
|
137
|
+
return this.issues;
|
|
138
|
+
}
|
|
139
|
+
toString() {
|
|
313
140
|
return this.message;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
});
|
|
329
|
-
ZodError.prototype.flatten = function (mapper) {
|
|
330
|
-
var e_2, _a;
|
|
331
|
-
if (mapper === void 0) { mapper = function (issue) { return issue.message; }; }
|
|
332
|
-
var fieldErrors = {};
|
|
333
|
-
var formErrors = [];
|
|
334
|
-
try {
|
|
335
|
-
for (var _b = __values(this.issues), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
336
|
-
var sub = _c.value;
|
|
337
|
-
if (sub.path.length > 0) {
|
|
338
|
-
fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
|
|
339
|
-
fieldErrors[sub.path[0]].push(mapper(sub));
|
|
340
|
-
}
|
|
341
|
-
else {
|
|
342
|
-
formErrors.push(mapper(sub));
|
|
343
|
-
}
|
|
141
|
+
}
|
|
142
|
+
get message() {
|
|
143
|
+
return JSON.stringify(this.issues, null, 2);
|
|
144
|
+
}
|
|
145
|
+
get isEmpty() {
|
|
146
|
+
return this.issues.length === 0;
|
|
147
|
+
}
|
|
148
|
+
flatten(mapper = (issue) => issue.message) {
|
|
149
|
+
const fieldErrors = {};
|
|
150
|
+
const formErrors = [];
|
|
151
|
+
for (const sub of this.issues) {
|
|
152
|
+
if (sub.path.length > 0) {
|
|
153
|
+
fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
|
|
154
|
+
fieldErrors[sub.path[0]].push(mapper(sub));
|
|
344
155
|
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
finally {
|
|
348
|
-
try {
|
|
349
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
156
|
+
else {
|
|
157
|
+
formErrors.push(mapper(sub));
|
|
350
158
|
}
|
|
351
|
-
finally { if (e_2) throw e_2.error; }
|
|
352
159
|
}
|
|
353
|
-
return { formErrors
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
};
|
|
366
|
-
return ZodError;
|
|
367
|
-
}(Error));
|
|
368
|
-
var defaultErrorMap = function (issue, _ctx) {
|
|
369
|
-
var message;
|
|
160
|
+
return { formErrors, fieldErrors };
|
|
161
|
+
}
|
|
162
|
+
get formErrors() {
|
|
163
|
+
return this.flatten();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
ZodError.create = (issues) => {
|
|
167
|
+
const error = new ZodError(issues);
|
|
168
|
+
return error;
|
|
169
|
+
};
|
|
170
|
+
const defaultErrorMap = (issue, _ctx) => {
|
|
171
|
+
let message;
|
|
370
172
|
switch (issue.code) {
|
|
371
173
|
case ZodIssueCode.invalid_type:
|
|
372
174
|
if (issue.received === "undefined") {
|
|
373
175
|
message = "Required";
|
|
374
176
|
}
|
|
375
177
|
else {
|
|
376
|
-
message =
|
|
178
|
+
message = `Expected ${issue.expected}, received ${issue.received}`;
|
|
377
179
|
}
|
|
378
180
|
break;
|
|
379
181
|
case ZodIssueCode.unrecognized_keys:
|
|
380
|
-
message =
|
|
381
|
-
.map(
|
|
382
|
-
.join(", ")
|
|
182
|
+
message = `Unrecognized key(s) in object: ${issue.keys
|
|
183
|
+
.map((k) => `'${k}'`)
|
|
184
|
+
.join(", ")}`;
|
|
383
185
|
break;
|
|
384
186
|
case ZodIssueCode.invalid_union:
|
|
385
|
-
message =
|
|
187
|
+
message = `Invalid input`;
|
|
386
188
|
break;
|
|
387
189
|
case ZodIssueCode.invalid_union_discriminator:
|
|
388
|
-
message =
|
|
389
|
-
.map(
|
|
390
|
-
.join(" | ")
|
|
190
|
+
message = `Invalid discriminator value. Expected ${issue.options
|
|
191
|
+
.map((val) => (typeof val === "string" ? `'${val}'` : val))
|
|
192
|
+
.join(" | ")}`;
|
|
391
193
|
break;
|
|
392
194
|
case ZodIssueCode.invalid_enum_value:
|
|
393
|
-
message =
|
|
394
|
-
.map(
|
|
395
|
-
.join(" | ")
|
|
195
|
+
message = `Invalid enum value. Expected ${issue.options
|
|
196
|
+
.map((val) => (typeof val === "string" ? `'${val}'` : val))
|
|
197
|
+
.join(" | ")}`;
|
|
396
198
|
break;
|
|
397
199
|
case ZodIssueCode.invalid_arguments:
|
|
398
|
-
message =
|
|
200
|
+
message = `Invalid function arguments`;
|
|
399
201
|
break;
|
|
400
202
|
case ZodIssueCode.invalid_return_type:
|
|
401
|
-
message =
|
|
203
|
+
message = `Invalid function return type`;
|
|
402
204
|
break;
|
|
403
205
|
case ZodIssueCode.invalid_date:
|
|
404
|
-
message =
|
|
206
|
+
message = `Invalid date`;
|
|
405
207
|
break;
|
|
406
208
|
case ZodIssueCode.invalid_string:
|
|
407
209
|
if (issue.validation !== "regex")
|
|
408
|
-
message =
|
|
210
|
+
message = `Invalid ${issue.validation}`;
|
|
409
211
|
else
|
|
410
212
|
message = "Invalid";
|
|
411
213
|
break;
|
|
412
214
|
case ZodIssueCode.too_small:
|
|
413
215
|
if (issue.type === "array")
|
|
414
|
-
message =
|
|
216
|
+
message = `Array must contain ${issue.inclusive ? `at least` : `more than`} ${issue.minimum} element(s)`;
|
|
415
217
|
else if (issue.type === "string")
|
|
416
|
-
message =
|
|
218
|
+
message = `String must contain ${issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
|
|
417
219
|
else if (issue.type === "number")
|
|
418
|
-
message =
|
|
220
|
+
message = `Number must be greater than ${issue.inclusive ? `or equal to ` : ``}${issue.minimum}`;
|
|
419
221
|
else
|
|
420
222
|
message = "Invalid input";
|
|
421
223
|
break;
|
|
422
224
|
case ZodIssueCode.too_big:
|
|
423
225
|
if (issue.type === "array")
|
|
424
|
-
message =
|
|
226
|
+
message = `Array must contain ${issue.inclusive ? `at most` : `less than`} ${issue.maximum} element(s)`;
|
|
425
227
|
else if (issue.type === "string")
|
|
426
|
-
message =
|
|
228
|
+
message = `String must contain ${issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
|
|
427
229
|
else if (issue.type === "number")
|
|
428
|
-
message =
|
|
230
|
+
message = `Number must be less than ${issue.inclusive ? `or equal to ` : ``}${issue.maximum}`;
|
|
429
231
|
else
|
|
430
232
|
message = "Invalid input";
|
|
431
233
|
break;
|
|
432
234
|
case ZodIssueCode.custom:
|
|
433
|
-
message =
|
|
235
|
+
message = `Invalid input`;
|
|
434
236
|
break;
|
|
435
237
|
case ZodIssueCode.invalid_intersection_types:
|
|
436
|
-
message =
|
|
238
|
+
message = `Intersection results could not be merged`;
|
|
437
239
|
break;
|
|
438
240
|
case ZodIssueCode.not_multiple_of:
|
|
439
|
-
message =
|
|
241
|
+
message = `Number must be a multiple of ${issue.multipleOf}`;
|
|
440
242
|
break;
|
|
441
243
|
default:
|
|
442
244
|
message = _ctx.defaultError;
|
|
443
245
|
util.assertNever(issue);
|
|
444
246
|
}
|
|
445
|
-
return { message
|
|
247
|
+
return { message };
|
|
446
248
|
};
|
|
447
|
-
|
|
448
|
-
|
|
249
|
+
let overrideErrorMap = defaultErrorMap;
|
|
250
|
+
const setErrorMap = (map) => {
|
|
449
251
|
overrideErrorMap = map;
|
|
450
252
|
};
|
|
451
253
|
|
|
452
|
-
|
|
254
|
+
const ZodParsedType = util.arrayToEnum([
|
|
453
255
|
"string",
|
|
454
256
|
"nan",
|
|
455
257
|
"number",
|
|
@@ -471,83 +273,72 @@ var ZodParsedType = util.arrayToEnum([
|
|
|
471
273
|
"map",
|
|
472
274
|
"set",
|
|
473
275
|
]);
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
cache.set(data, parsedType);
|
|
477
|
-
return parsedType;
|
|
478
|
-
}
|
|
479
|
-
var getParsedType = function (data, cache) {
|
|
480
|
-
if (cache && cache.has(data))
|
|
481
|
-
return cache.get(data);
|
|
482
|
-
var t = typeof data;
|
|
276
|
+
const getParsedType = (data) => {
|
|
277
|
+
const t = typeof data;
|
|
483
278
|
switch (t) {
|
|
484
279
|
case "undefined":
|
|
485
|
-
return
|
|
280
|
+
return ZodParsedType.undefined;
|
|
486
281
|
case "string":
|
|
487
|
-
return
|
|
282
|
+
return ZodParsedType.string;
|
|
488
283
|
case "number":
|
|
489
|
-
return
|
|
284
|
+
return isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
|
|
490
285
|
case "boolean":
|
|
491
|
-
return
|
|
286
|
+
return ZodParsedType.boolean;
|
|
492
287
|
case "function":
|
|
493
|
-
return
|
|
288
|
+
return ZodParsedType.function;
|
|
494
289
|
case "bigint":
|
|
495
|
-
return
|
|
290
|
+
return ZodParsedType.bigint;
|
|
496
291
|
case "object":
|
|
497
292
|
if (Array.isArray(data)) {
|
|
498
|
-
return
|
|
293
|
+
return ZodParsedType.array;
|
|
499
294
|
}
|
|
500
295
|
if (data === null) {
|
|
501
|
-
return
|
|
296
|
+
return ZodParsedType.null;
|
|
502
297
|
}
|
|
503
298
|
if (data.then &&
|
|
504
299
|
typeof data.then === "function" &&
|
|
505
300
|
data.catch &&
|
|
506
301
|
typeof data.catch === "function") {
|
|
507
|
-
return
|
|
302
|
+
return ZodParsedType.promise;
|
|
508
303
|
}
|
|
509
304
|
if (typeof Map !== "undefined" && data instanceof Map) {
|
|
510
|
-
return
|
|
305
|
+
return ZodParsedType.map;
|
|
511
306
|
}
|
|
512
307
|
if (typeof Set !== "undefined" && data instanceof Set) {
|
|
513
|
-
return
|
|
308
|
+
return ZodParsedType.set;
|
|
514
309
|
}
|
|
515
310
|
if (typeof Date !== "undefined" && data instanceof Date) {
|
|
516
|
-
return
|
|
311
|
+
return ZodParsedType.date;
|
|
517
312
|
}
|
|
518
|
-
return
|
|
313
|
+
return ZodParsedType.object;
|
|
519
314
|
default:
|
|
520
|
-
return
|
|
315
|
+
return ZodParsedType.unknown;
|
|
521
316
|
}
|
|
522
317
|
};
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
318
|
+
const makeIssue = (params) => {
|
|
319
|
+
const { data, path, errorMaps, issueData } = params;
|
|
320
|
+
const fullPath = [...path, ...(issueData.path || [])];
|
|
321
|
+
const fullIssue = {
|
|
322
|
+
...issueData,
|
|
323
|
+
path: fullPath,
|
|
324
|
+
};
|
|
325
|
+
let errorMessage = "";
|
|
326
|
+
const maps = errorMaps
|
|
327
|
+
.filter((m) => !!m)
|
|
531
328
|
.slice()
|
|
532
329
|
.reverse();
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
var map = maps_1_1.value;
|
|
536
|
-
errorMessage = map(fullIssue, { data: data, defaultError: errorMessage }).message;
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
540
|
-
finally {
|
|
541
|
-
try {
|
|
542
|
-
if (maps_1_1 && !maps_1_1.done && (_a = maps_1.return)) _a.call(maps_1);
|
|
543
|
-
}
|
|
544
|
-
finally { if (e_1) throw e_1.error; }
|
|
330
|
+
for (const map of maps) {
|
|
331
|
+
errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message;
|
|
545
332
|
}
|
|
546
|
-
return
|
|
333
|
+
return {
|
|
334
|
+
...issueData,
|
|
335
|
+
path: fullPath,
|
|
336
|
+
message: issueData.message || errorMessage,
|
|
337
|
+
};
|
|
547
338
|
};
|
|
548
|
-
|
|
339
|
+
const EMPTY_PATH = [];
|
|
549
340
|
function addIssueToContext(ctx, issueData) {
|
|
550
|
-
|
|
341
|
+
const issue = makeIssue({
|
|
551
342
|
issueData: issueData,
|
|
552
343
|
data: ctx.data,
|
|
553
344
|
path: ctx.path,
|
|
@@ -556,149 +347,79 @@ function addIssueToContext(ctx, issueData) {
|
|
|
556
347
|
ctx.schemaErrorMap,
|
|
557
348
|
overrideErrorMap,
|
|
558
349
|
defaultErrorMap, // then global default map
|
|
559
|
-
].filter(
|
|
350
|
+
].filter((x) => !!x),
|
|
560
351
|
});
|
|
561
352
|
ctx.issues.push(issue);
|
|
562
353
|
}
|
|
563
|
-
|
|
564
|
-
|
|
354
|
+
class ParseStatus {
|
|
355
|
+
constructor() {
|
|
565
356
|
this.value = "valid";
|
|
566
357
|
}
|
|
567
|
-
|
|
358
|
+
dirty() {
|
|
568
359
|
if (this.value === "valid")
|
|
569
360
|
this.value = "dirty";
|
|
570
|
-
}
|
|
571
|
-
|
|
361
|
+
}
|
|
362
|
+
abort() {
|
|
572
363
|
if (this.value !== "aborted")
|
|
573
364
|
this.value = "aborted";
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
if (s.status === "dirty")
|
|
584
|
-
status.dirty();
|
|
585
|
-
arrayValue.push(s.value);
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
589
|
-
finally {
|
|
590
|
-
try {
|
|
591
|
-
if (results_1_1 && !results_1_1.done && (_a = results_1.return)) _a.call(results_1);
|
|
592
|
-
}
|
|
593
|
-
finally { if (e_2) throw e_2.error; }
|
|
365
|
+
}
|
|
366
|
+
static mergeArray(status, results) {
|
|
367
|
+
const arrayValue = [];
|
|
368
|
+
for (const s of results) {
|
|
369
|
+
if (s.status === "aborted")
|
|
370
|
+
return INVALID;
|
|
371
|
+
if (s.status === "dirty")
|
|
372
|
+
status.dirty();
|
|
373
|
+
arrayValue.push(s.value);
|
|
594
374
|
}
|
|
595
375
|
return { status: status.value, value: arrayValue };
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
case 0:
|
|
604
|
-
syncPairs = [];
|
|
605
|
-
_e.label = 1;
|
|
606
|
-
case 1:
|
|
607
|
-
_e.trys.push([1, 7, 8, 9]);
|
|
608
|
-
pairs_1 = __values(pairs), pairs_1_1 = pairs_1.next();
|
|
609
|
-
_e.label = 2;
|
|
610
|
-
case 2:
|
|
611
|
-
if (!!pairs_1_1.done) return [3 /*break*/, 6];
|
|
612
|
-
pair = pairs_1_1.value;
|
|
613
|
-
_b = (_a = syncPairs).push;
|
|
614
|
-
_d = {};
|
|
615
|
-
return [4 /*yield*/, pair.key];
|
|
616
|
-
case 3:
|
|
617
|
-
_d.key = _e.sent();
|
|
618
|
-
return [4 /*yield*/, pair.value];
|
|
619
|
-
case 4:
|
|
620
|
-
_b.apply(_a, [(_d.value = _e.sent(),
|
|
621
|
-
_d)]);
|
|
622
|
-
_e.label = 5;
|
|
623
|
-
case 5:
|
|
624
|
-
pairs_1_1 = pairs_1.next();
|
|
625
|
-
return [3 /*break*/, 2];
|
|
626
|
-
case 6: return [3 /*break*/, 9];
|
|
627
|
-
case 7:
|
|
628
|
-
e_3_1 = _e.sent();
|
|
629
|
-
e_3 = { error: e_3_1 };
|
|
630
|
-
return [3 /*break*/, 9];
|
|
631
|
-
case 8:
|
|
632
|
-
try {
|
|
633
|
-
if (pairs_1_1 && !pairs_1_1.done && (_c = pairs_1.return)) _c.call(pairs_1);
|
|
634
|
-
}
|
|
635
|
-
finally { if (e_3) throw e_3.error; }
|
|
636
|
-
return [7 /*endfinally*/];
|
|
637
|
-
case 9: return [2 /*return*/, ParseStatus.mergeObjectSync(status, syncPairs)];
|
|
638
|
-
}
|
|
376
|
+
}
|
|
377
|
+
static async mergeObjectAsync(status, pairs) {
|
|
378
|
+
const syncPairs = [];
|
|
379
|
+
for (const pair of pairs) {
|
|
380
|
+
syncPairs.push({
|
|
381
|
+
key: await pair.key,
|
|
382
|
+
value: await pair.value,
|
|
639
383
|
});
|
|
640
|
-
});
|
|
641
|
-
};
|
|
642
|
-
ParseStatus.mergeObjectSync = function (status, pairs) {
|
|
643
|
-
var e_4, _a;
|
|
644
|
-
var finalObject = {};
|
|
645
|
-
try {
|
|
646
|
-
for (var pairs_2 = __values(pairs), pairs_2_1 = pairs_2.next(); !pairs_2_1.done; pairs_2_1 = pairs_2.next()) {
|
|
647
|
-
var pair = pairs_2_1.value;
|
|
648
|
-
var key = pair.key, value = pair.value;
|
|
649
|
-
if (key.status === "aborted")
|
|
650
|
-
return INVALID;
|
|
651
|
-
if (value.status === "aborted")
|
|
652
|
-
return INVALID;
|
|
653
|
-
if (key.status === "dirty")
|
|
654
|
-
status.dirty();
|
|
655
|
-
if (value.status === "dirty")
|
|
656
|
-
status.dirty();
|
|
657
|
-
if (typeof value.value !== "undefined" || pair.alwaysSet) {
|
|
658
|
-
finalObject[key.value] = value.value;
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
384
|
}
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
385
|
+
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
386
|
+
}
|
|
387
|
+
static mergeObjectSync(status, pairs) {
|
|
388
|
+
const finalObject = {};
|
|
389
|
+
for (const pair of pairs) {
|
|
390
|
+
const { key, value } = pair;
|
|
391
|
+
if (key.status === "aborted")
|
|
392
|
+
return INVALID;
|
|
393
|
+
if (value.status === "aborted")
|
|
394
|
+
return INVALID;
|
|
395
|
+
if (key.status === "dirty")
|
|
396
|
+
status.dirty();
|
|
397
|
+
if (value.status === "dirty")
|
|
398
|
+
status.dirty();
|
|
399
|
+
if (typeof value.value !== "undefined" || pair.alwaysSet) {
|
|
400
|
+
finalObject[key.value] = value.value;
|
|
666
401
|
}
|
|
667
|
-
finally { if (e_4) throw e_4.error; }
|
|
668
402
|
}
|
|
669
403
|
return { status: status.value, value: finalObject };
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
var INVALID = Object.freeze({
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
const INVALID = Object.freeze({
|
|
674
407
|
status: "aborted",
|
|
675
408
|
});
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
return x.status === "dirty";
|
|
683
|
-
};
|
|
684
|
-
var isValid = function (x) {
|
|
685
|
-
return x.status === "valid";
|
|
686
|
-
};
|
|
687
|
-
var isAsync = function (x) {
|
|
688
|
-
return typeof Promise !== undefined && x instanceof Promise;
|
|
689
|
-
};
|
|
409
|
+
const DIRTY = (value) => ({ status: "dirty", value });
|
|
410
|
+
const OK = (value) => ({ status: "valid", value });
|
|
411
|
+
const isAborted = (x) => x.status === "aborted";
|
|
412
|
+
const isDirty = (x) => x.status === "dirty";
|
|
413
|
+
const isValid = (x) => x.status === "valid";
|
|
414
|
+
const isAsync = (x) => typeof Promise !== undefined && x instanceof Promise;
|
|
690
415
|
|
|
691
416
|
var errorUtil;
|
|
692
417
|
(function (errorUtil) {
|
|
693
|
-
errorUtil.errToObj =
|
|
694
|
-
|
|
695
|
-
};
|
|
696
|
-
errorUtil.toString = function (message) {
|
|
697
|
-
return typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
|
|
698
|
-
};
|
|
418
|
+
errorUtil.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
419
|
+
errorUtil.toString = (message) => typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
|
|
699
420
|
})(errorUtil || (errorUtil = {}));
|
|
700
421
|
|
|
701
|
-
|
|
422
|
+
const handleResult = (ctx, result) => {
|
|
702
423
|
if (isValid(result)) {
|
|
703
424
|
return { success: true, data: result.value };
|
|
704
425
|
}
|
|
@@ -706,20 +427,20 @@ var handleResult = function (ctx, result) {
|
|
|
706
427
|
if (!ctx.issues.length) {
|
|
707
428
|
throw new Error("Validation failed but no issues detected.");
|
|
708
429
|
}
|
|
709
|
-
|
|
710
|
-
return { success: false, error
|
|
430
|
+
const error = new ZodError(ctx.issues);
|
|
431
|
+
return { success: false, error };
|
|
711
432
|
}
|
|
712
433
|
};
|
|
713
434
|
function processCreateParams(params) {
|
|
714
435
|
if (!params)
|
|
715
436
|
return {};
|
|
716
|
-
|
|
437
|
+
const { errorMap, invalid_type_error, required_error, description } = params;
|
|
717
438
|
if (errorMap && (invalid_type_error || required_error)) {
|
|
718
|
-
throw new Error(
|
|
439
|
+
throw new Error(`Can't use "invalid" or "required" in conjunction with custom error map.`);
|
|
719
440
|
}
|
|
720
441
|
if (errorMap)
|
|
721
|
-
return { errorMap: errorMap, description
|
|
722
|
-
|
|
442
|
+
return { errorMap: errorMap, description };
|
|
443
|
+
const customMap = (iss, ctx) => {
|
|
723
444
|
if (iss.code !== "invalid_type")
|
|
724
445
|
return { message: ctx.defaultError };
|
|
725
446
|
if (typeof ctx.data === "undefined" && required_error)
|
|
@@ -728,10 +449,10 @@ function processCreateParams(params) {
|
|
|
728
449
|
return { message: params.invalid_type_error };
|
|
729
450
|
return { message: ctx.defaultError };
|
|
730
451
|
};
|
|
731
|
-
return { errorMap: customMap, description
|
|
452
|
+
return { errorMap: customMap, description };
|
|
732
453
|
}
|
|
733
|
-
|
|
734
|
-
|
|
454
|
+
class ZodType {
|
|
455
|
+
constructor(def) {
|
|
735
456
|
/** Alias of safeParseAsync */
|
|
736
457
|
this.spa = this.safeParseAsync;
|
|
737
458
|
this.superRefine = this._refinement;
|
|
@@ -757,99 +478,81 @@ var ZodType = /** @class */ (function () {
|
|
|
757
478
|
this.isOptional = this.isOptional.bind(this);
|
|
758
479
|
this.isNullable = this.isNullable.bind(this);
|
|
759
480
|
}
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
enumerable: false,
|
|
765
|
-
configurable: true
|
|
766
|
-
});
|
|
767
|
-
ZodType.prototype._processInputParams = function (input) {
|
|
481
|
+
get description() {
|
|
482
|
+
return this._def.description;
|
|
483
|
+
}
|
|
484
|
+
_processInputParams(input) {
|
|
768
485
|
return {
|
|
769
486
|
status: new ParseStatus(),
|
|
770
|
-
ctx:
|
|
487
|
+
ctx: {
|
|
488
|
+
...input.parent,
|
|
489
|
+
data: input.data,
|
|
490
|
+
parsedType: getParsedType(input.data),
|
|
491
|
+
schemaErrorMap: this._def.errorMap,
|
|
492
|
+
path: input.path,
|
|
493
|
+
parent: input.parent,
|
|
494
|
+
},
|
|
771
495
|
};
|
|
772
|
-
}
|
|
773
|
-
|
|
774
|
-
|
|
496
|
+
}
|
|
497
|
+
_parseSync(input) {
|
|
498
|
+
const result = this._parse(input);
|
|
775
499
|
if (isAsync(result)) {
|
|
776
500
|
throw new Error("Synchronous parse encountered promise.");
|
|
777
501
|
}
|
|
778
502
|
return result;
|
|
779
|
-
}
|
|
780
|
-
|
|
781
|
-
|
|
503
|
+
}
|
|
504
|
+
_parseAsync(input) {
|
|
505
|
+
const result = this._parse(input);
|
|
782
506
|
return Promise.resolve(result);
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
|
|
507
|
+
}
|
|
508
|
+
parse(data, params) {
|
|
509
|
+
const result = this.safeParse(data, params);
|
|
786
510
|
if (result.success)
|
|
787
511
|
return result.data;
|
|
788
512
|
throw result.error;
|
|
789
|
-
}
|
|
790
|
-
|
|
513
|
+
}
|
|
514
|
+
safeParse(data, params) {
|
|
791
515
|
var _a;
|
|
792
|
-
|
|
516
|
+
const ctx = {
|
|
793
517
|
path: (params === null || params === void 0 ? void 0 : params.path) || [],
|
|
794
518
|
issues: [],
|
|
795
519
|
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
|
|
796
520
|
schemaErrorMap: this._def.errorMap,
|
|
797
521
|
async: (_a = params === null || params === void 0 ? void 0 : params.async) !== null && _a !== void 0 ? _a : false,
|
|
798
|
-
typeCache: typeof Map !== "undefined" ? new Map() : undefined,
|
|
799
522
|
parent: null,
|
|
800
|
-
data
|
|
523
|
+
data,
|
|
801
524
|
parsedType: getParsedType(data),
|
|
802
525
|
};
|
|
803
|
-
|
|
526
|
+
const result = this._parseSync({ data, path: ctx.path, parent: ctx });
|
|
804
527
|
return handleResult(ctx, result);
|
|
805
|
-
}
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
return
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
schemaErrorMap: this._def.errorMap,
|
|
832
|
-
async: true,
|
|
833
|
-
typeCache: typeof Map !== "undefined" ? new Map() : undefined,
|
|
834
|
-
parent: null,
|
|
835
|
-
data: data,
|
|
836
|
-
parsedType: getParsedType(data),
|
|
837
|
-
};
|
|
838
|
-
maybeAsyncResult = this._parse({ data: data, path: [], parent: ctx });
|
|
839
|
-
return [4 /*yield*/, (isAsync(maybeAsyncResult)
|
|
840
|
-
? maybeAsyncResult
|
|
841
|
-
: Promise.resolve(maybeAsyncResult))];
|
|
842
|
-
case 1:
|
|
843
|
-
result = _a.sent();
|
|
844
|
-
return [2 /*return*/, handleResult(ctx, result)];
|
|
845
|
-
}
|
|
846
|
-
});
|
|
847
|
-
});
|
|
848
|
-
};
|
|
849
|
-
ZodType.prototype.refine = function (check, message) {
|
|
850
|
-
var getIssueProperties = function (val) {
|
|
528
|
+
}
|
|
529
|
+
async parseAsync(data, params) {
|
|
530
|
+
const result = await this.safeParseAsync(data, params);
|
|
531
|
+
if (result.success)
|
|
532
|
+
return result.data;
|
|
533
|
+
throw result.error;
|
|
534
|
+
}
|
|
535
|
+
async safeParseAsync(data, params) {
|
|
536
|
+
const ctx = {
|
|
537
|
+
path: (params === null || params === void 0 ? void 0 : params.path) || [],
|
|
538
|
+
issues: [],
|
|
539
|
+
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
|
|
540
|
+
schemaErrorMap: this._def.errorMap,
|
|
541
|
+
async: true,
|
|
542
|
+
parent: null,
|
|
543
|
+
data,
|
|
544
|
+
parsedType: getParsedType(data),
|
|
545
|
+
};
|
|
546
|
+
const maybeAsyncResult = this._parse({ data, path: [], parent: ctx });
|
|
547
|
+
const result = await (isAsync(maybeAsyncResult)
|
|
548
|
+
? maybeAsyncResult
|
|
549
|
+
: Promise.resolve(maybeAsyncResult));
|
|
550
|
+
return handleResult(ctx, result);
|
|
551
|
+
}
|
|
552
|
+
refine(check, message) {
|
|
553
|
+
const getIssueProperties = (val) => {
|
|
851
554
|
if (typeof message === "string" || typeof message === "undefined") {
|
|
852
|
-
return { message
|
|
555
|
+
return { message };
|
|
853
556
|
}
|
|
854
557
|
else if (typeof message === "function") {
|
|
855
558
|
return message(val);
|
|
@@ -858,13 +561,14 @@ var ZodType = /** @class */ (function () {
|
|
|
858
561
|
return message;
|
|
859
562
|
}
|
|
860
563
|
};
|
|
861
|
-
return this._refinement(
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
564
|
+
return this._refinement((val, ctx) => {
|
|
565
|
+
const result = check(val);
|
|
566
|
+
const setError = () => ctx.addIssue({
|
|
567
|
+
code: ZodIssueCode.custom,
|
|
568
|
+
...getIssueProperties(val),
|
|
569
|
+
});
|
|
866
570
|
if (typeof Promise !== "undefined" && result instanceof Promise) {
|
|
867
|
-
return result.then(
|
|
571
|
+
return result.then((data) => {
|
|
868
572
|
if (!data) {
|
|
869
573
|
setError();
|
|
870
574
|
return false;
|
|
@@ -882,9 +586,9 @@ var ZodType = /** @class */ (function () {
|
|
|
882
586
|
return true;
|
|
883
587
|
}
|
|
884
588
|
});
|
|
885
|
-
}
|
|
886
|
-
|
|
887
|
-
return this._refinement(
|
|
589
|
+
}
|
|
590
|
+
refinement(check, refinementData) {
|
|
591
|
+
return this._refinement((val, ctx) => {
|
|
888
592
|
if (!check(val)) {
|
|
889
593
|
ctx.addIssue(typeof refinementData === "function"
|
|
890
594
|
? refinementData(val, ctx)
|
|
@@ -895,88 +599,87 @@ var ZodType = /** @class */ (function () {
|
|
|
895
599
|
return true;
|
|
896
600
|
}
|
|
897
601
|
});
|
|
898
|
-
}
|
|
899
|
-
|
|
602
|
+
}
|
|
603
|
+
_refinement(refinement) {
|
|
900
604
|
return new ZodEffects({
|
|
901
605
|
schema: this,
|
|
902
606
|
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
903
|
-
effect: { type: "refinement", refinement
|
|
607
|
+
effect: { type: "refinement", refinement },
|
|
904
608
|
});
|
|
905
|
-
}
|
|
906
|
-
|
|
609
|
+
}
|
|
610
|
+
optional() {
|
|
907
611
|
return ZodOptional.create(this);
|
|
908
|
-
}
|
|
909
|
-
|
|
612
|
+
}
|
|
613
|
+
nullable() {
|
|
910
614
|
return ZodNullable.create(this);
|
|
911
|
-
}
|
|
912
|
-
|
|
615
|
+
}
|
|
616
|
+
nullish() {
|
|
913
617
|
return this.optional().nullable();
|
|
914
|
-
}
|
|
915
|
-
|
|
618
|
+
}
|
|
619
|
+
array() {
|
|
916
620
|
return ZodArray.create(this);
|
|
917
|
-
}
|
|
918
|
-
|
|
621
|
+
}
|
|
622
|
+
promise() {
|
|
919
623
|
return ZodPromise.create(this);
|
|
920
|
-
}
|
|
921
|
-
|
|
624
|
+
}
|
|
625
|
+
or(option) {
|
|
922
626
|
return ZodUnion.create([this, option]);
|
|
923
|
-
}
|
|
924
|
-
|
|
627
|
+
}
|
|
628
|
+
and(incoming) {
|
|
925
629
|
return ZodIntersection.create(this, incoming);
|
|
926
|
-
}
|
|
927
|
-
|
|
630
|
+
}
|
|
631
|
+
transform(transform) {
|
|
928
632
|
return new ZodEffects({
|
|
929
633
|
schema: this,
|
|
930
634
|
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
931
|
-
effect: { type: "transform", transform
|
|
635
|
+
effect: { type: "transform", transform },
|
|
932
636
|
});
|
|
933
|
-
}
|
|
934
|
-
|
|
935
|
-
|
|
637
|
+
}
|
|
638
|
+
default(def) {
|
|
639
|
+
const defaultValueFunc = typeof def === "function" ? def : () => def;
|
|
936
640
|
return new ZodDefault({
|
|
937
641
|
innerType: this,
|
|
938
642
|
defaultValue: defaultValueFunc,
|
|
939
643
|
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
940
644
|
});
|
|
941
|
-
}
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
return new This(
|
|
945
|
-
|
|
946
|
-
|
|
645
|
+
}
|
|
646
|
+
describe(description) {
|
|
647
|
+
const This = this.constructor;
|
|
648
|
+
return new This({
|
|
649
|
+
...this._def,
|
|
650
|
+
description,
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
isOptional() {
|
|
947
654
|
return this.safeParse(undefined).success;
|
|
948
|
-
}
|
|
949
|
-
|
|
655
|
+
}
|
|
656
|
+
isNullable() {
|
|
950
657
|
return this.safeParse(null).success;
|
|
951
|
-
}
|
|
952
|
-
|
|
953
|
-
}
|
|
954
|
-
|
|
955
|
-
var uuidRegex = /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i;
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
const cuidRegex = /^c[^\s-]{8,}$/i;
|
|
661
|
+
const uuidRegex = /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i;
|
|
956
662
|
// from https://stackoverflow.com/a/46181/1550155
|
|
957
663
|
// old version: too slow, didn't support unicode
|
|
958
664
|
// const emailRegex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
|
|
959
665
|
// eslint-disable-next-line
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
666
|
+
const emailRegex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
|
|
667
|
+
class ZodString extends ZodType {
|
|
668
|
+
constructor() {
|
|
669
|
+
super(...arguments);
|
|
670
|
+
this._regex = (regex, validation, message) => this.refinement((data) => regex.test(data), {
|
|
671
|
+
validation,
|
|
672
|
+
code: ZodIssueCode.invalid_string,
|
|
673
|
+
...errorUtil.errToObj(message),
|
|
674
|
+
});
|
|
968
675
|
/**
|
|
969
676
|
* Deprecated.
|
|
970
677
|
* Use z.string().min(1) instead.
|
|
971
678
|
*/
|
|
972
|
-
|
|
973
|
-
return _this.min(1, errorUtil.errToObj(message));
|
|
974
|
-
};
|
|
975
|
-
return _this;
|
|
679
|
+
this.nonempty = (message) => this.min(1, errorUtil.errToObj(message));
|
|
976
680
|
}
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
var _b = this._processInputParams(input), status = _b.status, ctx = _b.ctx;
|
|
681
|
+
_parse(input) {
|
|
682
|
+
const { status, ctx } = this._processInputParams(input);
|
|
980
683
|
if (ctx.parsedType !== ZodParsedType.string) {
|
|
981
684
|
addIssueToContext(ctx, {
|
|
982
685
|
code: ZodIssueCode.invalid_type,
|
|
@@ -987,210 +690,191 @@ var ZodString = /** @class */ (function (_super) {
|
|
|
987
690
|
);
|
|
988
691
|
return INVALID;
|
|
989
692
|
}
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
});
|
|
1002
|
-
status.dirty();
|
|
1003
|
-
}
|
|
693
|
+
for (const check of this._def.checks) {
|
|
694
|
+
if (check.kind === "min") {
|
|
695
|
+
if (ctx.data.length < check.value) {
|
|
696
|
+
addIssueToContext(ctx, {
|
|
697
|
+
code: ZodIssueCode.too_small,
|
|
698
|
+
minimum: check.value,
|
|
699
|
+
type: "string",
|
|
700
|
+
inclusive: true,
|
|
701
|
+
message: check.message,
|
|
702
|
+
});
|
|
703
|
+
status.dirty();
|
|
1004
704
|
}
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
705
|
+
}
|
|
706
|
+
else if (check.kind === "max") {
|
|
707
|
+
if (ctx.data.length > check.value) {
|
|
708
|
+
addIssueToContext(ctx, {
|
|
709
|
+
code: ZodIssueCode.too_big,
|
|
710
|
+
maximum: check.value,
|
|
711
|
+
type: "string",
|
|
712
|
+
inclusive: true,
|
|
713
|
+
message: check.message,
|
|
714
|
+
});
|
|
715
|
+
status.dirty();
|
|
1016
716
|
}
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
717
|
+
}
|
|
718
|
+
else if (check.kind === "email") {
|
|
719
|
+
if (!emailRegex.test(ctx.data)) {
|
|
720
|
+
addIssueToContext(ctx, {
|
|
721
|
+
validation: "email",
|
|
722
|
+
code: ZodIssueCode.invalid_string,
|
|
723
|
+
message: check.message,
|
|
724
|
+
});
|
|
725
|
+
status.dirty();
|
|
1026
726
|
}
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
727
|
+
}
|
|
728
|
+
else if (check.kind === "uuid") {
|
|
729
|
+
if (!uuidRegex.test(ctx.data)) {
|
|
730
|
+
addIssueToContext(ctx, {
|
|
731
|
+
validation: "uuid",
|
|
732
|
+
code: ZodIssueCode.invalid_string,
|
|
733
|
+
message: check.message,
|
|
734
|
+
});
|
|
735
|
+
status.dirty();
|
|
1036
736
|
}
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
737
|
+
}
|
|
738
|
+
else if (check.kind === "cuid") {
|
|
739
|
+
if (!cuidRegex.test(ctx.data)) {
|
|
740
|
+
addIssueToContext(ctx, {
|
|
741
|
+
validation: "cuid",
|
|
742
|
+
code: ZodIssueCode.invalid_string,
|
|
743
|
+
message: check.message,
|
|
744
|
+
});
|
|
745
|
+
status.dirty();
|
|
1046
746
|
}
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
catch (_e) {
|
|
1052
|
-
addIssueToContext(ctx, {
|
|
1053
|
-
validation: "url",
|
|
1054
|
-
code: ZodIssueCode.invalid_string,
|
|
1055
|
-
message: check.message,
|
|
1056
|
-
});
|
|
1057
|
-
status.dirty();
|
|
1058
|
-
}
|
|
747
|
+
}
|
|
748
|
+
else if (check.kind === "url") {
|
|
749
|
+
try {
|
|
750
|
+
new URL(ctx.data);
|
|
1059
751
|
}
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
message: check.message,
|
|
1068
|
-
});
|
|
1069
|
-
status.dirty();
|
|
1070
|
-
}
|
|
752
|
+
catch (_a) {
|
|
753
|
+
addIssueToContext(ctx, {
|
|
754
|
+
validation: "url",
|
|
755
|
+
code: ZodIssueCode.invalid_string,
|
|
756
|
+
message: check.message,
|
|
757
|
+
});
|
|
758
|
+
status.dirty();
|
|
1071
759
|
}
|
|
1072
760
|
}
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
761
|
+
else if (check.kind === "regex") {
|
|
762
|
+
check.regex.lastIndex = 0;
|
|
763
|
+
const testResult = check.regex.test(ctx.data);
|
|
764
|
+
if (!testResult) {
|
|
765
|
+
addIssueToContext(ctx, {
|
|
766
|
+
validation: "regex",
|
|
767
|
+
code: ZodIssueCode.invalid_string,
|
|
768
|
+
message: check.message,
|
|
769
|
+
});
|
|
770
|
+
status.dirty();
|
|
771
|
+
}
|
|
1078
772
|
}
|
|
1079
|
-
finally { if (e_1) throw e_1.error; }
|
|
1080
773
|
}
|
|
1081
774
|
return { status: status.value, value: ctx.data };
|
|
1082
|
-
}
|
|
1083
|
-
|
|
1084
|
-
return new ZodString(
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
}
|
|
1089
|
-
|
|
1090
|
-
return this._addCheck(
|
|
1091
|
-
}
|
|
1092
|
-
|
|
1093
|
-
return this._addCheck(
|
|
1094
|
-
}
|
|
1095
|
-
|
|
1096
|
-
return this._addCheck(
|
|
1097
|
-
}
|
|
1098
|
-
|
|
1099
|
-
return this._addCheck(
|
|
1100
|
-
}
|
|
1101
|
-
|
|
1102
|
-
return this._addCheck(
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
775
|
+
}
|
|
776
|
+
_addCheck(check) {
|
|
777
|
+
return new ZodString({
|
|
778
|
+
...this._def,
|
|
779
|
+
checks: [...this._def.checks, check],
|
|
780
|
+
});
|
|
781
|
+
}
|
|
782
|
+
email(message) {
|
|
783
|
+
return this._addCheck({ kind: "email", ...errorUtil.errToObj(message) });
|
|
784
|
+
}
|
|
785
|
+
url(message) {
|
|
786
|
+
return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
|
|
787
|
+
}
|
|
788
|
+
uuid(message) {
|
|
789
|
+
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
|
|
790
|
+
}
|
|
791
|
+
cuid(message) {
|
|
792
|
+
return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
|
|
793
|
+
}
|
|
794
|
+
regex(regex, message) {
|
|
795
|
+
return this._addCheck({
|
|
796
|
+
kind: "regex",
|
|
797
|
+
regex: regex,
|
|
798
|
+
...errorUtil.errToObj(message),
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
min(minLength, message) {
|
|
802
|
+
return this._addCheck({
|
|
803
|
+
kind: "min",
|
|
804
|
+
value: minLength,
|
|
805
|
+
...errorUtil.errToObj(message),
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
max(maxLength, message) {
|
|
809
|
+
return this._addCheck({
|
|
810
|
+
kind: "max",
|
|
811
|
+
value: maxLength,
|
|
812
|
+
...errorUtil.errToObj(message),
|
|
813
|
+
});
|
|
814
|
+
}
|
|
815
|
+
length(len, message) {
|
|
1108
816
|
return this.min(len, message).max(len, message);
|
|
1109
|
-
}
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
enumerable: false,
|
|
1129
|
-
configurable: true
|
|
1130
|
-
});
|
|
1131
|
-
Object.defineProperty(ZodString.prototype, "isCUID", {
|
|
1132
|
-
get: function () {
|
|
1133
|
-
return !!this._def.checks.find(function (ch) { return ch.kind === "cuid"; });
|
|
1134
|
-
},
|
|
1135
|
-
enumerable: false,
|
|
1136
|
-
configurable: true
|
|
1137
|
-
});
|
|
1138
|
-
Object.defineProperty(ZodString.prototype, "minLength", {
|
|
1139
|
-
get: function () {
|
|
1140
|
-
var min = -Infinity;
|
|
1141
|
-
this._def.checks.map(function (ch) {
|
|
1142
|
-
if (ch.kind === "min") {
|
|
1143
|
-
if (min === null || ch.value > min) {
|
|
1144
|
-
min = ch.value;
|
|
1145
|
-
}
|
|
817
|
+
}
|
|
818
|
+
get isEmail() {
|
|
819
|
+
return !!this._def.checks.find((ch) => ch.kind === "email");
|
|
820
|
+
}
|
|
821
|
+
get isURL() {
|
|
822
|
+
return !!this._def.checks.find((ch) => ch.kind === "url");
|
|
823
|
+
}
|
|
824
|
+
get isUUID() {
|
|
825
|
+
return !!this._def.checks.find((ch) => ch.kind === "uuid");
|
|
826
|
+
}
|
|
827
|
+
get isCUID() {
|
|
828
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid");
|
|
829
|
+
}
|
|
830
|
+
get minLength() {
|
|
831
|
+
let min = -Infinity;
|
|
832
|
+
this._def.checks.map((ch) => {
|
|
833
|
+
if (ch.kind === "min") {
|
|
834
|
+
if (min === null || ch.value > min) {
|
|
835
|
+
min = ch.value;
|
|
1146
836
|
}
|
|
1147
|
-
}
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
if (ch.kind === "max") {
|
|
1158
|
-
if (max === null || ch.value < max) {
|
|
1159
|
-
max = ch.value;
|
|
1160
|
-
}
|
|
837
|
+
}
|
|
838
|
+
});
|
|
839
|
+
return min;
|
|
840
|
+
}
|
|
841
|
+
get maxLength() {
|
|
842
|
+
let max = null;
|
|
843
|
+
this._def.checks.map((ch) => {
|
|
844
|
+
if (ch.kind === "max") {
|
|
845
|
+
if (max === null || ch.value < max) {
|
|
846
|
+
max = ch.value;
|
|
1161
847
|
}
|
|
1162
|
-
}
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
848
|
+
}
|
|
849
|
+
});
|
|
850
|
+
return max;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
ZodString.create = (params) => {
|
|
854
|
+
return new ZodString({
|
|
855
|
+
checks: [],
|
|
856
|
+
typeName: ZodFirstPartyTypeKind.ZodString,
|
|
857
|
+
...processCreateParams(params),
|
|
1167
858
|
});
|
|
1168
|
-
|
|
1169
|
-
return new ZodString(__assign({ checks: [], typeName: ZodFirstPartyTypeKind.ZodString }, processCreateParams(params)));
|
|
1170
|
-
};
|
|
1171
|
-
return ZodString;
|
|
1172
|
-
}(ZodType));
|
|
859
|
+
};
|
|
1173
860
|
// https://stackoverflow.com/questions/3966484/why-does-modulus-operator-return-fractional-number-in-javascript/31711034#31711034
|
|
1174
861
|
function floatSafeRemainder(val, step) {
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
862
|
+
const valDecCount = (val.toString().split(".")[1] || "").length;
|
|
863
|
+
const stepDecCount = (step.toString().split(".")[1] || "").length;
|
|
864
|
+
const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
|
|
865
|
+
const valInt = parseInt(val.toFixed(decCount).replace(".", ""));
|
|
866
|
+
const stepInt = parseInt(step.toFixed(decCount).replace(".", ""));
|
|
1180
867
|
return (valInt % stepInt) / Math.pow(10, decCount);
|
|
1181
868
|
}
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
ZodNumber.prototype._parse = function (input) {
|
|
1192
|
-
var e_2, _a;
|
|
1193
|
-
var _b = this._processInputParams(input), status = _b.status, ctx = _b.ctx;
|
|
869
|
+
class ZodNumber extends ZodType {
|
|
870
|
+
constructor() {
|
|
871
|
+
super(...arguments);
|
|
872
|
+
this.min = this.gte;
|
|
873
|
+
this.max = this.lte;
|
|
874
|
+
this.step = this.multipleOf;
|
|
875
|
+
}
|
|
876
|
+
_parse(input) {
|
|
877
|
+
const { status, ctx } = this._processInputParams(input);
|
|
1194
878
|
if (ctx.parsedType !== ZodParsedType.number) {
|
|
1195
879
|
addIssueToContext(ctx, {
|
|
1196
880
|
code: ZodIssueCode.invalid_type,
|
|
@@ -1199,213 +883,175 @@ var ZodNumber = /** @class */ (function (_super) {
|
|
|
1199
883
|
});
|
|
1200
884
|
return INVALID;
|
|
1201
885
|
}
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
});
|
|
1213
|
-
status.dirty();
|
|
1214
|
-
}
|
|
1215
|
-
}
|
|
1216
|
-
else if (check.kind === "min") {
|
|
1217
|
-
var tooSmall = check.inclusive
|
|
1218
|
-
? ctx.data < check.value
|
|
1219
|
-
: ctx.data <= check.value;
|
|
1220
|
-
if (tooSmall) {
|
|
1221
|
-
addIssueToContext(ctx, {
|
|
1222
|
-
code: ZodIssueCode.too_small,
|
|
1223
|
-
minimum: check.value,
|
|
1224
|
-
type: "number",
|
|
1225
|
-
inclusive: check.inclusive,
|
|
1226
|
-
message: check.message,
|
|
1227
|
-
});
|
|
1228
|
-
status.dirty();
|
|
1229
|
-
}
|
|
886
|
+
for (const check of this._def.checks) {
|
|
887
|
+
if (check.kind === "int") {
|
|
888
|
+
if (!util.isInteger(ctx.data)) {
|
|
889
|
+
addIssueToContext(ctx, {
|
|
890
|
+
code: ZodIssueCode.invalid_type,
|
|
891
|
+
expected: "integer",
|
|
892
|
+
received: "float",
|
|
893
|
+
message: check.message,
|
|
894
|
+
});
|
|
895
|
+
status.dirty();
|
|
1230
896
|
}
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
897
|
+
}
|
|
898
|
+
else if (check.kind === "min") {
|
|
899
|
+
const tooSmall = check.inclusive
|
|
900
|
+
? ctx.data < check.value
|
|
901
|
+
: ctx.data <= check.value;
|
|
902
|
+
if (tooSmall) {
|
|
903
|
+
addIssueToContext(ctx, {
|
|
904
|
+
code: ZodIssueCode.too_small,
|
|
905
|
+
minimum: check.value,
|
|
906
|
+
type: "number",
|
|
907
|
+
inclusive: check.inclusive,
|
|
908
|
+
message: check.message,
|
|
909
|
+
});
|
|
910
|
+
status.dirty();
|
|
1245
911
|
}
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
912
|
+
}
|
|
913
|
+
else if (check.kind === "max") {
|
|
914
|
+
const tooBig = check.inclusive
|
|
915
|
+
? ctx.data > check.value
|
|
916
|
+
: ctx.data >= check.value;
|
|
917
|
+
if (tooBig) {
|
|
918
|
+
addIssueToContext(ctx, {
|
|
919
|
+
code: ZodIssueCode.too_big,
|
|
920
|
+
maximum: check.value,
|
|
921
|
+
type: "number",
|
|
922
|
+
inclusive: check.inclusive,
|
|
923
|
+
message: check.message,
|
|
924
|
+
});
|
|
925
|
+
status.dirty();
|
|
1255
926
|
}
|
|
1256
|
-
|
|
1257
|
-
|
|
927
|
+
}
|
|
928
|
+
else if (check.kind === "multipleOf") {
|
|
929
|
+
if (floatSafeRemainder(ctx.data, check.value) !== 0) {
|
|
930
|
+
addIssueToContext(ctx, {
|
|
931
|
+
code: ZodIssueCode.not_multiple_of,
|
|
932
|
+
multipleOf: check.value,
|
|
933
|
+
message: check.message,
|
|
934
|
+
});
|
|
935
|
+
status.dirty();
|
|
1258
936
|
}
|
|
1259
937
|
}
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
finally {
|
|
1263
|
-
try {
|
|
1264
|
-
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
938
|
+
else {
|
|
939
|
+
util.assertNever(check);
|
|
1265
940
|
}
|
|
1266
|
-
finally { if (e_2) throw e_2.error; }
|
|
1267
941
|
}
|
|
1268
942
|
return { status: status.value, value: ctx.data };
|
|
1269
|
-
}
|
|
1270
|
-
|
|
943
|
+
}
|
|
944
|
+
gte(value, message) {
|
|
1271
945
|
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
1272
|
-
}
|
|
1273
|
-
|
|
946
|
+
}
|
|
947
|
+
gt(value, message) {
|
|
1274
948
|
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
1275
|
-
}
|
|
1276
|
-
|
|
949
|
+
}
|
|
950
|
+
lte(value, message) {
|
|
1277
951
|
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
1278
|
-
}
|
|
1279
|
-
|
|
952
|
+
}
|
|
953
|
+
lt(value, message) {
|
|
1280
954
|
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
1281
|
-
}
|
|
1282
|
-
|
|
1283
|
-
return new ZodNumber(
|
|
955
|
+
}
|
|
956
|
+
setLimit(kind, value, inclusive, message) {
|
|
957
|
+
return new ZodNumber({
|
|
958
|
+
...this._def,
|
|
959
|
+
checks: [
|
|
960
|
+
...this._def.checks,
|
|
1284
961
|
{
|
|
1285
|
-
kind
|
|
1286
|
-
value
|
|
1287
|
-
inclusive
|
|
962
|
+
kind,
|
|
963
|
+
value,
|
|
964
|
+
inclusive,
|
|
1288
965
|
message: errorUtil.toString(message),
|
|
1289
966
|
},
|
|
1290
|
-
],
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
967
|
+
],
|
|
968
|
+
});
|
|
969
|
+
}
|
|
970
|
+
_addCheck(check) {
|
|
971
|
+
return new ZodNumber({
|
|
972
|
+
...this._def,
|
|
973
|
+
checks: [...this._def.checks, check],
|
|
974
|
+
});
|
|
975
|
+
}
|
|
976
|
+
int(message) {
|
|
1296
977
|
return this._addCheck({
|
|
1297
978
|
kind: "int",
|
|
1298
979
|
message: errorUtil.toString(message),
|
|
1299
980
|
});
|
|
1300
|
-
}
|
|
1301
|
-
|
|
981
|
+
}
|
|
982
|
+
positive(message) {
|
|
1302
983
|
return this._addCheck({
|
|
1303
984
|
kind: "min",
|
|
1304
985
|
value: 0,
|
|
1305
986
|
inclusive: false,
|
|
1306
987
|
message: errorUtil.toString(message),
|
|
1307
988
|
});
|
|
1308
|
-
}
|
|
1309
|
-
|
|
989
|
+
}
|
|
990
|
+
negative(message) {
|
|
1310
991
|
return this._addCheck({
|
|
1311
992
|
kind: "max",
|
|
1312
993
|
value: 0,
|
|
1313
994
|
inclusive: false,
|
|
1314
995
|
message: errorUtil.toString(message),
|
|
1315
996
|
});
|
|
1316
|
-
}
|
|
1317
|
-
|
|
997
|
+
}
|
|
998
|
+
nonpositive(message) {
|
|
1318
999
|
return this._addCheck({
|
|
1319
1000
|
kind: "max",
|
|
1320
1001
|
value: 0,
|
|
1321
1002
|
inclusive: true,
|
|
1322
1003
|
message: errorUtil.toString(message),
|
|
1323
1004
|
});
|
|
1324
|
-
}
|
|
1325
|
-
|
|
1005
|
+
}
|
|
1006
|
+
nonnegative(message) {
|
|
1326
1007
|
return this._addCheck({
|
|
1327
1008
|
kind: "min",
|
|
1328
1009
|
value: 0,
|
|
1329
1010
|
inclusive: true,
|
|
1330
1011
|
message: errorUtil.toString(message),
|
|
1331
1012
|
});
|
|
1332
|
-
}
|
|
1333
|
-
|
|
1013
|
+
}
|
|
1014
|
+
multipleOf(value, message) {
|
|
1334
1015
|
return this._addCheck({
|
|
1335
1016
|
kind: "multipleOf",
|
|
1336
1017
|
value: value,
|
|
1337
1018
|
message: errorUtil.toString(message),
|
|
1338
1019
|
});
|
|
1339
|
-
}
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
var ch = _c.value;
|
|
1347
|
-
if (ch.kind === "min") {
|
|
1348
|
-
if (min === null || ch.value > min)
|
|
1349
|
-
min = ch.value;
|
|
1350
|
-
}
|
|
1351
|
-
}
|
|
1352
|
-
}
|
|
1353
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
1354
|
-
finally {
|
|
1355
|
-
try {
|
|
1356
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
1357
|
-
}
|
|
1358
|
-
finally { if (e_3) throw e_3.error; }
|
|
1359
|
-
}
|
|
1360
|
-
return min;
|
|
1361
|
-
},
|
|
1362
|
-
enumerable: false,
|
|
1363
|
-
configurable: true
|
|
1364
|
-
});
|
|
1365
|
-
Object.defineProperty(ZodNumber.prototype, "maxValue", {
|
|
1366
|
-
get: function () {
|
|
1367
|
-
var e_4, _a;
|
|
1368
|
-
var max = null;
|
|
1369
|
-
try {
|
|
1370
|
-
for (var _b = __values(this._def.checks), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
1371
|
-
var ch = _c.value;
|
|
1372
|
-
if (ch.kind === "max") {
|
|
1373
|
-
if (max === null || ch.value < max)
|
|
1374
|
-
max = ch.value;
|
|
1375
|
-
}
|
|
1376
|
-
}
|
|
1020
|
+
}
|
|
1021
|
+
get minValue() {
|
|
1022
|
+
let min = null;
|
|
1023
|
+
for (const ch of this._def.checks) {
|
|
1024
|
+
if (ch.kind === "min") {
|
|
1025
|
+
if (min === null || ch.value > min)
|
|
1026
|
+
min = ch.value;
|
|
1377
1027
|
}
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1028
|
+
}
|
|
1029
|
+
return min;
|
|
1030
|
+
}
|
|
1031
|
+
get maxValue() {
|
|
1032
|
+
let max = null;
|
|
1033
|
+
for (const ch of this._def.checks) {
|
|
1034
|
+
if (ch.kind === "max") {
|
|
1035
|
+
if (max === null || ch.value < max)
|
|
1036
|
+
max = ch.value;
|
|
1384
1037
|
}
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1038
|
+
}
|
|
1039
|
+
return max;
|
|
1040
|
+
}
|
|
1041
|
+
get isInt() {
|
|
1042
|
+
return !!this._def.checks.find((ch) => ch.kind === "int");
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
ZodNumber.create = (params) => {
|
|
1046
|
+
return new ZodNumber({
|
|
1047
|
+
checks: [],
|
|
1048
|
+
typeName: ZodFirstPartyTypeKind.ZodNumber,
|
|
1049
|
+
...processCreateParams(params),
|
|
1396
1050
|
});
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
}(ZodType));
|
|
1402
|
-
var ZodBigInt = /** @class */ (function (_super) {
|
|
1403
|
-
__extends(ZodBigInt, _super);
|
|
1404
|
-
function ZodBigInt() {
|
|
1405
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
1406
|
-
}
|
|
1407
|
-
ZodBigInt.prototype._parse = function (input) {
|
|
1408
|
-
var ctx = this._processInputParams(input).ctx;
|
|
1051
|
+
};
|
|
1052
|
+
class ZodBigInt extends ZodType {
|
|
1053
|
+
_parse(input) {
|
|
1054
|
+
const { ctx } = this._processInputParams(input);
|
|
1409
1055
|
if (ctx.parsedType !== ZodParsedType.bigint) {
|
|
1410
1056
|
addIssueToContext(ctx, {
|
|
1411
1057
|
code: ZodIssueCode.invalid_type,
|
|
@@ -1415,19 +1061,17 @@ var ZodBigInt = /** @class */ (function (_super) {
|
|
|
1415
1061
|
return INVALID;
|
|
1416
1062
|
}
|
|
1417
1063
|
return OK(ctx.data);
|
|
1418
|
-
}
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
ZodBoolean.prototype._parse = function (input) {
|
|
1430
|
-
var ctx = this._processInputParams(input).ctx;
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
ZodBigInt.create = (params) => {
|
|
1067
|
+
return new ZodBigInt({
|
|
1068
|
+
typeName: ZodFirstPartyTypeKind.ZodBigInt,
|
|
1069
|
+
...processCreateParams(params),
|
|
1070
|
+
});
|
|
1071
|
+
};
|
|
1072
|
+
class ZodBoolean extends ZodType {
|
|
1073
|
+
_parse(input) {
|
|
1074
|
+
const { ctx } = this._processInputParams(input);
|
|
1431
1075
|
if (ctx.parsedType !== ZodParsedType.boolean) {
|
|
1432
1076
|
addIssueToContext(ctx, {
|
|
1433
1077
|
code: ZodIssueCode.invalid_type,
|
|
@@ -1437,19 +1081,17 @@ var ZodBoolean = /** @class */ (function (_super) {
|
|
|
1437
1081
|
return INVALID;
|
|
1438
1082
|
}
|
|
1439
1083
|
return OK(ctx.data);
|
|
1440
|
-
}
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
ZodDate.prototype._parse = function (input) {
|
|
1452
|
-
var _a = this._processInputParams(input), status = _a.status, ctx = _a.ctx;
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
ZodBoolean.create = (params) => {
|
|
1087
|
+
return new ZodBoolean({
|
|
1088
|
+
typeName: ZodFirstPartyTypeKind.ZodBoolean,
|
|
1089
|
+
...processCreateParams(params),
|
|
1090
|
+
});
|
|
1091
|
+
};
|
|
1092
|
+
class ZodDate extends ZodType {
|
|
1093
|
+
_parse(input) {
|
|
1094
|
+
const { status, ctx } = this._processInputParams(input);
|
|
1453
1095
|
if (ctx.parsedType !== ZodParsedType.date) {
|
|
1454
1096
|
addIssueToContext(ctx, {
|
|
1455
1097
|
code: ZodIssueCode.invalid_type,
|
|
@@ -1468,19 +1110,17 @@ var ZodDate = /** @class */ (function (_super) {
|
|
|
1468
1110
|
status: status.value,
|
|
1469
1111
|
value: new Date(ctx.data.getTime()),
|
|
1470
1112
|
};
|
|
1471
|
-
}
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
ZodUndefined.prototype._parse = function (input) {
|
|
1483
|
-
var ctx = this._processInputParams(input).ctx;
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
ZodDate.create = (params) => {
|
|
1116
|
+
return new ZodDate({
|
|
1117
|
+
typeName: ZodFirstPartyTypeKind.ZodDate,
|
|
1118
|
+
...processCreateParams(params),
|
|
1119
|
+
});
|
|
1120
|
+
};
|
|
1121
|
+
class ZodUndefined extends ZodType {
|
|
1122
|
+
_parse(input) {
|
|
1123
|
+
const { ctx } = this._processInputParams(input);
|
|
1484
1124
|
if (ctx.parsedType !== ZodParsedType.undefined) {
|
|
1485
1125
|
addIssueToContext(ctx, {
|
|
1486
1126
|
code: ZodIssueCode.invalid_type,
|
|
@@ -1490,19 +1130,17 @@ var ZodUndefined = /** @class */ (function (_super) {
|
|
|
1490
1130
|
return INVALID;
|
|
1491
1131
|
}
|
|
1492
1132
|
return OK(ctx.data);
|
|
1493
|
-
}
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
ZodNull.prototype._parse = function (input) {
|
|
1505
|
-
var ctx = this._processInputParams(input).ctx;
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
ZodUndefined.create = (params) => {
|
|
1136
|
+
return new ZodUndefined({
|
|
1137
|
+
typeName: ZodFirstPartyTypeKind.ZodUndefined,
|
|
1138
|
+
...processCreateParams(params),
|
|
1139
|
+
});
|
|
1140
|
+
};
|
|
1141
|
+
class ZodNull extends ZodType {
|
|
1142
|
+
_parse(input) {
|
|
1143
|
+
const { ctx } = this._processInputParams(input);
|
|
1506
1144
|
if (ctx.parsedType !== ZodParsedType.null) {
|
|
1507
1145
|
addIssueToContext(ctx, {
|
|
1508
1146
|
code: ZodIssueCode.invalid_type,
|
|
@@ -1512,72 +1150,68 @@ var ZodNull = /** @class */ (function (_super) {
|
|
|
1512
1150
|
return INVALID;
|
|
1513
1151
|
}
|
|
1514
1152
|
return OK(ctx.data);
|
|
1515
|
-
}
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
ZodNull.create = (params) => {
|
|
1156
|
+
return new ZodNull({
|
|
1157
|
+
typeName: ZodFirstPartyTypeKind.ZodNull,
|
|
1158
|
+
...processCreateParams(params),
|
|
1159
|
+
});
|
|
1160
|
+
};
|
|
1161
|
+
class ZodAny extends ZodType {
|
|
1162
|
+
constructor() {
|
|
1163
|
+
super(...arguments);
|
|
1525
1164
|
// to prevent instances of other classes from extending ZodAny. this causes issues with catchall in ZodObject.
|
|
1526
|
-
|
|
1527
|
-
return _this;
|
|
1165
|
+
this._any = true;
|
|
1528
1166
|
}
|
|
1529
|
-
|
|
1530
|
-
|
|
1167
|
+
_parse(input) {
|
|
1168
|
+
const { ctx } = this._processInputParams(input);
|
|
1531
1169
|
return OK(ctx.data);
|
|
1532
|
-
}
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
ZodAny.create = (params) => {
|
|
1173
|
+
return new ZodAny({
|
|
1174
|
+
typeName: ZodFirstPartyTypeKind.ZodAny,
|
|
1175
|
+
...processCreateParams(params),
|
|
1176
|
+
});
|
|
1177
|
+
};
|
|
1178
|
+
class ZodUnknown extends ZodType {
|
|
1179
|
+
constructor() {
|
|
1180
|
+
super(...arguments);
|
|
1542
1181
|
// required
|
|
1543
|
-
|
|
1544
|
-
return _this;
|
|
1182
|
+
this._unknown = true;
|
|
1545
1183
|
}
|
|
1546
|
-
|
|
1547
|
-
|
|
1184
|
+
_parse(input) {
|
|
1185
|
+
const { ctx } = this._processInputParams(input);
|
|
1548
1186
|
return OK(ctx.data);
|
|
1549
|
-
}
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
ZodNever.prototype._parse = function (input) {
|
|
1561
|
-
var ctx = this._processInputParams(input).ctx;
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
ZodUnknown.create = (params) => {
|
|
1190
|
+
return new ZodUnknown({
|
|
1191
|
+
typeName: ZodFirstPartyTypeKind.ZodUnknown,
|
|
1192
|
+
...processCreateParams(params),
|
|
1193
|
+
});
|
|
1194
|
+
};
|
|
1195
|
+
class ZodNever extends ZodType {
|
|
1196
|
+
_parse(input) {
|
|
1197
|
+
const { ctx } = this._processInputParams(input);
|
|
1562
1198
|
addIssueToContext(ctx, {
|
|
1563
1199
|
code: ZodIssueCode.invalid_type,
|
|
1564
1200
|
expected: ZodParsedType.never,
|
|
1565
1201
|
received: ctx.parsedType,
|
|
1566
1202
|
});
|
|
1567
1203
|
return INVALID;
|
|
1568
|
-
}
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
ZodVoid.prototype._parse = function (input) {
|
|
1580
|
-
var ctx = this._processInputParams(input).ctx;
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
ZodNever.create = (params) => {
|
|
1207
|
+
return new ZodNever({
|
|
1208
|
+
typeName: ZodFirstPartyTypeKind.ZodNever,
|
|
1209
|
+
...processCreateParams(params),
|
|
1210
|
+
});
|
|
1211
|
+
};
|
|
1212
|
+
class ZodVoid extends ZodType {
|
|
1213
|
+
_parse(input) {
|
|
1214
|
+
const { ctx } = this._processInputParams(input);
|
|
1581
1215
|
if (ctx.parsedType !== ZodParsedType.undefined) {
|
|
1582
1216
|
addIssueToContext(ctx, {
|
|
1583
1217
|
code: ZodIssueCode.invalid_type,
|
|
@@ -1587,20 +1221,18 @@ var ZodVoid = /** @class */ (function (_super) {
|
|
|
1587
1221
|
return INVALID;
|
|
1588
1222
|
}
|
|
1589
1223
|
return OK(ctx.data);
|
|
1590
|
-
}
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
var _a = this._processInputParams(input), status = _a.status, ctx = _a.ctx;
|
|
1603
|
-
var def = this._def;
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
ZodVoid.create = (params) => {
|
|
1227
|
+
return new ZodVoid({
|
|
1228
|
+
typeName: ZodFirstPartyTypeKind.ZodVoid,
|
|
1229
|
+
...processCreateParams(params),
|
|
1230
|
+
});
|
|
1231
|
+
};
|
|
1232
|
+
class ZodArray extends ZodType {
|
|
1233
|
+
_parse(input) {
|
|
1234
|
+
const { status, ctx } = this._processInputParams(input);
|
|
1235
|
+
const def = this._def;
|
|
1604
1236
|
if (ctx.parsedType !== ZodParsedType.array) {
|
|
1605
1237
|
addIssueToContext(ctx, {
|
|
1606
1238
|
code: ZodIssueCode.invalid_type,
|
|
@@ -1634,49 +1266,56 @@ var ZodArray = /** @class */ (function (_super) {
|
|
|
1634
1266
|
}
|
|
1635
1267
|
}
|
|
1636
1268
|
if (ctx.async) {
|
|
1637
|
-
return Promise.all(ctx.data.map(
|
|
1269
|
+
return Promise.all(ctx.data.map((item, i) => {
|
|
1638
1270
|
return def.type._parseAsync({
|
|
1639
1271
|
parent: ctx,
|
|
1640
|
-
path:
|
|
1272
|
+
path: [...ctx.path, i],
|
|
1641
1273
|
data: item,
|
|
1642
1274
|
});
|
|
1643
|
-
})).then(
|
|
1275
|
+
})).then((result) => {
|
|
1644
1276
|
return ParseStatus.mergeArray(status, result);
|
|
1645
1277
|
});
|
|
1646
1278
|
}
|
|
1647
|
-
|
|
1279
|
+
const result = ctx.data.map((item, i) => {
|
|
1648
1280
|
return def.type._parseSync({
|
|
1649
1281
|
parent: ctx,
|
|
1650
|
-
path:
|
|
1282
|
+
path: [...ctx.path, i],
|
|
1651
1283
|
data: item,
|
|
1652
1284
|
});
|
|
1653
1285
|
});
|
|
1654
1286
|
return ParseStatus.mergeArray(status, result);
|
|
1655
|
-
}
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1287
|
+
}
|
|
1288
|
+
get element() {
|
|
1289
|
+
return this._def.type;
|
|
1290
|
+
}
|
|
1291
|
+
min(minLength, message) {
|
|
1292
|
+
return new ZodArray({
|
|
1293
|
+
...this._def,
|
|
1294
|
+
minLength: { value: minLength, message: errorUtil.toString(message) },
|
|
1295
|
+
});
|
|
1296
|
+
}
|
|
1297
|
+
max(maxLength, message) {
|
|
1298
|
+
return new ZodArray({
|
|
1299
|
+
...this._def,
|
|
1300
|
+
maxLength: { value: maxLength, message: errorUtil.toString(message) },
|
|
1301
|
+
});
|
|
1302
|
+
}
|
|
1303
|
+
length(len, message) {
|
|
1670
1304
|
return this.min(len, message).max(len, message);
|
|
1671
|
-
}
|
|
1672
|
-
|
|
1305
|
+
}
|
|
1306
|
+
nonempty(message) {
|
|
1673
1307
|
return this.min(1, message);
|
|
1674
|
-
}
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
ZodArray.create = (schema, params) => {
|
|
1311
|
+
return new ZodArray({
|
|
1312
|
+
type: schema,
|
|
1313
|
+
minLength: null,
|
|
1314
|
+
maxLength: null,
|
|
1315
|
+
typeName: ZodFirstPartyTypeKind.ZodArray,
|
|
1316
|
+
...processCreateParams(params),
|
|
1317
|
+
});
|
|
1318
|
+
};
|
|
1680
1319
|
/////////////////////////////////////////
|
|
1681
1320
|
/////////////////////////////////////////
|
|
1682
1321
|
////////// //////////
|
|
@@ -1686,21 +1325,33 @@ var ZodArray = /** @class */ (function (_super) {
|
|
|
1686
1325
|
/////////////////////////////////////////
|
|
1687
1326
|
var objectUtil;
|
|
1688
1327
|
(function (objectUtil) {
|
|
1689
|
-
objectUtil.mergeShapes =
|
|
1690
|
-
return
|
|
1328
|
+
objectUtil.mergeShapes = (first, second) => {
|
|
1329
|
+
return {
|
|
1330
|
+
...first,
|
|
1331
|
+
...second, // second overwrites first
|
|
1332
|
+
};
|
|
1691
1333
|
};
|
|
1692
1334
|
})(objectUtil || (objectUtil = {}));
|
|
1693
|
-
|
|
1694
|
-
return new ZodObject(
|
|
1695
|
-
|
|
1335
|
+
const AugmentFactory = (def) => (augmentation) => {
|
|
1336
|
+
return new ZodObject({
|
|
1337
|
+
...def,
|
|
1338
|
+
shape: () => ({
|
|
1339
|
+
...def.shape(),
|
|
1340
|
+
...augmentation,
|
|
1341
|
+
}),
|
|
1342
|
+
});
|
|
1343
|
+
};
|
|
1696
1344
|
function deepPartialify(schema) {
|
|
1697
1345
|
if (schema instanceof ZodObject) {
|
|
1698
|
-
|
|
1699
|
-
for (
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
}
|
|
1703
|
-
return new ZodObject(
|
|
1346
|
+
const newShape = {};
|
|
1347
|
+
for (const key in schema.shape) {
|
|
1348
|
+
const fieldSchema = schema.shape[key];
|
|
1349
|
+
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema));
|
|
1350
|
+
}
|
|
1351
|
+
return new ZodObject({
|
|
1352
|
+
...schema._def,
|
|
1353
|
+
shape: () => newShape,
|
|
1354
|
+
});
|
|
1704
1355
|
}
|
|
1705
1356
|
else if (schema instanceof ZodArray) {
|
|
1706
1357
|
return ZodArray.create(deepPartialify(schema.element));
|
|
@@ -1712,37 +1363,33 @@ function deepPartialify(schema) {
|
|
|
1712
1363
|
return ZodNullable.create(deepPartialify(schema.unwrap()));
|
|
1713
1364
|
}
|
|
1714
1365
|
else if (schema instanceof ZodTuple) {
|
|
1715
|
-
return ZodTuple.create(schema.items.map(
|
|
1366
|
+
return ZodTuple.create(schema.items.map((item) => deepPartialify(item)));
|
|
1716
1367
|
}
|
|
1717
1368
|
else {
|
|
1718
1369
|
return schema;
|
|
1719
1370
|
}
|
|
1720
1371
|
}
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
_this._cached = null;
|
|
1372
|
+
class ZodObject extends ZodType {
|
|
1373
|
+
constructor() {
|
|
1374
|
+
super(...arguments);
|
|
1375
|
+
this._cached = null;
|
|
1726
1376
|
/**
|
|
1727
1377
|
* @deprecated In most cases, this is no longer needed - unknown properties are now silently stripped.
|
|
1728
1378
|
* If you want to pass through unknown properties, use `.passthrough()` instead.
|
|
1729
1379
|
*/
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
return _this;
|
|
1380
|
+
this.nonstrict = this.passthrough;
|
|
1381
|
+
this.augment = AugmentFactory(this._def);
|
|
1382
|
+
this.extend = AugmentFactory(this._def);
|
|
1734
1383
|
}
|
|
1735
|
-
|
|
1384
|
+
_getCached() {
|
|
1736
1385
|
if (this._cached !== null)
|
|
1737
1386
|
return this._cached;
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
return (this._cached = { shape
|
|
1741
|
-
}
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
var _this = this;
|
|
1745
|
-
var _d = this._processInputParams(input), status = _d.status, ctx = _d.ctx;
|
|
1387
|
+
const shape = this._def.shape();
|
|
1388
|
+
const keys = util.objectKeys(shape);
|
|
1389
|
+
return (this._cached = { shape, keys });
|
|
1390
|
+
}
|
|
1391
|
+
_parse(input) {
|
|
1392
|
+
const { status, ctx } = this._processInputParams(input);
|
|
1746
1393
|
if (ctx.parsedType !== ZodParsedType.object) {
|
|
1747
1394
|
addIssueToContext(ctx, {
|
|
1748
1395
|
code: ZodIssueCode.invalid_type,
|
|
@@ -1751,51 +1398,31 @@ var ZodObject = /** @class */ (function (_super) {
|
|
|
1751
1398
|
});
|
|
1752
1399
|
return INVALID;
|
|
1753
1400
|
}
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
alwaysSet: key in ctx.data,
|
|
1771
|
-
});
|
|
1772
|
-
}
|
|
1773
|
-
}
|
|
1774
|
-
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
1775
|
-
finally {
|
|
1776
|
-
try {
|
|
1777
|
-
if (shapeKeys_1_1 && !shapeKeys_1_1.done && (_a = shapeKeys_1.return)) _a.call(shapeKeys_1);
|
|
1778
|
-
}
|
|
1779
|
-
finally { if (e_5) throw e_5.error; }
|
|
1401
|
+
const { shape, keys: shapeKeys } = this._getCached();
|
|
1402
|
+
const dataKeys = util.objectKeys(ctx.data);
|
|
1403
|
+
const extraKeys = dataKeys.filter((k) => !shapeKeys.includes(k));
|
|
1404
|
+
const pairs = [];
|
|
1405
|
+
for (const key of shapeKeys) {
|
|
1406
|
+
const keyValidator = shape[key];
|
|
1407
|
+
const value = ctx.data[key];
|
|
1408
|
+
pairs.push({
|
|
1409
|
+
key: { status: "valid", value: key },
|
|
1410
|
+
value: keyValidator._parse({
|
|
1411
|
+
parent: ctx,
|
|
1412
|
+
data: value,
|
|
1413
|
+
path: [...ctx.path, key],
|
|
1414
|
+
}),
|
|
1415
|
+
alwaysSet: key in ctx.data,
|
|
1416
|
+
});
|
|
1780
1417
|
}
|
|
1781
1418
|
if (this._def.catchall instanceof ZodNever) {
|
|
1782
|
-
|
|
1419
|
+
const unknownKeys = this._def.unknownKeys;
|
|
1783
1420
|
if (unknownKeys === "passthrough") {
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
value: { status: "valid", value: ctx.data[key] },
|
|
1790
|
-
});
|
|
1791
|
-
}
|
|
1792
|
-
}
|
|
1793
|
-
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
1794
|
-
finally {
|
|
1795
|
-
try {
|
|
1796
|
-
if (extraKeys_1_1 && !extraKeys_1_1.done && (_b = extraKeys_1.return)) _b.call(extraKeys_1);
|
|
1797
|
-
}
|
|
1798
|
-
finally { if (e_6) throw e_6.error; }
|
|
1421
|
+
for (const key of extraKeys) {
|
|
1422
|
+
pairs.push({
|
|
1423
|
+
key: { status: "valid", value: key },
|
|
1424
|
+
value: { status: "valid", value: ctx.data[key] },
|
|
1425
|
+
});
|
|
1799
1426
|
}
|
|
1800
1427
|
}
|
|
1801
1428
|
else if (unknownKeys === "strict") {
|
|
@@ -1809,321 +1436,296 @@ var ZodObject = /** @class */ (function (_super) {
|
|
|
1809
1436
|
}
|
|
1810
1437
|
else if (unknownKeys === "strip") ;
|
|
1811
1438
|
else {
|
|
1812
|
-
throw new Error(
|
|
1439
|
+
throw new Error(`Internal ZodObject error: invalid unknownKeys value.`);
|
|
1813
1440
|
}
|
|
1814
1441
|
}
|
|
1815
1442
|
else {
|
|
1816
1443
|
// run catchall validation
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
alwaysSet: key in ctx.data,
|
|
1827
|
-
});
|
|
1828
|
-
}
|
|
1829
|
-
}
|
|
1830
|
-
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
1831
|
-
finally {
|
|
1832
|
-
try {
|
|
1833
|
-
if (extraKeys_2_1 && !extraKeys_2_1.done && (_c = extraKeys_2.return)) _c.call(extraKeys_2);
|
|
1834
|
-
}
|
|
1835
|
-
finally { if (e_7) throw e_7.error; }
|
|
1444
|
+
const catchall = this._def.catchall;
|
|
1445
|
+
for (const key of extraKeys) {
|
|
1446
|
+
const value = ctx.data[key];
|
|
1447
|
+
pairs.push({
|
|
1448
|
+
key: { status: "valid", value: key },
|
|
1449
|
+
value: catchall._parse({ parent: ctx, path: [...ctx.path, key], data: value } //, ctx.child(key), value, getParsedType(value)
|
|
1450
|
+
),
|
|
1451
|
+
alwaysSet: key in ctx.data,
|
|
1452
|
+
});
|
|
1836
1453
|
}
|
|
1837
1454
|
}
|
|
1838
1455
|
if (ctx.async) {
|
|
1839
1456
|
return Promise.resolve()
|
|
1840
|
-
.then(
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
if (!!pairs_1_1.done) return [3 /*break*/, 6];
|
|
1854
|
-
pair = pairs_1_1.value;
|
|
1855
|
-
return [4 /*yield*/, pair.key];
|
|
1856
|
-
case 3:
|
|
1857
|
-
key = _e.sent();
|
|
1858
|
-
_b = (_a = syncPairs).push;
|
|
1859
|
-
_d = {
|
|
1860
|
-
key: key
|
|
1861
|
-
};
|
|
1862
|
-
return [4 /*yield*/, pair.value];
|
|
1863
|
-
case 4:
|
|
1864
|
-
_b.apply(_a, [(_d.value = _e.sent(),
|
|
1865
|
-
_d.alwaysSet = pair.alwaysSet,
|
|
1866
|
-
_d)]);
|
|
1867
|
-
_e.label = 5;
|
|
1868
|
-
case 5:
|
|
1869
|
-
pairs_1_1 = pairs_1.next();
|
|
1870
|
-
return [3 /*break*/, 2];
|
|
1871
|
-
case 6: return [3 /*break*/, 9];
|
|
1872
|
-
case 7:
|
|
1873
|
-
e_8_1 = _e.sent();
|
|
1874
|
-
e_8 = { error: e_8_1 };
|
|
1875
|
-
return [3 /*break*/, 9];
|
|
1876
|
-
case 8:
|
|
1877
|
-
try {
|
|
1878
|
-
if (pairs_1_1 && !pairs_1_1.done && (_c = pairs_1.return)) _c.call(pairs_1);
|
|
1879
|
-
}
|
|
1880
|
-
finally { if (e_8) throw e_8.error; }
|
|
1881
|
-
return [7 /*endfinally*/];
|
|
1882
|
-
case 9: return [2 /*return*/, syncPairs];
|
|
1883
|
-
}
|
|
1884
|
-
});
|
|
1885
|
-
}); })
|
|
1886
|
-
.then(function (syncPairs) {
|
|
1457
|
+
.then(async () => {
|
|
1458
|
+
const syncPairs = [];
|
|
1459
|
+
for (const pair of pairs) {
|
|
1460
|
+
const key = await pair.key;
|
|
1461
|
+
syncPairs.push({
|
|
1462
|
+
key,
|
|
1463
|
+
value: await pair.value,
|
|
1464
|
+
alwaysSet: pair.alwaysSet,
|
|
1465
|
+
});
|
|
1466
|
+
}
|
|
1467
|
+
return syncPairs;
|
|
1468
|
+
})
|
|
1469
|
+
.then((syncPairs) => {
|
|
1887
1470
|
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
1888
1471
|
});
|
|
1889
1472
|
}
|
|
1890
1473
|
else {
|
|
1891
1474
|
return ParseStatus.mergeObjectSync(status, pairs);
|
|
1892
1475
|
}
|
|
1893
|
-
}
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
enumerable: false,
|
|
1899
|
-
configurable: true
|
|
1900
|
-
});
|
|
1901
|
-
ZodObject.prototype.strict = function (message) {
|
|
1902
|
-
var _this = this;
|
|
1476
|
+
}
|
|
1477
|
+
get shape() {
|
|
1478
|
+
return this._def.shape();
|
|
1479
|
+
}
|
|
1480
|
+
strict(message) {
|
|
1903
1481
|
errorUtil.errToObj;
|
|
1904
|
-
return new ZodObject(
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1482
|
+
return new ZodObject({
|
|
1483
|
+
...this._def,
|
|
1484
|
+
unknownKeys: "strict",
|
|
1485
|
+
...(message !== undefined
|
|
1486
|
+
? {
|
|
1487
|
+
errorMap: (issue, ctx) => {
|
|
1488
|
+
var _a, _b, _c, _d;
|
|
1489
|
+
const defaultError = (_c = (_b = (_a = this._def).errorMap) === null || _b === void 0 ? void 0 : _b.call(_a, issue, ctx).message) !== null && _c !== void 0 ? _c : ctx.defaultError;
|
|
1490
|
+
if (issue.code === "unrecognized_keys")
|
|
1491
|
+
return {
|
|
1492
|
+
message: (_d = errorUtil.errToObj(message).message) !== null && _d !== void 0 ? _d : defaultError,
|
|
1493
|
+
};
|
|
1910
1494
|
return {
|
|
1911
|
-
message:
|
|
1495
|
+
message: defaultError,
|
|
1912
1496
|
};
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
}
|
|
1497
|
+
},
|
|
1498
|
+
}
|
|
1499
|
+
: {}),
|
|
1500
|
+
});
|
|
1501
|
+
}
|
|
1502
|
+
strip() {
|
|
1503
|
+
return new ZodObject({
|
|
1504
|
+
...this._def,
|
|
1505
|
+
unknownKeys: "strip",
|
|
1506
|
+
});
|
|
1507
|
+
}
|
|
1508
|
+
passthrough() {
|
|
1509
|
+
return new ZodObject({
|
|
1510
|
+
...this._def,
|
|
1511
|
+
unknownKeys: "passthrough",
|
|
1512
|
+
});
|
|
1513
|
+
}
|
|
1514
|
+
setKey(key, schema) {
|
|
1515
|
+
return this.augment({ [key]: schema });
|
|
1516
|
+
}
|
|
1930
1517
|
/**
|
|
1931
1518
|
* Prior to zod@1.0.12 there was a bug in the
|
|
1932
1519
|
* inferred type of merged objects. Please
|
|
1933
1520
|
* upgrade if you are experiencing issues.
|
|
1934
1521
|
*/
|
|
1935
|
-
|
|
1936
|
-
var _this = this;
|
|
1522
|
+
merge(merging) {
|
|
1937
1523
|
// const mergedShape = objectUtil.mergeShapes(
|
|
1938
1524
|
// this._def.shape(),
|
|
1939
1525
|
// merging._def.shape()
|
|
1940
1526
|
// );
|
|
1941
|
-
|
|
1527
|
+
const merged = new ZodObject({
|
|
1942
1528
|
unknownKeys: merging._def.unknownKeys,
|
|
1943
1529
|
catchall: merging._def.catchall,
|
|
1944
|
-
shape:
|
|
1945
|
-
return objectUtil.mergeShapes(_this._def.shape(), merging._def.shape());
|
|
1946
|
-
},
|
|
1530
|
+
shape: () => objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
1947
1531
|
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
1948
1532
|
});
|
|
1949
1533
|
return merged;
|
|
1950
|
-
}
|
|
1951
|
-
|
|
1952
|
-
return new ZodObject(
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
var _this = this;
|
|
1956
|
-
var shape = {};
|
|
1957
|
-
util.objectKeys(mask).map(function (key) {
|
|
1958
|
-
shape[key] = _this.shape[key];
|
|
1534
|
+
}
|
|
1535
|
+
catchall(index) {
|
|
1536
|
+
return new ZodObject({
|
|
1537
|
+
...this._def,
|
|
1538
|
+
catchall: index,
|
|
1959
1539
|
});
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1540
|
+
}
|
|
1541
|
+
pick(mask) {
|
|
1542
|
+
const shape = {};
|
|
1543
|
+
util.objectKeys(mask).map((key) => {
|
|
1544
|
+
shape[key] = this.shape[key];
|
|
1545
|
+
});
|
|
1546
|
+
return new ZodObject({
|
|
1547
|
+
...this._def,
|
|
1548
|
+
shape: () => shape,
|
|
1549
|
+
});
|
|
1550
|
+
}
|
|
1551
|
+
omit(mask) {
|
|
1552
|
+
const shape = {};
|
|
1553
|
+
util.objectKeys(this.shape).map((key) => {
|
|
1966
1554
|
if (util.objectKeys(mask).indexOf(key) === -1) {
|
|
1967
|
-
shape[key] =
|
|
1555
|
+
shape[key] = this.shape[key];
|
|
1968
1556
|
}
|
|
1969
1557
|
});
|
|
1970
|
-
return new ZodObject(
|
|
1971
|
-
|
|
1972
|
-
|
|
1558
|
+
return new ZodObject({
|
|
1559
|
+
...this._def,
|
|
1560
|
+
shape: () => shape,
|
|
1561
|
+
});
|
|
1562
|
+
}
|
|
1563
|
+
deepPartial() {
|
|
1973
1564
|
return deepPartialify(this);
|
|
1974
|
-
}
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
var newShape = {};
|
|
1565
|
+
}
|
|
1566
|
+
partial(mask) {
|
|
1567
|
+
const newShape = {};
|
|
1978
1568
|
if (mask) {
|
|
1979
|
-
util.objectKeys(this.shape).map(
|
|
1569
|
+
util.objectKeys(this.shape).map((key) => {
|
|
1980
1570
|
if (util.objectKeys(mask).indexOf(key) === -1) {
|
|
1981
|
-
newShape[key] =
|
|
1571
|
+
newShape[key] = this.shape[key];
|
|
1982
1572
|
}
|
|
1983
1573
|
else {
|
|
1984
|
-
newShape[key] =
|
|
1574
|
+
newShape[key] = this.shape[key].optional();
|
|
1985
1575
|
}
|
|
1986
1576
|
});
|
|
1987
|
-
return new ZodObject(
|
|
1577
|
+
return new ZodObject({
|
|
1578
|
+
...this._def,
|
|
1579
|
+
shape: () => newShape,
|
|
1580
|
+
});
|
|
1988
1581
|
}
|
|
1989
1582
|
else {
|
|
1990
|
-
for (
|
|
1991
|
-
|
|
1583
|
+
for (const key in this.shape) {
|
|
1584
|
+
const fieldSchema = this.shape[key];
|
|
1992
1585
|
newShape[key] = fieldSchema.optional();
|
|
1993
1586
|
}
|
|
1994
1587
|
}
|
|
1995
|
-
return new ZodObject(
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
1588
|
+
return new ZodObject({
|
|
1589
|
+
...this._def,
|
|
1590
|
+
shape: () => newShape,
|
|
1591
|
+
});
|
|
1592
|
+
}
|
|
1593
|
+
required() {
|
|
1594
|
+
const newShape = {};
|
|
1595
|
+
for (const key in this.shape) {
|
|
1596
|
+
const fieldSchema = this.shape[key];
|
|
1597
|
+
let newField = fieldSchema;
|
|
2002
1598
|
while (newField instanceof ZodOptional) {
|
|
2003
1599
|
newField = newField._def.innerType;
|
|
2004
1600
|
}
|
|
2005
1601
|
newShape[key] = newField;
|
|
2006
1602
|
}
|
|
2007
|
-
return new ZodObject(
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
}
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
1603
|
+
return new ZodObject({
|
|
1604
|
+
...this._def,
|
|
1605
|
+
shape: () => newShape,
|
|
1606
|
+
});
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
ZodObject.create = (shape, params) => {
|
|
1610
|
+
return new ZodObject({
|
|
1611
|
+
shape: () => shape,
|
|
1612
|
+
unknownKeys: "strip",
|
|
1613
|
+
catchall: ZodNever.create(),
|
|
1614
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
1615
|
+
...processCreateParams(params),
|
|
1616
|
+
});
|
|
1617
|
+
};
|
|
1618
|
+
ZodObject.strictCreate = (shape, params) => {
|
|
1619
|
+
return new ZodObject({
|
|
1620
|
+
shape: () => shape,
|
|
1621
|
+
unknownKeys: "strict",
|
|
1622
|
+
catchall: ZodNever.create(),
|
|
1623
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
1624
|
+
...processCreateParams(params),
|
|
1625
|
+
});
|
|
1626
|
+
};
|
|
1627
|
+
ZodObject.lazycreate = (shape, params) => {
|
|
1628
|
+
return new ZodObject({
|
|
1629
|
+
shape,
|
|
1630
|
+
unknownKeys: "strip",
|
|
1631
|
+
catchall: ZodNever.create(),
|
|
1632
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
1633
|
+
...processCreateParams(params),
|
|
1634
|
+
});
|
|
1635
|
+
};
|
|
1636
|
+
class ZodUnion extends ZodType {
|
|
1637
|
+
_parse(input) {
|
|
1638
|
+
const { ctx } = this._processInputParams(input);
|
|
1639
|
+
const options = this._def.options;
|
|
2029
1640
|
function handleResults(results) {
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
var result = results_1_1.value;
|
|
2035
|
-
if (result.result.status === "valid") {
|
|
2036
|
-
return result.result;
|
|
2037
|
-
}
|
|
2038
|
-
}
|
|
2039
|
-
}
|
|
2040
|
-
catch (e_9_1) { e_9 = { error: e_9_1 }; }
|
|
2041
|
-
finally {
|
|
2042
|
-
try {
|
|
2043
|
-
if (results_1_1 && !results_1_1.done && (_a = results_1.return)) _a.call(results_1);
|
|
2044
|
-
}
|
|
2045
|
-
finally { if (e_9) throw e_9.error; }
|
|
2046
|
-
}
|
|
2047
|
-
try {
|
|
2048
|
-
for (var results_2 = __values(results), results_2_1 = results_2.next(); !results_2_1.done; results_2_1 = results_2.next()) {
|
|
2049
|
-
var result = results_2_1.value;
|
|
2050
|
-
if (result.result.status === "dirty") {
|
|
2051
|
-
// add issues from dirty option
|
|
2052
|
-
(_c = ctx.issues).push.apply(_c, __spreadArray([], __read(result.ctx.issues), false));
|
|
2053
|
-
return result.result;
|
|
2054
|
-
}
|
|
1641
|
+
// return first issue-free validation if it exists
|
|
1642
|
+
for (const result of results) {
|
|
1643
|
+
if (result.result.status === "valid") {
|
|
1644
|
+
return result.result;
|
|
2055
1645
|
}
|
|
2056
1646
|
}
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
1647
|
+
for (const result of results) {
|
|
1648
|
+
if (result.result.status === "dirty") {
|
|
1649
|
+
// add issues from dirty option
|
|
1650
|
+
ctx.issues.push(...result.ctx.issues);
|
|
1651
|
+
return result.result;
|
|
2061
1652
|
}
|
|
2062
|
-
finally { if (e_10) throw e_10.error; }
|
|
2063
1653
|
}
|
|
2064
1654
|
// return invalid
|
|
2065
|
-
|
|
1655
|
+
const unionErrors = results.map((result) => new ZodError(result.ctx.issues));
|
|
2066
1656
|
addIssueToContext(ctx, {
|
|
2067
1657
|
code: ZodIssueCode.invalid_union,
|
|
2068
|
-
unionErrors
|
|
1658
|
+
unionErrors,
|
|
2069
1659
|
});
|
|
2070
1660
|
return INVALID;
|
|
2071
1661
|
}
|
|
2072
1662
|
if (ctx.async) {
|
|
2073
|
-
return Promise.all(options.map(
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
childCtx = __assign(__assign({}, ctx), { issues: [], parent: null });
|
|
2080
|
-
_a = {};
|
|
2081
|
-
return [4 /*yield*/, option._parseAsync({
|
|
2082
|
-
data: ctx.data,
|
|
2083
|
-
path: ctx.path,
|
|
2084
|
-
parent: childCtx,
|
|
2085
|
-
})];
|
|
2086
|
-
case 1: return [2 /*return*/, (_a.result = _b.sent(),
|
|
2087
|
-
_a.ctx = childCtx,
|
|
2088
|
-
_a)];
|
|
2089
|
-
}
|
|
2090
|
-
});
|
|
2091
|
-
}); })).then(handleResults);
|
|
2092
|
-
}
|
|
2093
|
-
else {
|
|
2094
|
-
var optionResults = options.map(function (option) {
|
|
2095
|
-
var childCtx = __assign(__assign({}, ctx), { issues: [], parent: null });
|
|
1663
|
+
return Promise.all(options.map(async (option) => {
|
|
1664
|
+
const childCtx = {
|
|
1665
|
+
...ctx,
|
|
1666
|
+
issues: [],
|
|
1667
|
+
parent: null,
|
|
1668
|
+
};
|
|
2096
1669
|
return {
|
|
2097
|
-
result: option.
|
|
1670
|
+
result: await option._parseAsync({
|
|
2098
1671
|
data: ctx.data,
|
|
2099
1672
|
path: ctx.path,
|
|
2100
1673
|
parent: childCtx,
|
|
2101
1674
|
}),
|
|
2102
1675
|
ctx: childCtx,
|
|
2103
1676
|
};
|
|
1677
|
+
})).then(handleResults);
|
|
1678
|
+
}
|
|
1679
|
+
else {
|
|
1680
|
+
let dirty = undefined;
|
|
1681
|
+
const issues = [];
|
|
1682
|
+
for (const option of options) {
|
|
1683
|
+
const childCtx = {
|
|
1684
|
+
...ctx,
|
|
1685
|
+
issues: [],
|
|
1686
|
+
parent: null,
|
|
1687
|
+
};
|
|
1688
|
+
const result = option._parseSync({
|
|
1689
|
+
data: ctx.data,
|
|
1690
|
+
path: ctx.path,
|
|
1691
|
+
parent: childCtx,
|
|
1692
|
+
});
|
|
1693
|
+
if (result.status === "valid") {
|
|
1694
|
+
return result;
|
|
1695
|
+
}
|
|
1696
|
+
else if (result.status === "dirty" && !dirty) {
|
|
1697
|
+
dirty = { result, ctx: childCtx };
|
|
1698
|
+
}
|
|
1699
|
+
if (childCtx.issues.length) {
|
|
1700
|
+
issues.push(childCtx.issues);
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
if (dirty) {
|
|
1704
|
+
ctx.issues.push(...dirty.ctx.issues);
|
|
1705
|
+
return dirty.result;
|
|
1706
|
+
}
|
|
1707
|
+
const unionErrors = issues.map((issues) => new ZodError(issues));
|
|
1708
|
+
addIssueToContext(ctx, {
|
|
1709
|
+
code: ZodIssueCode.invalid_union,
|
|
1710
|
+
unionErrors,
|
|
2104
1711
|
});
|
|
2105
|
-
return
|
|
1712
|
+
return INVALID;
|
|
2106
1713
|
}
|
|
2107
|
-
}
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
1714
|
+
}
|
|
1715
|
+
get options() {
|
|
1716
|
+
return this._def.options;
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
ZodUnion.create = (types, params) => {
|
|
1720
|
+
return new ZodUnion({
|
|
1721
|
+
options: types,
|
|
1722
|
+
typeName: ZodFirstPartyTypeKind.ZodUnion,
|
|
1723
|
+
...processCreateParams(params),
|
|
2114
1724
|
});
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
}(ZodType));
|
|
2120
|
-
var ZodDiscriminatedUnion = /** @class */ (function (_super) {
|
|
2121
|
-
__extends(ZodDiscriminatedUnion, _super);
|
|
2122
|
-
function ZodDiscriminatedUnion() {
|
|
2123
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
2124
|
-
}
|
|
2125
|
-
ZodDiscriminatedUnion.prototype._parse = function (input) {
|
|
2126
|
-
var ctx = this._processInputParams(input).ctx;
|
|
1725
|
+
};
|
|
1726
|
+
class ZodDiscriminatedUnion extends ZodType {
|
|
1727
|
+
_parse(input) {
|
|
1728
|
+
const { ctx } = this._processInputParams(input);
|
|
2127
1729
|
if (ctx.parsedType !== ZodParsedType.object) {
|
|
2128
1730
|
addIssueToContext(ctx, {
|
|
2129
1731
|
code: ZodIssueCode.invalid_type,
|
|
@@ -2132,9 +1734,9 @@ var ZodDiscriminatedUnion = /** @class */ (function (_super) {
|
|
|
2132
1734
|
});
|
|
2133
1735
|
return INVALID;
|
|
2134
1736
|
}
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
1737
|
+
const discriminator = this.discriminator;
|
|
1738
|
+
const discriminatorValue = ctx.data[discriminator];
|
|
1739
|
+
const option = this.options.get(discriminatorValue);
|
|
2138
1740
|
if (!option) {
|
|
2139
1741
|
addIssueToContext(ctx, {
|
|
2140
1742
|
code: ZodIssueCode.invalid_union_discriminator,
|
|
@@ -2157,28 +1759,16 @@ var ZodDiscriminatedUnion = /** @class */ (function (_super) {
|
|
|
2157
1759
|
parent: ctx,
|
|
2158
1760
|
});
|
|
2159
1761
|
}
|
|
2160
|
-
}
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
return Array.from(this.options.keys());
|
|
2171
|
-
},
|
|
2172
|
-
enumerable: false,
|
|
2173
|
-
configurable: true
|
|
2174
|
-
});
|
|
2175
|
-
Object.defineProperty(ZodDiscriminatedUnion.prototype, "options", {
|
|
2176
|
-
get: function () {
|
|
2177
|
-
return this._def.options;
|
|
2178
|
-
},
|
|
2179
|
-
enumerable: false,
|
|
2180
|
-
configurable: true
|
|
2181
|
-
});
|
|
1762
|
+
}
|
|
1763
|
+
get discriminator() {
|
|
1764
|
+
return this._def.discriminator;
|
|
1765
|
+
}
|
|
1766
|
+
get validDiscriminatorValues() {
|
|
1767
|
+
return Array.from(this.options.keys());
|
|
1768
|
+
}
|
|
1769
|
+
get options() {
|
|
1770
|
+
return this._def.options;
|
|
1771
|
+
}
|
|
2182
1772
|
/**
|
|
2183
1773
|
* The constructor of the discriminated union schema. Its behaviour is very similar to that of the normal z.union() constructor.
|
|
2184
1774
|
* However, it only allows a union of objects, all of which need to share a discriminator property. This property must
|
|
@@ -2187,12 +1777,12 @@ var ZodDiscriminatedUnion = /** @class */ (function (_super) {
|
|
|
2187
1777
|
* @param types an array of object schemas
|
|
2188
1778
|
* @param params
|
|
2189
1779
|
*/
|
|
2190
|
-
|
|
1780
|
+
static create(discriminator, types, params) {
|
|
2191
1781
|
// Get all the valid discriminator values
|
|
2192
|
-
|
|
1782
|
+
const options = new Map();
|
|
2193
1783
|
try {
|
|
2194
|
-
types.forEach(
|
|
2195
|
-
|
|
1784
|
+
types.forEach((type) => {
|
|
1785
|
+
const discriminatorValue = type.shape[discriminator].value;
|
|
2196
1786
|
options.set(discriminatorValue, type);
|
|
2197
1787
|
});
|
|
2198
1788
|
}
|
|
@@ -2203,39 +1793,32 @@ var ZodDiscriminatedUnion = /** @class */ (function (_super) {
|
|
|
2203
1793
|
if (options.size !== types.length) {
|
|
2204
1794
|
throw new Error("Some of the discriminator values are not unique");
|
|
2205
1795
|
}
|
|
2206
|
-
return new ZodDiscriminatedUnion(
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
1796
|
+
return new ZodDiscriminatedUnion({
|
|
1797
|
+
typeName: ZodFirstPartyTypeKind.ZodDiscriminatedUnion,
|
|
1798
|
+
discriminator,
|
|
1799
|
+
options,
|
|
1800
|
+
...processCreateParams(params),
|
|
1801
|
+
});
|
|
1802
|
+
}
|
|
1803
|
+
}
|
|
2210
1804
|
function mergeValues(a, b) {
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
var bType = getParsedType(b);
|
|
1805
|
+
const aType = getParsedType(a);
|
|
1806
|
+
const bType = getParsedType(b);
|
|
2214
1807
|
if (a === b) {
|
|
2215
1808
|
return { valid: true, data: a };
|
|
2216
1809
|
}
|
|
2217
1810
|
else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
|
|
2218
|
-
|
|
2219
|
-
|
|
1811
|
+
const bKeys = util.objectKeys(b);
|
|
1812
|
+
const sharedKeys = util
|
|
2220
1813
|
.objectKeys(a)
|
|
2221
|
-
.filter(
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
if (!sharedValue.valid) {
|
|
2228
|
-
return { valid: false };
|
|
2229
|
-
}
|
|
2230
|
-
newObj[key] = sharedValue.data;
|
|
2231
|
-
}
|
|
2232
|
-
}
|
|
2233
|
-
catch (e_11_1) { e_11 = { error: e_11_1 }; }
|
|
2234
|
-
finally {
|
|
2235
|
-
try {
|
|
2236
|
-
if (sharedKeys_1_1 && !sharedKeys_1_1.done && (_a = sharedKeys_1.return)) _a.call(sharedKeys_1);
|
|
1814
|
+
.filter((key) => bKeys.indexOf(key) !== -1);
|
|
1815
|
+
const newObj = { ...a, ...b };
|
|
1816
|
+
for (const key of sharedKeys) {
|
|
1817
|
+
const sharedValue = mergeValues(a[key], b[key]);
|
|
1818
|
+
if (!sharedValue.valid) {
|
|
1819
|
+
return { valid: false };
|
|
2237
1820
|
}
|
|
2238
|
-
|
|
1821
|
+
newObj[key] = sharedValue.data;
|
|
2239
1822
|
}
|
|
2240
1823
|
return { valid: true, data: newObj };
|
|
2241
1824
|
}
|
|
@@ -2243,11 +1826,11 @@ function mergeValues(a, b) {
|
|
|
2243
1826
|
if (a.length !== b.length) {
|
|
2244
1827
|
return { valid: false };
|
|
2245
1828
|
}
|
|
2246
|
-
|
|
2247
|
-
for (
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
1829
|
+
const newArray = [];
|
|
1830
|
+
for (let index = 0; index < a.length; index++) {
|
|
1831
|
+
const itemA = a[index];
|
|
1832
|
+
const itemB = b[index];
|
|
1833
|
+
const sharedValue = mergeValues(itemA, itemB);
|
|
2251
1834
|
if (!sharedValue.valid) {
|
|
2252
1835
|
return { valid: false };
|
|
2253
1836
|
}
|
|
@@ -2264,18 +1847,14 @@ function mergeValues(a, b) {
|
|
|
2264
1847
|
return { valid: false };
|
|
2265
1848
|
}
|
|
2266
1849
|
}
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
}
|
|
2272
|
-
ZodIntersection.prototype._parse = function (input) {
|
|
2273
|
-
var _a = this._processInputParams(input), status = _a.status, ctx = _a.ctx;
|
|
2274
|
-
var handleParsed = function (parsedLeft, parsedRight) {
|
|
1850
|
+
class ZodIntersection extends ZodType {
|
|
1851
|
+
_parse(input) {
|
|
1852
|
+
const { status, ctx } = this._processInputParams(input);
|
|
1853
|
+
const handleParsed = (parsedLeft, parsedRight) => {
|
|
2275
1854
|
if (isAborted(parsedLeft) || isAborted(parsedRight)) {
|
|
2276
1855
|
return INVALID;
|
|
2277
1856
|
}
|
|
2278
|
-
|
|
1857
|
+
const merged = mergeValues(parsedLeft.value, parsedRight.value);
|
|
2279
1858
|
if (!merged.valid) {
|
|
2280
1859
|
addIssueToContext(ctx, {
|
|
2281
1860
|
code: ZodIssueCode.invalid_intersection_types,
|
|
@@ -2299,10 +1878,7 @@ var ZodIntersection = /** @class */ (function (_super) {
|
|
|
2299
1878
|
path: ctx.path,
|
|
2300
1879
|
parent: ctx,
|
|
2301
1880
|
}),
|
|
2302
|
-
]).then(
|
|
2303
|
-
var _b = __read(_a, 2), left = _b[0], right = _b[1];
|
|
2304
|
-
return handleParsed(left, right);
|
|
2305
|
-
});
|
|
1881
|
+
]).then(([left, right]) => handleParsed(left, right));
|
|
2306
1882
|
}
|
|
2307
1883
|
else {
|
|
2308
1884
|
return handleParsed(this._def.left._parseSync({
|
|
@@ -2315,20 +1891,19 @@ var ZodIntersection = /** @class */ (function (_super) {
|
|
|
2315
1891
|
parent: ctx,
|
|
2316
1892
|
}));
|
|
2317
1893
|
}
|
|
2318
|
-
}
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
var _a = this._processInputParams(input), status = _a.status, ctx = _a.ctx;
|
|
1894
|
+
}
|
|
1895
|
+
}
|
|
1896
|
+
ZodIntersection.create = (left, right, params) => {
|
|
1897
|
+
return new ZodIntersection({
|
|
1898
|
+
left: left,
|
|
1899
|
+
right: right,
|
|
1900
|
+
typeName: ZodFirstPartyTypeKind.ZodIntersection,
|
|
1901
|
+
...processCreateParams(params),
|
|
1902
|
+
});
|
|
1903
|
+
};
|
|
1904
|
+
class ZodTuple extends ZodType {
|
|
1905
|
+
_parse(input) {
|
|
1906
|
+
const { status, ctx } = this._processInputParams(input);
|
|
2332
1907
|
if (ctx.parsedType !== ZodParsedType.array) {
|
|
2333
1908
|
addIssueToContext(ctx, {
|
|
2334
1909
|
code: ZodIssueCode.invalid_type,
|
|
@@ -2346,7 +1921,7 @@ var ZodTuple = /** @class */ (function (_super) {
|
|
|
2346
1921
|
});
|
|
2347
1922
|
return INVALID;
|
|
2348
1923
|
}
|
|
2349
|
-
|
|
1924
|
+
const rest = this._def.rest;
|
|
2350
1925
|
if (!rest && ctx.data.length > this._def.items.length) {
|
|
2351
1926
|
addIssueToContext(ctx, {
|
|
2352
1927
|
code: ZodIssueCode.too_big,
|
|
@@ -2356,63 +1931,54 @@ var ZodTuple = /** @class */ (function (_super) {
|
|
|
2356
1931
|
});
|
|
2357
1932
|
status.dirty();
|
|
2358
1933
|
}
|
|
2359
|
-
|
|
2360
|
-
.map(
|
|
2361
|
-
|
|
1934
|
+
const items = ctx.data
|
|
1935
|
+
.map((item, itemIndex) => {
|
|
1936
|
+
const schema = this._def.items[itemIndex] || this._def.rest;
|
|
2362
1937
|
if (!schema)
|
|
2363
1938
|
return null;
|
|
2364
1939
|
return schema._parse({
|
|
2365
1940
|
data: item,
|
|
2366
|
-
path:
|
|
1941
|
+
path: [...ctx.path, itemIndex],
|
|
2367
1942
|
parent: ctx,
|
|
2368
1943
|
});
|
|
2369
1944
|
})
|
|
2370
|
-
.filter(
|
|
1945
|
+
.filter((x) => !!x); // filter nulls
|
|
2371
1946
|
if (ctx.async) {
|
|
2372
|
-
return Promise.all(items).then(
|
|
1947
|
+
return Promise.all(items).then((results) => {
|
|
2373
1948
|
return ParseStatus.mergeArray(status, results);
|
|
2374
1949
|
});
|
|
2375
1950
|
}
|
|
2376
1951
|
else {
|
|
2377
1952
|
return ParseStatus.mergeArray(status, items);
|
|
2378
1953
|
}
|
|
2379
|
-
}
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
__extends(ZodRecord, _super);
|
|
2397
|
-
function ZodRecord() {
|
|
2398
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
2399
|
-
}
|
|
2400
|
-
Object.defineProperty(ZodRecord.prototype, "keySchema", {
|
|
2401
|
-
get: function () {
|
|
2402
|
-
return this._def.keyType;
|
|
2403
|
-
},
|
|
2404
|
-
enumerable: false,
|
|
2405
|
-
configurable: true
|
|
2406
|
-
});
|
|
2407
|
-
Object.defineProperty(ZodRecord.prototype, "valueSchema", {
|
|
2408
|
-
get: function () {
|
|
2409
|
-
return this._def.valueType;
|
|
2410
|
-
},
|
|
2411
|
-
enumerable: false,
|
|
2412
|
-
configurable: true
|
|
1954
|
+
}
|
|
1955
|
+
get items() {
|
|
1956
|
+
return this._def.items;
|
|
1957
|
+
}
|
|
1958
|
+
rest(rest) {
|
|
1959
|
+
return new ZodTuple({
|
|
1960
|
+
...this._def,
|
|
1961
|
+
rest,
|
|
1962
|
+
});
|
|
1963
|
+
}
|
|
1964
|
+
}
|
|
1965
|
+
ZodTuple.create = (schemas, params) => {
|
|
1966
|
+
return new ZodTuple({
|
|
1967
|
+
items: schemas,
|
|
1968
|
+
typeName: ZodFirstPartyTypeKind.ZodTuple,
|
|
1969
|
+
rest: null,
|
|
1970
|
+
...processCreateParams(params),
|
|
2413
1971
|
});
|
|
2414
|
-
|
|
2415
|
-
|
|
1972
|
+
};
|
|
1973
|
+
class ZodRecord extends ZodType {
|
|
1974
|
+
get keySchema() {
|
|
1975
|
+
return this._def.keyType;
|
|
1976
|
+
}
|
|
1977
|
+
get valueSchema() {
|
|
1978
|
+
return this._def.valueType;
|
|
1979
|
+
}
|
|
1980
|
+
_parse(input) {
|
|
1981
|
+
const { status, ctx } = this._processInputParams(input);
|
|
2416
1982
|
if (ctx.parsedType !== ZodParsedType.object) {
|
|
2417
1983
|
addIssueToContext(ctx, {
|
|
2418
1984
|
code: ZodIssueCode.invalid_type,
|
|
@@ -2421,19 +1987,19 @@ var ZodRecord = /** @class */ (function (_super) {
|
|
|
2421
1987
|
});
|
|
2422
1988
|
return INVALID;
|
|
2423
1989
|
}
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
for (
|
|
1990
|
+
const pairs = [];
|
|
1991
|
+
const keyType = this._def.keyType;
|
|
1992
|
+
const valueType = this._def.valueType;
|
|
1993
|
+
for (const key in ctx.data) {
|
|
2428
1994
|
pairs.push({
|
|
2429
1995
|
key: keyType._parse({
|
|
2430
1996
|
data: key,
|
|
2431
|
-
path:
|
|
1997
|
+
path: [...ctx.path, key],
|
|
2432
1998
|
parent: ctx,
|
|
2433
1999
|
}),
|
|
2434
2000
|
value: valueType._parse({
|
|
2435
2001
|
data: ctx.data[key],
|
|
2436
|
-
path:
|
|
2002
|
+
path: [...ctx.path, key],
|
|
2437
2003
|
parent: ctx,
|
|
2438
2004
|
}),
|
|
2439
2005
|
});
|
|
@@ -2444,31 +2010,30 @@ var ZodRecord = /** @class */ (function (_super) {
|
|
|
2444
2010
|
else {
|
|
2445
2011
|
return ParseStatus.mergeObjectSync(status, pairs);
|
|
2446
2012
|
}
|
|
2447
|
-
}
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
enumerable: false,
|
|
2453
|
-
configurable: true
|
|
2454
|
-
});
|
|
2455
|
-
ZodRecord.create = function (first, second, third) {
|
|
2013
|
+
}
|
|
2014
|
+
get element() {
|
|
2015
|
+
return this._def.valueType;
|
|
2016
|
+
}
|
|
2017
|
+
static create(first, second, third) {
|
|
2456
2018
|
if (second instanceof ZodType) {
|
|
2457
|
-
return new ZodRecord(
|
|
2019
|
+
return new ZodRecord({
|
|
2020
|
+
keyType: first,
|
|
2021
|
+
valueType: second,
|
|
2022
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
2023
|
+
...processCreateParams(third),
|
|
2024
|
+
});
|
|
2458
2025
|
}
|
|
2459
|
-
return new ZodRecord(
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
var _this = this;
|
|
2471
|
-
var _b = this._processInputParams(input), status = _b.status, ctx = _b.ctx;
|
|
2026
|
+
return new ZodRecord({
|
|
2027
|
+
keyType: ZodString.create(),
|
|
2028
|
+
valueType: first,
|
|
2029
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
2030
|
+
...processCreateParams(second),
|
|
2031
|
+
});
|
|
2032
|
+
}
|
|
2033
|
+
}
|
|
2034
|
+
class ZodMap extends ZodType {
|
|
2035
|
+
_parse(input) {
|
|
2036
|
+
const { status, ctx } = this._processInputParams(input);
|
|
2472
2037
|
if (ctx.parsedType !== ZodParsedType.map) {
|
|
2473
2038
|
addIssueToContext(ctx, {
|
|
2474
2039
|
code: ZodIssueCode.invalid_type,
|
|
@@ -2477,77 +2042,28 @@ var ZodMap = /** @class */ (function (_super) {
|
|
|
2477
2042
|
});
|
|
2478
2043
|
return INVALID;
|
|
2479
2044
|
}
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
var _b = __read(_a, 2), key = _b[0], value = _b[1];
|
|
2045
|
+
const keyType = this._def.keyType;
|
|
2046
|
+
const valueType = this._def.valueType;
|
|
2047
|
+
const pairs = [...ctx.data.entries()].map(([key, value], index) => {
|
|
2484
2048
|
return {
|
|
2485
2049
|
key: keyType._parse({
|
|
2486
2050
|
data: key,
|
|
2487
|
-
path:
|
|
2051
|
+
path: [...ctx.path, index, "key"],
|
|
2488
2052
|
parent: ctx,
|
|
2489
2053
|
}),
|
|
2490
2054
|
value: valueType._parse({
|
|
2491
2055
|
data: value,
|
|
2492
|
-
path:
|
|
2056
|
+
path: [...ctx.path, index, "value"],
|
|
2493
2057
|
parent: ctx,
|
|
2494
2058
|
}),
|
|
2495
2059
|
};
|
|
2496
2060
|
});
|
|
2497
2061
|
if (ctx.async) {
|
|
2498
|
-
|
|
2499
|
-
return Promise.resolve().then(
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
switch (_b.label) {
|
|
2504
|
-
case 0:
|
|
2505
|
-
_b.trys.push([0, 6, 7, 8]);
|
|
2506
|
-
pairs_3 = __values(pairs), pairs_3_1 = pairs_3.next();
|
|
2507
|
-
_b.label = 1;
|
|
2508
|
-
case 1:
|
|
2509
|
-
if (!!pairs_3_1.done) return [3 /*break*/, 5];
|
|
2510
|
-
pair = pairs_3_1.value;
|
|
2511
|
-
return [4 /*yield*/, pair.key];
|
|
2512
|
-
case 2:
|
|
2513
|
-
key = _b.sent();
|
|
2514
|
-
return [4 /*yield*/, pair.value];
|
|
2515
|
-
case 3:
|
|
2516
|
-
value = _b.sent();
|
|
2517
|
-
if (key.status === "aborted" || value.status === "aborted") {
|
|
2518
|
-
return [2 /*return*/, INVALID];
|
|
2519
|
-
}
|
|
2520
|
-
if (key.status === "dirty" || value.status === "dirty") {
|
|
2521
|
-
status.dirty();
|
|
2522
|
-
}
|
|
2523
|
-
finalMap_1.set(key.value, value.value);
|
|
2524
|
-
_b.label = 4;
|
|
2525
|
-
case 4:
|
|
2526
|
-
pairs_3_1 = pairs_3.next();
|
|
2527
|
-
return [3 /*break*/, 1];
|
|
2528
|
-
case 5: return [3 /*break*/, 8];
|
|
2529
|
-
case 6:
|
|
2530
|
-
e_13_1 = _b.sent();
|
|
2531
|
-
e_13 = { error: e_13_1 };
|
|
2532
|
-
return [3 /*break*/, 8];
|
|
2533
|
-
case 7:
|
|
2534
|
-
try {
|
|
2535
|
-
if (pairs_3_1 && !pairs_3_1.done && (_a = pairs_3.return)) _a.call(pairs_3);
|
|
2536
|
-
}
|
|
2537
|
-
finally { if (e_13) throw e_13.error; }
|
|
2538
|
-
return [7 /*endfinally*/];
|
|
2539
|
-
case 8: return [2 /*return*/, { status: status.value, value: finalMap_1 }];
|
|
2540
|
-
}
|
|
2541
|
-
});
|
|
2542
|
-
}); });
|
|
2543
|
-
}
|
|
2544
|
-
else {
|
|
2545
|
-
var finalMap = new Map();
|
|
2546
|
-
try {
|
|
2547
|
-
for (var pairs_2 = __values(pairs), pairs_2_1 = pairs_2.next(); !pairs_2_1.done; pairs_2_1 = pairs_2.next()) {
|
|
2548
|
-
var pair = pairs_2_1.value;
|
|
2549
|
-
var key = pair.key;
|
|
2550
|
-
var value = pair.value;
|
|
2062
|
+
const finalMap = new Map();
|
|
2063
|
+
return Promise.resolve().then(async () => {
|
|
2064
|
+
for (const pair of pairs) {
|
|
2065
|
+
const key = await pair.key;
|
|
2066
|
+
const value = await pair.value;
|
|
2551
2067
|
if (key.status === "aborted" || value.status === "aborted") {
|
|
2552
2068
|
return INVALID;
|
|
2553
2069
|
}
|
|
@@ -2556,29 +2072,37 @@ var ZodMap = /** @class */ (function (_super) {
|
|
|
2556
2072
|
}
|
|
2557
2073
|
finalMap.set(key.value, value.value);
|
|
2558
2074
|
}
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2075
|
+
return { status: status.value, value: finalMap };
|
|
2076
|
+
});
|
|
2077
|
+
}
|
|
2078
|
+
else {
|
|
2079
|
+
const finalMap = new Map();
|
|
2080
|
+
for (const pair of pairs) {
|
|
2081
|
+
const key = pair.key;
|
|
2082
|
+
const value = pair.value;
|
|
2083
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
2084
|
+
return INVALID;
|
|
2085
|
+
}
|
|
2086
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
2087
|
+
status.dirty();
|
|
2564
2088
|
}
|
|
2565
|
-
|
|
2089
|
+
finalMap.set(key.value, value.value);
|
|
2566
2090
|
}
|
|
2567
2091
|
return { status: status.value, value: finalMap };
|
|
2568
2092
|
}
|
|
2569
|
-
}
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2093
|
+
}
|
|
2094
|
+
}
|
|
2095
|
+
ZodMap.create = (keyType, valueType, params) => {
|
|
2096
|
+
return new ZodMap({
|
|
2097
|
+
valueType,
|
|
2098
|
+
keyType,
|
|
2099
|
+
typeName: ZodFirstPartyTypeKind.ZodMap,
|
|
2100
|
+
...processCreateParams(params),
|
|
2101
|
+
});
|
|
2102
|
+
};
|
|
2103
|
+
class ZodSet extends ZodType {
|
|
2104
|
+
_parse(input) {
|
|
2105
|
+
const { status, ctx } = this._processInputParams(input);
|
|
2582
2106
|
if (ctx.parsedType !== ZodParsedType.set) {
|
|
2583
2107
|
addIssueToContext(ctx, {
|
|
2584
2108
|
code: ZodIssueCode.invalid_type,
|
|
@@ -2587,7 +2111,7 @@ var ZodSet = /** @class */ (function (_super) {
|
|
|
2587
2111
|
});
|
|
2588
2112
|
return INVALID;
|
|
2589
2113
|
}
|
|
2590
|
-
|
|
2114
|
+
const def = this._def;
|
|
2591
2115
|
if (def.minSize !== null) {
|
|
2592
2116
|
if (ctx.data.size < def.minSize.value) {
|
|
2593
2117
|
addIssueToContext(ctx, {
|
|
@@ -2612,66 +2136,61 @@ var ZodSet = /** @class */ (function (_super) {
|
|
|
2612
2136
|
status.dirty();
|
|
2613
2137
|
}
|
|
2614
2138
|
}
|
|
2615
|
-
|
|
2139
|
+
const valueType = this._def.valueType;
|
|
2616
2140
|
function finalizeSet(elements) {
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
if (element.status === "dirty")
|
|
2625
|
-
status.dirty();
|
|
2626
|
-
parsedSet.add(element.value);
|
|
2627
|
-
}
|
|
2628
|
-
}
|
|
2629
|
-
catch (e_14_1) { e_14 = { error: e_14_1 }; }
|
|
2630
|
-
finally {
|
|
2631
|
-
try {
|
|
2632
|
-
if (elements_1_1 && !elements_1_1.done && (_a = elements_1.return)) _a.call(elements_1);
|
|
2633
|
-
}
|
|
2634
|
-
finally { if (e_14) throw e_14.error; }
|
|
2141
|
+
const parsedSet = new Set();
|
|
2142
|
+
for (const element of elements) {
|
|
2143
|
+
if (element.status === "aborted")
|
|
2144
|
+
return INVALID;
|
|
2145
|
+
if (element.status === "dirty")
|
|
2146
|
+
status.dirty();
|
|
2147
|
+
parsedSet.add(element.value);
|
|
2635
2148
|
}
|
|
2636
2149
|
return { status: status.value, value: parsedSet };
|
|
2637
2150
|
}
|
|
2638
|
-
|
|
2639
|
-
return valueType._parse({ data: item, path: __spreadArray(__spreadArray([], __read(ctx.path), false), [i], false), parent: ctx });
|
|
2640
|
-
});
|
|
2151
|
+
const elements = [...ctx.data.values()].map((item, i) => valueType._parse({ data: item, path: [...ctx.path, i], parent: ctx }));
|
|
2641
2152
|
if (ctx.async) {
|
|
2642
|
-
return Promise.all(elements).then(
|
|
2153
|
+
return Promise.all(elements).then((elements) => finalizeSet(elements));
|
|
2643
2154
|
}
|
|
2644
2155
|
else {
|
|
2645
2156
|
return finalizeSet(elements);
|
|
2646
2157
|
}
|
|
2647
|
-
}
|
|
2648
|
-
|
|
2649
|
-
return new ZodSet(
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
}
|
|
2654
|
-
|
|
2158
|
+
}
|
|
2159
|
+
min(minSize, message) {
|
|
2160
|
+
return new ZodSet({
|
|
2161
|
+
...this._def,
|
|
2162
|
+
minSize: { value: minSize, message: errorUtil.toString(message) },
|
|
2163
|
+
});
|
|
2164
|
+
}
|
|
2165
|
+
max(maxSize, message) {
|
|
2166
|
+
return new ZodSet({
|
|
2167
|
+
...this._def,
|
|
2168
|
+
maxSize: { value: maxSize, message: errorUtil.toString(message) },
|
|
2169
|
+
});
|
|
2170
|
+
}
|
|
2171
|
+
size(size, message) {
|
|
2655
2172
|
return this.min(size, message).max(size, message);
|
|
2656
|
-
}
|
|
2657
|
-
|
|
2173
|
+
}
|
|
2174
|
+
nonempty(message) {
|
|
2658
2175
|
return this.min(1, message);
|
|
2659
|
-
}
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2176
|
+
}
|
|
2177
|
+
}
|
|
2178
|
+
ZodSet.create = (valueType, params) => {
|
|
2179
|
+
return new ZodSet({
|
|
2180
|
+
valueType,
|
|
2181
|
+
minSize: null,
|
|
2182
|
+
maxSize: null,
|
|
2183
|
+
typeName: ZodFirstPartyTypeKind.ZodSet,
|
|
2184
|
+
...processCreateParams(params),
|
|
2185
|
+
});
|
|
2186
|
+
};
|
|
2187
|
+
class ZodFunction extends ZodType {
|
|
2188
|
+
constructor() {
|
|
2189
|
+
super(...arguments);
|
|
2190
|
+
this.validate = this.implement;
|
|
2191
|
+
}
|
|
2192
|
+
_parse(input) {
|
|
2193
|
+
const { ctx } = this._processInputParams(input);
|
|
2675
2194
|
if (ctx.parsedType !== ZodParsedType.function) {
|
|
2676
2195
|
addIssueToContext(ctx, {
|
|
2677
2196
|
code: ZodIssueCode.invalid_type,
|
|
@@ -2689,7 +2208,7 @@ var ZodFunction = /** @class */ (function (_super) {
|
|
|
2689
2208
|
ctx.schemaErrorMap,
|
|
2690
2209
|
overrideErrorMap,
|
|
2691
2210
|
defaultErrorMap,
|
|
2692
|
-
].filter(
|
|
2211
|
+
].filter((x) => !!x),
|
|
2693
2212
|
issueData: {
|
|
2694
2213
|
code: ZodIssueCode.invalid_arguments,
|
|
2695
2214
|
argumentsError: error,
|
|
@@ -2705,132 +2224,106 @@ var ZodFunction = /** @class */ (function (_super) {
|
|
|
2705
2224
|
ctx.schemaErrorMap,
|
|
2706
2225
|
overrideErrorMap,
|
|
2707
2226
|
defaultErrorMap,
|
|
2708
|
-
].filter(
|
|
2227
|
+
].filter((x) => !!x),
|
|
2709
2228
|
issueData: {
|
|
2710
2229
|
code: ZodIssueCode.invalid_return_type,
|
|
2711
2230
|
returnTypeError: error,
|
|
2712
2231
|
},
|
|
2713
2232
|
});
|
|
2714
2233
|
}
|
|
2715
|
-
|
|
2716
|
-
|
|
2234
|
+
const params = { errorMap: ctx.contextualErrorMap };
|
|
2235
|
+
const fn = ctx.data;
|
|
2717
2236
|
if (this._def.returns instanceof ZodPromise) {
|
|
2718
|
-
return OK(
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
args
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
error.addIssue(makeArgsIssue(args, e));
|
|
2733
|
-
throw error;
|
|
2734
|
-
})];
|
|
2735
|
-
case 1:
|
|
2736
|
-
parsedArgs = _a.sent();
|
|
2737
|
-
return [4 /*yield*/, fn.apply(void 0, __spreadArray([], __read(parsedArgs), false))];
|
|
2738
|
-
case 2:
|
|
2739
|
-
result = _a.sent();
|
|
2740
|
-
return [4 /*yield*/, this._def
|
|
2741
|
-
.returns._def.type
|
|
2742
|
-
.parseAsync(result, params)
|
|
2743
|
-
.catch(function (e) {
|
|
2744
|
-
error.addIssue(makeReturnsIssue(result, e));
|
|
2745
|
-
throw error;
|
|
2746
|
-
})];
|
|
2747
|
-
case 3:
|
|
2748
|
-
parsedReturns = _a.sent();
|
|
2749
|
-
return [2 /*return*/, parsedReturns];
|
|
2750
|
-
}
|
|
2751
|
-
});
|
|
2237
|
+
return OK(async (...args) => {
|
|
2238
|
+
const error = new ZodError([]);
|
|
2239
|
+
const parsedArgs = await this._def.args
|
|
2240
|
+
.parseAsync(args, params)
|
|
2241
|
+
.catch((e) => {
|
|
2242
|
+
error.addIssue(makeArgsIssue(args, e));
|
|
2243
|
+
throw error;
|
|
2244
|
+
});
|
|
2245
|
+
const result = await fn(...parsedArgs);
|
|
2246
|
+
const parsedReturns = await this._def.returns._def.type
|
|
2247
|
+
.parseAsync(result, params)
|
|
2248
|
+
.catch((e) => {
|
|
2249
|
+
error.addIssue(makeReturnsIssue(result, e));
|
|
2250
|
+
throw error;
|
|
2752
2251
|
});
|
|
2252
|
+
return parsedReturns;
|
|
2753
2253
|
});
|
|
2754
2254
|
}
|
|
2755
2255
|
else {
|
|
2756
|
-
return OK(
|
|
2757
|
-
|
|
2758
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2759
|
-
args[_i] = arguments[_i];
|
|
2760
|
-
}
|
|
2761
|
-
var parsedArgs = _this._def.args.safeParse(args, params);
|
|
2256
|
+
return OK((...args) => {
|
|
2257
|
+
const parsedArgs = this._def.args.safeParse(args, params);
|
|
2762
2258
|
if (!parsedArgs.success) {
|
|
2763
2259
|
throw new ZodError([makeArgsIssue(args, parsedArgs.error)]);
|
|
2764
2260
|
}
|
|
2765
|
-
|
|
2766
|
-
|
|
2261
|
+
const result = fn(...parsedArgs.data);
|
|
2262
|
+
const parsedReturns = this._def.returns.safeParse(result, params);
|
|
2767
2263
|
if (!parsedReturns.success) {
|
|
2768
2264
|
throw new ZodError([makeReturnsIssue(result, parsedReturns.error)]);
|
|
2769
2265
|
}
|
|
2770
2266
|
return parsedReturns.data;
|
|
2771
2267
|
});
|
|
2772
2268
|
}
|
|
2773
|
-
}
|
|
2774
|
-
|
|
2269
|
+
}
|
|
2270
|
+
parameters() {
|
|
2775
2271
|
return this._def.args;
|
|
2776
|
-
}
|
|
2777
|
-
|
|
2272
|
+
}
|
|
2273
|
+
returnType() {
|
|
2778
2274
|
return this._def.returns;
|
|
2779
|
-
}
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
items
|
|
2784
|
-
}
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2275
|
+
}
|
|
2276
|
+
args(...items) {
|
|
2277
|
+
return new ZodFunction({
|
|
2278
|
+
...this._def,
|
|
2279
|
+
args: ZodTuple.create(items).rest(ZodUnknown.create()),
|
|
2280
|
+
});
|
|
2281
|
+
}
|
|
2282
|
+
returns(returnType) {
|
|
2283
|
+
return new ZodFunction({
|
|
2284
|
+
...this._def,
|
|
2285
|
+
returns: returnType,
|
|
2286
|
+
});
|
|
2287
|
+
}
|
|
2288
|
+
implement(func) {
|
|
2289
|
+
const validatedFunc = this.parse(func);
|
|
2792
2290
|
return validatedFunc;
|
|
2793
|
-
}
|
|
2794
|
-
|
|
2795
|
-
|
|
2291
|
+
}
|
|
2292
|
+
strictImplement(func) {
|
|
2293
|
+
const validatedFunc = this.parse(func);
|
|
2796
2294
|
return validatedFunc;
|
|
2797
|
-
}
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
function ZodLazy() {
|
|
2808
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
2809
|
-
}
|
|
2810
|
-
Object.defineProperty(ZodLazy.prototype, "schema", {
|
|
2811
|
-
get: function () {
|
|
2812
|
-
return this._def.getter();
|
|
2813
|
-
},
|
|
2814
|
-
enumerable: false,
|
|
2815
|
-
configurable: true
|
|
2295
|
+
}
|
|
2296
|
+
}
|
|
2297
|
+
ZodFunction.create = (args, returns, params) => {
|
|
2298
|
+
return new ZodFunction({
|
|
2299
|
+
args: (args
|
|
2300
|
+
? args.rest(ZodUnknown.create())
|
|
2301
|
+
: ZodTuple.create([]).rest(ZodUnknown.create())),
|
|
2302
|
+
returns: returns || ZodUnknown.create(),
|
|
2303
|
+
typeName: ZodFirstPartyTypeKind.ZodFunction,
|
|
2304
|
+
...processCreateParams(params),
|
|
2816
2305
|
});
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2306
|
+
};
|
|
2307
|
+
class ZodLazy extends ZodType {
|
|
2308
|
+
get schema() {
|
|
2309
|
+
return this._def.getter();
|
|
2310
|
+
}
|
|
2311
|
+
_parse(input) {
|
|
2312
|
+
const { ctx } = this._processInputParams(input);
|
|
2313
|
+
const lazySchema = this._def.getter();
|
|
2820
2314
|
return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx });
|
|
2821
|
-
}
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
var _a = this._processInputParams(input), status = _a.status, ctx = _a.ctx;
|
|
2315
|
+
}
|
|
2316
|
+
}
|
|
2317
|
+
ZodLazy.create = (getter, params) => {
|
|
2318
|
+
return new ZodLazy({
|
|
2319
|
+
getter: getter,
|
|
2320
|
+
typeName: ZodFirstPartyTypeKind.ZodLazy,
|
|
2321
|
+
...processCreateParams(params),
|
|
2322
|
+
});
|
|
2323
|
+
};
|
|
2324
|
+
class ZodLiteral extends ZodType {
|
|
2325
|
+
_parse(input) {
|
|
2326
|
+
const { status, ctx } = this._processInputParams(input);
|
|
2834
2327
|
if (ctx.data !== this._def.value) {
|
|
2835
2328
|
addIssueToContext(ctx, {
|
|
2836
2329
|
code: ZodIssueCode.invalid_type,
|
|
@@ -2840,32 +2333,27 @@ var ZodLiteral = /** @class */ (function (_super) {
|
|
|
2840
2333
|
return INVALID;
|
|
2841
2334
|
}
|
|
2842
2335
|
return { status: status.value, value: ctx.data };
|
|
2843
|
-
}
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2336
|
+
}
|
|
2337
|
+
get value() {
|
|
2338
|
+
return this._def.value;
|
|
2339
|
+
}
|
|
2340
|
+
}
|
|
2341
|
+
ZodLiteral.create = (value, params) => {
|
|
2342
|
+
return new ZodLiteral({
|
|
2343
|
+
value: value,
|
|
2344
|
+
typeName: ZodFirstPartyTypeKind.ZodLiteral,
|
|
2345
|
+
...processCreateParams(params),
|
|
2850
2346
|
});
|
|
2851
|
-
|
|
2852
|
-
return new ZodLiteral(__assign({ value: value, typeName: ZodFirstPartyTypeKind.ZodLiteral }, processCreateParams(params)));
|
|
2853
|
-
};
|
|
2854
|
-
return ZodLiteral;
|
|
2855
|
-
}(ZodType));
|
|
2347
|
+
};
|
|
2856
2348
|
function createZodEnum(values) {
|
|
2857
2349
|
return new ZodEnum({
|
|
2858
2350
|
values: values,
|
|
2859
2351
|
typeName: ZodFirstPartyTypeKind.ZodEnum,
|
|
2860
2352
|
});
|
|
2861
2353
|
}
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
2866
|
-
}
|
|
2867
|
-
ZodEnum.prototype._parse = function (input) {
|
|
2868
|
-
var ctx = this._processInputParams(input).ctx;
|
|
2354
|
+
class ZodEnum extends ZodType {
|
|
2355
|
+
_parse(input) {
|
|
2356
|
+
const { ctx } = this._processInputParams(input);
|
|
2869
2357
|
if (this._def.values.indexOf(ctx.data) === -1) {
|
|
2870
2358
|
addIssueToContext(ctx, {
|
|
2871
2359
|
code: ZodIssueCode.invalid_enum_value,
|
|
@@ -2874,91 +2362,37 @@ var ZodEnum = /** @class */ (function (_super) {
|
|
|
2874
2362
|
return INVALID;
|
|
2875
2363
|
}
|
|
2876
2364
|
return OK(ctx.data);
|
|
2877
|
-
}
|
|
2878
|
-
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
get: function () {
|
|
2909
|
-
var e_16, _a;
|
|
2910
|
-
var enumValues = {};
|
|
2911
|
-
try {
|
|
2912
|
-
for (var _b = __values(this._def.values), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
2913
|
-
var val = _c.value;
|
|
2914
|
-
enumValues[val] = val;
|
|
2915
|
-
}
|
|
2916
|
-
}
|
|
2917
|
-
catch (e_16_1) { e_16 = { error: e_16_1 }; }
|
|
2918
|
-
finally {
|
|
2919
|
-
try {
|
|
2920
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
2921
|
-
}
|
|
2922
|
-
finally { if (e_16) throw e_16.error; }
|
|
2923
|
-
}
|
|
2924
|
-
return enumValues;
|
|
2925
|
-
},
|
|
2926
|
-
enumerable: false,
|
|
2927
|
-
configurable: true
|
|
2928
|
-
});
|
|
2929
|
-
Object.defineProperty(ZodEnum.prototype, "Enum", {
|
|
2930
|
-
get: function () {
|
|
2931
|
-
var e_17, _a;
|
|
2932
|
-
var enumValues = {};
|
|
2933
|
-
try {
|
|
2934
|
-
for (var _b = __values(this._def.values), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
2935
|
-
var val = _c.value;
|
|
2936
|
-
enumValues[val] = val;
|
|
2937
|
-
}
|
|
2938
|
-
}
|
|
2939
|
-
catch (e_17_1) { e_17 = { error: e_17_1 }; }
|
|
2940
|
-
finally {
|
|
2941
|
-
try {
|
|
2942
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
2943
|
-
}
|
|
2944
|
-
finally { if (e_17) throw e_17.error; }
|
|
2945
|
-
}
|
|
2946
|
-
return enumValues;
|
|
2947
|
-
},
|
|
2948
|
-
enumerable: false,
|
|
2949
|
-
configurable: true
|
|
2950
|
-
});
|
|
2951
|
-
ZodEnum.create = createZodEnum;
|
|
2952
|
-
return ZodEnum;
|
|
2953
|
-
}(ZodType));
|
|
2954
|
-
var ZodNativeEnum = /** @class */ (function (_super) {
|
|
2955
|
-
__extends(ZodNativeEnum, _super);
|
|
2956
|
-
function ZodNativeEnum() {
|
|
2957
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
2958
|
-
}
|
|
2959
|
-
ZodNativeEnum.prototype._parse = function (input) {
|
|
2960
|
-
var ctx = this._processInputParams(input).ctx;
|
|
2961
|
-
var nativeEnumValues = util.getValidEnumValues(this._def.values);
|
|
2365
|
+
}
|
|
2366
|
+
get options() {
|
|
2367
|
+
return this._def.values;
|
|
2368
|
+
}
|
|
2369
|
+
get enum() {
|
|
2370
|
+
const enumValues = {};
|
|
2371
|
+
for (const val of this._def.values) {
|
|
2372
|
+
enumValues[val] = val;
|
|
2373
|
+
}
|
|
2374
|
+
return enumValues;
|
|
2375
|
+
}
|
|
2376
|
+
get Values() {
|
|
2377
|
+
const enumValues = {};
|
|
2378
|
+
for (const val of this._def.values) {
|
|
2379
|
+
enumValues[val] = val;
|
|
2380
|
+
}
|
|
2381
|
+
return enumValues;
|
|
2382
|
+
}
|
|
2383
|
+
get Enum() {
|
|
2384
|
+
const enumValues = {};
|
|
2385
|
+
for (const val of this._def.values) {
|
|
2386
|
+
enumValues[val] = val;
|
|
2387
|
+
}
|
|
2388
|
+
return enumValues;
|
|
2389
|
+
}
|
|
2390
|
+
}
|
|
2391
|
+
ZodEnum.create = createZodEnum;
|
|
2392
|
+
class ZodNativeEnum extends ZodType {
|
|
2393
|
+
_parse(input) {
|
|
2394
|
+
const { ctx } = this._processInputParams(input);
|
|
2395
|
+
const nativeEnumValues = util.getValidEnumValues(this._def.values);
|
|
2962
2396
|
if (nativeEnumValues.indexOf(ctx.data) === -1) {
|
|
2963
2397
|
addIssueToContext(ctx, {
|
|
2964
2398
|
code: ZodIssueCode.invalid_enum_value,
|
|
@@ -2967,20 +2401,21 @@ var ZodNativeEnum = /** @class */ (function (_super) {
|
|
|
2967
2401
|
return INVALID;
|
|
2968
2402
|
}
|
|
2969
2403
|
return OK(ctx.data);
|
|
2970
|
-
}
|
|
2971
|
-
|
|
2972
|
-
return
|
|
2973
|
-
}
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
}
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2404
|
+
}
|
|
2405
|
+
get enum() {
|
|
2406
|
+
return this._def.values;
|
|
2407
|
+
}
|
|
2408
|
+
}
|
|
2409
|
+
ZodNativeEnum.create = (values, params) => {
|
|
2410
|
+
return new ZodNativeEnum({
|
|
2411
|
+
values: values,
|
|
2412
|
+
typeName: ZodFirstPartyTypeKind.ZodNativeEnum,
|
|
2413
|
+
...processCreateParams(params),
|
|
2414
|
+
});
|
|
2415
|
+
};
|
|
2416
|
+
class ZodPromise extends ZodType {
|
|
2417
|
+
_parse(input) {
|
|
2418
|
+
const { ctx } = this._processInputParams(input);
|
|
2984
2419
|
if (ctx.parsedType !== ZodParsedType.promise && ctx.async === false) {
|
|
2985
2420
|
addIssueToContext(ctx, {
|
|
2986
2421
|
code: ZodIssueCode.invalid_type,
|
|
@@ -2989,38 +2424,36 @@ var ZodPromise = /** @class */ (function (_super) {
|
|
|
2989
2424
|
});
|
|
2990
2425
|
return INVALID;
|
|
2991
2426
|
}
|
|
2992
|
-
|
|
2427
|
+
const promisified = ctx.parsedType === ZodParsedType.promise
|
|
2993
2428
|
? ctx.data
|
|
2994
2429
|
: Promise.resolve(ctx.data);
|
|
2995
|
-
return OK(promisified.then(
|
|
2996
|
-
return
|
|
2430
|
+
return OK(promisified.then((data) => {
|
|
2431
|
+
return this._def.type.parseAsync(data, {
|
|
2997
2432
|
path: ctx.path,
|
|
2998
2433
|
errorMap: ctx.contextualErrorMap,
|
|
2999
2434
|
});
|
|
3000
2435
|
}));
|
|
3001
|
-
}
|
|
3002
|
-
|
|
3003
|
-
|
|
3004
|
-
|
|
3005
|
-
|
|
3006
|
-
|
|
3007
|
-
|
|
3008
|
-
|
|
3009
|
-
|
|
3010
|
-
|
|
3011
|
-
|
|
3012
|
-
ZodEffects.prototype.innerType = function () {
|
|
2436
|
+
}
|
|
2437
|
+
}
|
|
2438
|
+
ZodPromise.create = (schema, params) => {
|
|
2439
|
+
return new ZodPromise({
|
|
2440
|
+
type: schema,
|
|
2441
|
+
typeName: ZodFirstPartyTypeKind.ZodPromise,
|
|
2442
|
+
...processCreateParams(params),
|
|
2443
|
+
});
|
|
2444
|
+
};
|
|
2445
|
+
class ZodEffects extends ZodType {
|
|
2446
|
+
innerType() {
|
|
3013
2447
|
return this._def.schema;
|
|
3014
|
-
}
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
|
|
3018
|
-
var effect = this._def.effect || null;
|
|
2448
|
+
}
|
|
2449
|
+
_parse(input) {
|
|
2450
|
+
const { status, ctx } = this._processInputParams(input);
|
|
2451
|
+
const effect = this._def.effect || null;
|
|
3019
2452
|
if (effect.type === "preprocess") {
|
|
3020
|
-
|
|
2453
|
+
const processed = effect.transform(ctx.data);
|
|
3021
2454
|
if (ctx.async) {
|
|
3022
|
-
return Promise.resolve(processed).then(
|
|
3023
|
-
return
|
|
2455
|
+
return Promise.resolve(processed).then((processed) => {
|
|
2456
|
+
return this._def.schema._parseAsync({
|
|
3024
2457
|
data: processed,
|
|
3025
2458
|
path: ctx.path,
|
|
3026
2459
|
parent: ctx,
|
|
@@ -3036,8 +2469,8 @@ var ZodEffects = /** @class */ (function (_super) {
|
|
|
3036
2469
|
}
|
|
3037
2470
|
}
|
|
3038
2471
|
if (effect.type === "refinement") {
|
|
3039
|
-
|
|
3040
|
-
addIssue:
|
|
2472
|
+
const checkCtx = {
|
|
2473
|
+
addIssue: (arg) => {
|
|
3041
2474
|
addIssueToContext(ctx, arg);
|
|
3042
2475
|
if (arg.fatal) {
|
|
3043
2476
|
status.abort();
|
|
@@ -3050,11 +2483,11 @@ var ZodEffects = /** @class */ (function (_super) {
|
|
|
3050
2483
|
return ctx.path;
|
|
3051
2484
|
},
|
|
3052
2485
|
};
|
|
3053
|
-
|
|
3054
|
-
|
|
2486
|
+
checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
|
|
2487
|
+
const executeRefinement = (acc
|
|
3055
2488
|
// effect: RefinementEffect<any>
|
|
3056
|
-
) {
|
|
3057
|
-
|
|
2489
|
+
) => {
|
|
2490
|
+
const result = effect.refinement(acc, checkCtx);
|
|
3058
2491
|
if (ctx.async) {
|
|
3059
2492
|
return Promise.resolve(result);
|
|
3060
2493
|
}
|
|
@@ -3064,7 +2497,7 @@ var ZodEffects = /** @class */ (function (_super) {
|
|
|
3064
2497
|
return acc;
|
|
3065
2498
|
};
|
|
3066
2499
|
if (ctx.async === false) {
|
|
3067
|
-
|
|
2500
|
+
const inner = this._def.schema._parseSync({
|
|
3068
2501
|
data: ctx.data,
|
|
3069
2502
|
path: ctx.path,
|
|
3070
2503
|
parent: ctx,
|
|
@@ -3074,18 +2507,18 @@ var ZodEffects = /** @class */ (function (_super) {
|
|
|
3074
2507
|
if (inner.status === "dirty")
|
|
3075
2508
|
status.dirty();
|
|
3076
2509
|
// return value is ignored
|
|
3077
|
-
|
|
2510
|
+
executeRefinement(inner.value);
|
|
3078
2511
|
return { status: status.value, value: inner.value };
|
|
3079
2512
|
}
|
|
3080
2513
|
else {
|
|
3081
2514
|
return this._def.schema
|
|
3082
2515
|
._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx })
|
|
3083
|
-
.then(
|
|
2516
|
+
.then((inner) => {
|
|
3084
2517
|
if (inner.status === "aborted")
|
|
3085
2518
|
return INVALID;
|
|
3086
2519
|
if (inner.status === "dirty")
|
|
3087
2520
|
status.dirty();
|
|
3088
|
-
return
|
|
2521
|
+
return executeRefinement(inner.value).then(() => {
|
|
3089
2522
|
return { status: status.value, value: inner.value };
|
|
3090
2523
|
});
|
|
3091
2524
|
});
|
|
@@ -3093,7 +2526,7 @@ var ZodEffects = /** @class */ (function (_super) {
|
|
|
3093
2526
|
}
|
|
3094
2527
|
if (effect.type === "transform") {
|
|
3095
2528
|
if (ctx.async === false) {
|
|
3096
|
-
|
|
2529
|
+
const base = this._def.schema._parseSync({
|
|
3097
2530
|
data: ctx.data,
|
|
3098
2531
|
path: ctx.path,
|
|
3099
2532
|
parent: ctx,
|
|
@@ -3104,16 +2537,16 @@ var ZodEffects = /** @class */ (function (_super) {
|
|
|
3104
2537
|
// }
|
|
3105
2538
|
if (!isValid(base))
|
|
3106
2539
|
return base;
|
|
3107
|
-
|
|
2540
|
+
const result = effect.transform(base.value);
|
|
3108
2541
|
if (result instanceof Promise) {
|
|
3109
|
-
throw new Error(
|
|
2542
|
+
throw new Error(`Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.`);
|
|
3110
2543
|
}
|
|
3111
2544
|
return OK(result);
|
|
3112
2545
|
}
|
|
3113
2546
|
else {
|
|
3114
2547
|
return this._def.schema
|
|
3115
2548
|
._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx })
|
|
3116
|
-
.then(
|
|
2549
|
+
.then((base) => {
|
|
3117
2550
|
if (!isValid(base))
|
|
3118
2551
|
return base;
|
|
3119
2552
|
// if (base.status === "aborted") return INVALID;
|
|
@@ -3125,22 +2558,27 @@ var ZodEffects = /** @class */ (function (_super) {
|
|
|
3125
2558
|
}
|
|
3126
2559
|
}
|
|
3127
2560
|
util.assertNever(effect);
|
|
3128
|
-
}
|
|
3129
|
-
|
|
3130
|
-
|
|
3131
|
-
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
-
|
|
3135
|
-
|
|
3136
|
-
}
|
|
3137
|
-
|
|
3138
|
-
|
|
3139
|
-
|
|
3140
|
-
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
|
|
2561
|
+
}
|
|
2562
|
+
}
|
|
2563
|
+
ZodEffects.create = (schema, effect, params) => {
|
|
2564
|
+
return new ZodEffects({
|
|
2565
|
+
schema,
|
|
2566
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
2567
|
+
effect,
|
|
2568
|
+
...processCreateParams(params),
|
|
2569
|
+
});
|
|
2570
|
+
};
|
|
2571
|
+
ZodEffects.createWithPreprocess = (preprocess, schema, params) => {
|
|
2572
|
+
return new ZodEffects({
|
|
2573
|
+
schema,
|
|
2574
|
+
effect: { type: "preprocess", transform: preprocess },
|
|
2575
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
2576
|
+
...processCreateParams(params),
|
|
2577
|
+
});
|
|
2578
|
+
};
|
|
2579
|
+
class ZodOptional extends ZodType {
|
|
2580
|
+
_parse(input) {
|
|
2581
|
+
const { ctx } = this._processInputParams(input);
|
|
3144
2582
|
if (ctx.parsedType === ZodParsedType.undefined) {
|
|
3145
2583
|
return OK(undefined);
|
|
3146
2584
|
}
|
|
@@ -3149,22 +2587,21 @@ var ZodOptional = /** @class */ (function (_super) {
|
|
|
3149
2587
|
path: ctx.path,
|
|
3150
2588
|
parent: ctx,
|
|
3151
2589
|
});
|
|
3152
|
-
}
|
|
3153
|
-
|
|
2590
|
+
}
|
|
2591
|
+
unwrap() {
|
|
3154
2592
|
return this._def.innerType;
|
|
3155
|
-
}
|
|
3156
|
-
|
|
3157
|
-
|
|
3158
|
-
|
|
3159
|
-
|
|
3160
|
-
|
|
3161
|
-
|
|
3162
|
-
|
|
3163
|
-
|
|
3164
|
-
|
|
3165
|
-
|
|
3166
|
-
|
|
3167
|
-
var ctx = this._processInputParams(input).ctx;
|
|
2593
|
+
}
|
|
2594
|
+
}
|
|
2595
|
+
ZodOptional.create = (type, params) => {
|
|
2596
|
+
return new ZodOptional({
|
|
2597
|
+
innerType: type,
|
|
2598
|
+
typeName: ZodFirstPartyTypeKind.ZodOptional,
|
|
2599
|
+
...processCreateParams(params),
|
|
2600
|
+
});
|
|
2601
|
+
};
|
|
2602
|
+
class ZodNullable extends ZodType {
|
|
2603
|
+
_parse(input) {
|
|
2604
|
+
const { ctx } = this._processInputParams(input);
|
|
3168
2605
|
if (ctx.parsedType === ZodParsedType.null) {
|
|
3169
2606
|
return OK(null);
|
|
3170
2607
|
}
|
|
@@ -3173,47 +2610,45 @@ var ZodNullable = /** @class */ (function (_super) {
|
|
|
3173
2610
|
path: ctx.path,
|
|
3174
2611
|
parent: ctx,
|
|
3175
2612
|
});
|
|
3176
|
-
}
|
|
3177
|
-
|
|
2613
|
+
}
|
|
2614
|
+
unwrap() {
|
|
3178
2615
|
return this._def.innerType;
|
|
3179
|
-
}
|
|
3180
|
-
|
|
3181
|
-
|
|
3182
|
-
|
|
3183
|
-
|
|
3184
|
-
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
|
|
3189
|
-
|
|
3190
|
-
|
|
3191
|
-
|
|
3192
|
-
var data = ctx.data;
|
|
2616
|
+
}
|
|
2617
|
+
}
|
|
2618
|
+
ZodNullable.create = (type, params) => {
|
|
2619
|
+
return new ZodNullable({
|
|
2620
|
+
innerType: type,
|
|
2621
|
+
typeName: ZodFirstPartyTypeKind.ZodNullable,
|
|
2622
|
+
...processCreateParams(params),
|
|
2623
|
+
});
|
|
2624
|
+
};
|
|
2625
|
+
class ZodDefault extends ZodType {
|
|
2626
|
+
_parse(input) {
|
|
2627
|
+
const { ctx } = this._processInputParams(input);
|
|
2628
|
+
let data = ctx.data;
|
|
3193
2629
|
if (ctx.parsedType === ZodParsedType.undefined) {
|
|
3194
2630
|
data = this._def.defaultValue();
|
|
3195
2631
|
}
|
|
3196
2632
|
return this._def.innerType._parse({
|
|
3197
|
-
data
|
|
2633
|
+
data,
|
|
3198
2634
|
path: ctx.path,
|
|
3199
2635
|
parent: ctx,
|
|
3200
2636
|
});
|
|
3201
|
-
}
|
|
3202
|
-
|
|
2637
|
+
}
|
|
2638
|
+
removeDefault() {
|
|
3203
2639
|
return this._def.innerType;
|
|
3204
|
-
}
|
|
3205
|
-
|
|
3206
|
-
|
|
3207
|
-
|
|
3208
|
-
|
|
3209
|
-
|
|
3210
|
-
|
|
3211
|
-
|
|
3212
|
-
|
|
3213
|
-
|
|
3214
|
-
|
|
3215
|
-
|
|
3216
|
-
var _a = this._processInputParams(input), status = _a.status, ctx = _a.ctx;
|
|
2640
|
+
}
|
|
2641
|
+
}
|
|
2642
|
+
ZodDefault.create = (type, params) => {
|
|
2643
|
+
return new ZodOptional({
|
|
2644
|
+
innerType: type,
|
|
2645
|
+
typeName: ZodFirstPartyTypeKind.ZodOptional,
|
|
2646
|
+
...processCreateParams(params),
|
|
2647
|
+
});
|
|
2648
|
+
};
|
|
2649
|
+
class ZodNaN extends ZodType {
|
|
2650
|
+
_parse(input) {
|
|
2651
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3217
2652
|
if (ctx.parsedType !== ZodParsedType.nan) {
|
|
3218
2653
|
addIssueToContext(ctx, {
|
|
3219
2654
|
code: ZodIssueCode.invalid_type,
|
|
@@ -3223,18 +2658,20 @@ var ZodNaN = /** @class */ (function (_super) {
|
|
|
3223
2658
|
return INVALID;
|
|
3224
2659
|
}
|
|
3225
2660
|
return { status: status.value, value: ctx.data };
|
|
3226
|
-
}
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
|
|
2661
|
+
}
|
|
2662
|
+
}
|
|
2663
|
+
ZodNaN.create = (params) => {
|
|
2664
|
+
return new ZodNaN({
|
|
2665
|
+
typeName: ZodFirstPartyTypeKind.ZodNaN,
|
|
2666
|
+
...processCreateParams(params),
|
|
2667
|
+
});
|
|
2668
|
+
};
|
|
2669
|
+
const custom = (check, params) => {
|
|
3233
2670
|
if (check)
|
|
3234
2671
|
return ZodAny.create().refine(check, params);
|
|
3235
2672
|
return ZodAny.create();
|
|
3236
2673
|
};
|
|
3237
|
-
|
|
2674
|
+
const late = {
|
|
3238
2675
|
object: ZodObject.lazycreate,
|
|
3239
2676
|
};
|
|
3240
2677
|
var ZodFirstPartyTypeKind;
|
|
@@ -3271,47 +2708,44 @@ var ZodFirstPartyTypeKind;
|
|
|
3271
2708
|
ZodFirstPartyTypeKind["ZodDefault"] = "ZodDefault";
|
|
3272
2709
|
ZodFirstPartyTypeKind["ZodPromise"] = "ZodPromise";
|
|
3273
2710
|
})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
|
|
3274
|
-
|
|
3275
|
-
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
|
|
3279
|
-
|
|
3280
|
-
|
|
3281
|
-
|
|
3282
|
-
|
|
3283
|
-
|
|
3284
|
-
|
|
3285
|
-
|
|
3286
|
-
|
|
3287
|
-
|
|
3288
|
-
|
|
3289
|
-
|
|
3290
|
-
|
|
3291
|
-
|
|
3292
|
-
|
|
3293
|
-
|
|
3294
|
-
|
|
3295
|
-
|
|
3296
|
-
|
|
3297
|
-
|
|
3298
|
-
|
|
3299
|
-
|
|
3300
|
-
|
|
3301
|
-
|
|
3302
|
-
|
|
3303
|
-
|
|
3304
|
-
|
|
3305
|
-
|
|
3306
|
-
|
|
3307
|
-
|
|
3308
|
-
|
|
3309
|
-
|
|
3310
|
-
|
|
3311
|
-
|
|
3312
|
-
var ostring = function () { return stringType().optional(); };
|
|
3313
|
-
var onumber = function () { return numberType().optional(); };
|
|
3314
|
-
var oboolean = function () { return booleanType().optional(); };
|
|
2711
|
+
const instanceOfType = (cls, params = {
|
|
2712
|
+
message: `Input not instance of ${cls.name}`,
|
|
2713
|
+
}) => custom((data) => data instanceof cls, params);
|
|
2714
|
+
const stringType = ZodString.create;
|
|
2715
|
+
const numberType = ZodNumber.create;
|
|
2716
|
+
const nanType = ZodNaN.create;
|
|
2717
|
+
const bigIntType = ZodBigInt.create;
|
|
2718
|
+
const booleanType = ZodBoolean.create;
|
|
2719
|
+
const dateType = ZodDate.create;
|
|
2720
|
+
const undefinedType = ZodUndefined.create;
|
|
2721
|
+
const nullType = ZodNull.create;
|
|
2722
|
+
const anyType = ZodAny.create;
|
|
2723
|
+
const unknownType = ZodUnknown.create;
|
|
2724
|
+
const neverType = ZodNever.create;
|
|
2725
|
+
const voidType = ZodVoid.create;
|
|
2726
|
+
const arrayType = ZodArray.create;
|
|
2727
|
+
const objectType = ZodObject.create;
|
|
2728
|
+
const strictObjectType = ZodObject.strictCreate;
|
|
2729
|
+
const unionType = ZodUnion.create;
|
|
2730
|
+
const discriminatedUnionType = ZodDiscriminatedUnion.create;
|
|
2731
|
+
const intersectionType = ZodIntersection.create;
|
|
2732
|
+
const tupleType = ZodTuple.create;
|
|
2733
|
+
const recordType = ZodRecord.create;
|
|
2734
|
+
const mapType = ZodMap.create;
|
|
2735
|
+
const setType = ZodSet.create;
|
|
2736
|
+
const functionType = ZodFunction.create;
|
|
2737
|
+
const lazyType = ZodLazy.create;
|
|
2738
|
+
const literalType = ZodLiteral.create;
|
|
2739
|
+
const enumType = ZodEnum.create;
|
|
2740
|
+
const nativeEnumType = ZodNativeEnum.create;
|
|
2741
|
+
const promiseType = ZodPromise.create;
|
|
2742
|
+
const effectsType = ZodEffects.create;
|
|
2743
|
+
const optionalType = ZodOptional.create;
|
|
2744
|
+
const nullableType = ZodNullable.create;
|
|
2745
|
+
const preprocessType = ZodEffects.createWithPreprocess;
|
|
2746
|
+
const ostring = () => stringType().optional();
|
|
2747
|
+
const onumber = () => numberType().optional();
|
|
2748
|
+
const oboolean = () => booleanType().optional();
|
|
3315
2749
|
|
|
3316
2750
|
var mod = /*#__PURE__*/Object.freeze({
|
|
3317
2751
|
__proto__: null,
|
|
@@ -3412,5 +2846,4 @@ var mod = /*#__PURE__*/Object.freeze({
|
|
|
3412
2846
|
setErrorMap: setErrorMap
|
|
3413
2847
|
});
|
|
3414
2848
|
|
|
3415
|
-
export default mod;
|
|
3416
|
-
export { DIRTY, EMPTY_PATH, INVALID, OK, ParseStatus, ZodType as Schema, ZodAny, ZodArray, ZodBigInt, ZodBoolean, ZodDate, ZodDefault, ZodDiscriminatedUnion, ZodEffects, ZodEnum, ZodError, ZodFirstPartyTypeKind, ZodFunction, ZodIntersection, ZodIssueCode, ZodLazy, ZodLiteral, ZodMap, ZodNaN, ZodNativeEnum, ZodNever, ZodNull, ZodNullable, ZodNumber, ZodObject, ZodOptional, ZodParsedType, ZodPromise, ZodRecord, ZodType as ZodSchema, ZodSet, ZodString, ZodEffects as ZodTransformer, ZodTuple, ZodType, ZodUndefined, ZodUnion, ZodUnknown, ZodVoid, addIssueToContext, anyType as any, arrayType as array, bigIntType as bigint, booleanType as boolean, custom, dateType as date, defaultErrorMap, discriminatedUnionType as discriminatedUnion, effectsType as effect, enumType as enum, functionType as function, getParsedType, instanceOfType as instanceof, intersectionType as intersection, isAborted, isAsync, isDirty, isValid, late, lazyType as lazy, literalType as literal, makeIssue, mapType as map, nanType as nan, nativeEnumType as nativeEnum, neverType as never, nullType as null, nullableType as nullable, numberType as number, objectType as object, objectUtil, oboolean, onumber, optionalType as optional, ostring, overrideErrorMap, preprocessType as preprocess, promiseType as promise, quotelessJson, recordType as record, setType as set, setErrorMap, strictObjectType as strictObject, stringType as string, effectsType as transformer, tupleType as tuple, undefinedType as undefined, unionType as union, unknownType as unknown, voidType as void, mod as z };
|
|
2849
|
+
export { DIRTY, EMPTY_PATH, INVALID, OK, ParseStatus, ZodType as Schema, ZodAny, ZodArray, ZodBigInt, ZodBoolean, ZodDate, ZodDefault, ZodDiscriminatedUnion, ZodEffects, ZodEnum, ZodError, ZodFirstPartyTypeKind, ZodFunction, ZodIntersection, ZodIssueCode, ZodLazy, ZodLiteral, ZodMap, ZodNaN, ZodNativeEnum, ZodNever, ZodNull, ZodNullable, ZodNumber, ZodObject, ZodOptional, ZodParsedType, ZodPromise, ZodRecord, ZodType as ZodSchema, ZodSet, ZodString, ZodEffects as ZodTransformer, ZodTuple, ZodType, ZodUndefined, ZodUnion, ZodUnknown, ZodVoid, addIssueToContext, anyType as any, arrayType as array, bigIntType as bigint, booleanType as boolean, custom, dateType as date, mod as default, defaultErrorMap, discriminatedUnionType as discriminatedUnion, effectsType as effect, enumType as enum, functionType as function, getParsedType, instanceOfType as instanceof, intersectionType as intersection, isAborted, isAsync, isDirty, isValid, late, lazyType as lazy, literalType as literal, makeIssue, mapType as map, nanType as nan, nativeEnumType as nativeEnum, neverType as never, nullType as null, nullableType as nullable, numberType as number, objectType as object, objectUtil, oboolean, onumber, optionalType as optional, ostring, overrideErrorMap, preprocessType as preprocess, promiseType as promise, quotelessJson, recordType as record, setType as set, setErrorMap, strictObjectType as strictObject, stringType as string, effectsType as transformer, tupleType as tuple, undefinedType as undefined, unionType as union, unknownType as unknown, voidType as void, mod as z };
|