zod 3.13.4 → 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 +4 -3
- 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/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 +1 -1
- package/lib/index.mjs +1527 -2101
- package/lib/types.d.ts +1 -1
- package/lib/types.js +1318 -1711
- package/package.json +2 -1
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,23 +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
|
|
1695
|
-
|
|
1696
|
-
|
|
1335
|
+
const AugmentFactory = (def) => (augmentation) => {
|
|
1336
|
+
return new ZodObject({
|
|
1337
|
+
...def,
|
|
1338
|
+
shape: () => ({
|
|
1339
|
+
...def.shape(),
|
|
1340
|
+
...augmentation,
|
|
1341
|
+
}),
|
|
1342
|
+
});
|
|
1697
1343
|
};
|
|
1698
1344
|
function deepPartialify(schema) {
|
|
1699
1345
|
if (schema instanceof ZodObject) {
|
|
1700
|
-
|
|
1701
|
-
for (
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
}
|
|
1705
|
-
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
|
+
});
|
|
1706
1355
|
}
|
|
1707
1356
|
else if (schema instanceof ZodArray) {
|
|
1708
1357
|
return ZodArray.create(deepPartialify(schema.element));
|
|
@@ -1714,37 +1363,33 @@ function deepPartialify(schema) {
|
|
|
1714
1363
|
return ZodNullable.create(deepPartialify(schema.unwrap()));
|
|
1715
1364
|
}
|
|
1716
1365
|
else if (schema instanceof ZodTuple) {
|
|
1717
|
-
return ZodTuple.create(schema.items.map(
|
|
1366
|
+
return ZodTuple.create(schema.items.map((item) => deepPartialify(item)));
|
|
1718
1367
|
}
|
|
1719
1368
|
else {
|
|
1720
1369
|
return schema;
|
|
1721
1370
|
}
|
|
1722
1371
|
}
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
_this._cached = null;
|
|
1372
|
+
class ZodObject extends ZodType {
|
|
1373
|
+
constructor() {
|
|
1374
|
+
super(...arguments);
|
|
1375
|
+
this._cached = null;
|
|
1728
1376
|
/**
|
|
1729
1377
|
* @deprecated In most cases, this is no longer needed - unknown properties are now silently stripped.
|
|
1730
1378
|
* If you want to pass through unknown properties, use `.passthrough()` instead.
|
|
1731
1379
|
*/
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
return _this;
|
|
1380
|
+
this.nonstrict = this.passthrough;
|
|
1381
|
+
this.augment = AugmentFactory(this._def);
|
|
1382
|
+
this.extend = AugmentFactory(this._def);
|
|
1736
1383
|
}
|
|
1737
|
-
|
|
1384
|
+
_getCached() {
|
|
1738
1385
|
if (this._cached !== null)
|
|
1739
1386
|
return this._cached;
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
return (this._cached = { shape
|
|
1743
|
-
}
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
var _this = this;
|
|
1747
|
-
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);
|
|
1748
1393
|
if (ctx.parsedType !== ZodParsedType.object) {
|
|
1749
1394
|
addIssueToContext(ctx, {
|
|
1750
1395
|
code: ZodIssueCode.invalid_type,
|
|
@@ -1753,51 +1398,31 @@ var ZodObject = /** @class */ (function (_super) {
|
|
|
1753
1398
|
});
|
|
1754
1399
|
return INVALID;
|
|
1755
1400
|
}
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
alwaysSet: key in ctx.data,
|
|
1773
|
-
});
|
|
1774
|
-
}
|
|
1775
|
-
}
|
|
1776
|
-
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
1777
|
-
finally {
|
|
1778
|
-
try {
|
|
1779
|
-
if (shapeKeys_1_1 && !shapeKeys_1_1.done && (_a = shapeKeys_1.return)) _a.call(shapeKeys_1);
|
|
1780
|
-
}
|
|
1781
|
-
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
|
+
});
|
|
1782
1417
|
}
|
|
1783
1418
|
if (this._def.catchall instanceof ZodNever) {
|
|
1784
|
-
|
|
1419
|
+
const unknownKeys = this._def.unknownKeys;
|
|
1785
1420
|
if (unknownKeys === "passthrough") {
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
value: { status: "valid", value: ctx.data[key] },
|
|
1792
|
-
});
|
|
1793
|
-
}
|
|
1794
|
-
}
|
|
1795
|
-
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
1796
|
-
finally {
|
|
1797
|
-
try {
|
|
1798
|
-
if (extraKeys_1_1 && !extraKeys_1_1.done && (_b = extraKeys_1.return)) _b.call(extraKeys_1);
|
|
1799
|
-
}
|
|
1800
|
-
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
|
+
});
|
|
1801
1426
|
}
|
|
1802
1427
|
}
|
|
1803
1428
|
else if (unknownKeys === "strict") {
|
|
@@ -1811,321 +1436,296 @@ var ZodObject = /** @class */ (function (_super) {
|
|
|
1811
1436
|
}
|
|
1812
1437
|
else if (unknownKeys === "strip") ;
|
|
1813
1438
|
else {
|
|
1814
|
-
throw new Error(
|
|
1439
|
+
throw new Error(`Internal ZodObject error: invalid unknownKeys value.`);
|
|
1815
1440
|
}
|
|
1816
1441
|
}
|
|
1817
1442
|
else {
|
|
1818
1443
|
// run catchall validation
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
alwaysSet: key in ctx.data,
|
|
1829
|
-
});
|
|
1830
|
-
}
|
|
1831
|
-
}
|
|
1832
|
-
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
1833
|
-
finally {
|
|
1834
|
-
try {
|
|
1835
|
-
if (extraKeys_2_1 && !extraKeys_2_1.done && (_c = extraKeys_2.return)) _c.call(extraKeys_2);
|
|
1836
|
-
}
|
|
1837
|
-
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
|
+
});
|
|
1838
1453
|
}
|
|
1839
1454
|
}
|
|
1840
1455
|
if (ctx.async) {
|
|
1841
1456
|
return Promise.resolve()
|
|
1842
|
-
.then(
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
if (!!pairs_1_1.done) return [3 /*break*/, 6];
|
|
1856
|
-
pair = pairs_1_1.value;
|
|
1857
|
-
return [4 /*yield*/, pair.key];
|
|
1858
|
-
case 3:
|
|
1859
|
-
key = _e.sent();
|
|
1860
|
-
_b = (_a = syncPairs).push;
|
|
1861
|
-
_d = {
|
|
1862
|
-
key: key
|
|
1863
|
-
};
|
|
1864
|
-
return [4 /*yield*/, pair.value];
|
|
1865
|
-
case 4:
|
|
1866
|
-
_b.apply(_a, [(_d.value = _e.sent(),
|
|
1867
|
-
_d.alwaysSet = pair.alwaysSet,
|
|
1868
|
-
_d)]);
|
|
1869
|
-
_e.label = 5;
|
|
1870
|
-
case 5:
|
|
1871
|
-
pairs_1_1 = pairs_1.next();
|
|
1872
|
-
return [3 /*break*/, 2];
|
|
1873
|
-
case 6: return [3 /*break*/, 9];
|
|
1874
|
-
case 7:
|
|
1875
|
-
e_8_1 = _e.sent();
|
|
1876
|
-
e_8 = { error: e_8_1 };
|
|
1877
|
-
return [3 /*break*/, 9];
|
|
1878
|
-
case 8:
|
|
1879
|
-
try {
|
|
1880
|
-
if (pairs_1_1 && !pairs_1_1.done && (_c = pairs_1.return)) _c.call(pairs_1);
|
|
1881
|
-
}
|
|
1882
|
-
finally { if (e_8) throw e_8.error; }
|
|
1883
|
-
return [7 /*endfinally*/];
|
|
1884
|
-
case 9: return [2 /*return*/, syncPairs];
|
|
1885
|
-
}
|
|
1886
|
-
});
|
|
1887
|
-
}); })
|
|
1888
|
-
.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) => {
|
|
1889
1470
|
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
1890
1471
|
});
|
|
1891
1472
|
}
|
|
1892
1473
|
else {
|
|
1893
1474
|
return ParseStatus.mergeObjectSync(status, pairs);
|
|
1894
1475
|
}
|
|
1895
|
-
}
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
enumerable: false,
|
|
1901
|
-
configurable: true
|
|
1902
|
-
});
|
|
1903
|
-
ZodObject.prototype.strict = function (message) {
|
|
1904
|
-
var _this = this;
|
|
1476
|
+
}
|
|
1477
|
+
get shape() {
|
|
1478
|
+
return this._def.shape();
|
|
1479
|
+
}
|
|
1480
|
+
strict(message) {
|
|
1905
1481
|
errorUtil.errToObj;
|
|
1906
|
-
return new ZodObject(
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
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
|
+
};
|
|
1912
1494
|
return {
|
|
1913
|
-
message:
|
|
1495
|
+
message: defaultError,
|
|
1914
1496
|
};
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
}
|
|
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
|
+
}
|
|
1932
1517
|
/**
|
|
1933
1518
|
* Prior to zod@1.0.12 there was a bug in the
|
|
1934
1519
|
* inferred type of merged objects. Please
|
|
1935
1520
|
* upgrade if you are experiencing issues.
|
|
1936
1521
|
*/
|
|
1937
|
-
|
|
1938
|
-
var _this = this;
|
|
1522
|
+
merge(merging) {
|
|
1939
1523
|
// const mergedShape = objectUtil.mergeShapes(
|
|
1940
1524
|
// this._def.shape(),
|
|
1941
1525
|
// merging._def.shape()
|
|
1942
1526
|
// );
|
|
1943
|
-
|
|
1527
|
+
const merged = new ZodObject({
|
|
1944
1528
|
unknownKeys: merging._def.unknownKeys,
|
|
1945
1529
|
catchall: merging._def.catchall,
|
|
1946
|
-
shape:
|
|
1947
|
-
return objectUtil.mergeShapes(_this._def.shape(), merging._def.shape());
|
|
1948
|
-
},
|
|
1530
|
+
shape: () => objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
1949
1531
|
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
1950
1532
|
});
|
|
1951
1533
|
return merged;
|
|
1952
|
-
}
|
|
1953
|
-
|
|
1954
|
-
return new ZodObject(
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
var _this = this;
|
|
1958
|
-
var shape = {};
|
|
1959
|
-
util.objectKeys(mask).map(function (key) {
|
|
1960
|
-
shape[key] = _this.shape[key];
|
|
1534
|
+
}
|
|
1535
|
+
catchall(index) {
|
|
1536
|
+
return new ZodObject({
|
|
1537
|
+
...this._def,
|
|
1538
|
+
catchall: index,
|
|
1961
1539
|
});
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
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) => {
|
|
1968
1554
|
if (util.objectKeys(mask).indexOf(key) === -1) {
|
|
1969
|
-
shape[key] =
|
|
1555
|
+
shape[key] = this.shape[key];
|
|
1970
1556
|
}
|
|
1971
1557
|
});
|
|
1972
|
-
return new ZodObject(
|
|
1973
|
-
|
|
1974
|
-
|
|
1558
|
+
return new ZodObject({
|
|
1559
|
+
...this._def,
|
|
1560
|
+
shape: () => shape,
|
|
1561
|
+
});
|
|
1562
|
+
}
|
|
1563
|
+
deepPartial() {
|
|
1975
1564
|
return deepPartialify(this);
|
|
1976
|
-
}
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
var newShape = {};
|
|
1565
|
+
}
|
|
1566
|
+
partial(mask) {
|
|
1567
|
+
const newShape = {};
|
|
1980
1568
|
if (mask) {
|
|
1981
|
-
util.objectKeys(this.shape).map(
|
|
1569
|
+
util.objectKeys(this.shape).map((key) => {
|
|
1982
1570
|
if (util.objectKeys(mask).indexOf(key) === -1) {
|
|
1983
|
-
newShape[key] =
|
|
1571
|
+
newShape[key] = this.shape[key];
|
|
1984
1572
|
}
|
|
1985
1573
|
else {
|
|
1986
|
-
newShape[key] =
|
|
1574
|
+
newShape[key] = this.shape[key].optional();
|
|
1987
1575
|
}
|
|
1988
1576
|
});
|
|
1989
|
-
return new ZodObject(
|
|
1577
|
+
return new ZodObject({
|
|
1578
|
+
...this._def,
|
|
1579
|
+
shape: () => newShape,
|
|
1580
|
+
});
|
|
1990
1581
|
}
|
|
1991
1582
|
else {
|
|
1992
|
-
for (
|
|
1993
|
-
|
|
1583
|
+
for (const key in this.shape) {
|
|
1584
|
+
const fieldSchema = this.shape[key];
|
|
1994
1585
|
newShape[key] = fieldSchema.optional();
|
|
1995
1586
|
}
|
|
1996
1587
|
}
|
|
1997
|
-
return new ZodObject(
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
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;
|
|
2004
1598
|
while (newField instanceof ZodOptional) {
|
|
2005
1599
|
newField = newField._def.innerType;
|
|
2006
1600
|
}
|
|
2007
1601
|
newShape[key] = newField;
|
|
2008
1602
|
}
|
|
2009
|
-
return new ZodObject(
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
}
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
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;
|
|
2031
1640
|
function handleResults(results) {
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
var result = results_1_1.value;
|
|
2037
|
-
if (result.result.status === "valid") {
|
|
2038
|
-
return result.result;
|
|
2039
|
-
}
|
|
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;
|
|
2040
1645
|
}
|
|
2041
1646
|
}
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
finally { if (e_9) throw e_9.error; }
|
|
2048
|
-
}
|
|
2049
|
-
try {
|
|
2050
|
-
for (var results_2 = __values(results), results_2_1 = results_2.next(); !results_2_1.done; results_2_1 = results_2.next()) {
|
|
2051
|
-
var result = results_2_1.value;
|
|
2052
|
-
if (result.result.status === "dirty") {
|
|
2053
|
-
// add issues from dirty option
|
|
2054
|
-
(_c = ctx.issues).push.apply(_c, __spreadArray([], __read(result.ctx.issues), false));
|
|
2055
|
-
return result.result;
|
|
2056
|
-
}
|
|
2057
|
-
}
|
|
2058
|
-
}
|
|
2059
|
-
catch (e_10_1) { e_10 = { error: e_10_1 }; }
|
|
2060
|
-
finally {
|
|
2061
|
-
try {
|
|
2062
|
-
if (results_2_1 && !results_2_1.done && (_b = results_2.return)) _b.call(results_2);
|
|
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;
|
|
2063
1652
|
}
|
|
2064
|
-
finally { if (e_10) throw e_10.error; }
|
|
2065
1653
|
}
|
|
2066
1654
|
// return invalid
|
|
2067
|
-
|
|
1655
|
+
const unionErrors = results.map((result) => new ZodError(result.ctx.issues));
|
|
2068
1656
|
addIssueToContext(ctx, {
|
|
2069
1657
|
code: ZodIssueCode.invalid_union,
|
|
2070
|
-
unionErrors
|
|
1658
|
+
unionErrors,
|
|
2071
1659
|
});
|
|
2072
1660
|
return INVALID;
|
|
2073
1661
|
}
|
|
2074
1662
|
if (ctx.async) {
|
|
2075
|
-
return Promise.all(options.map(
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
childCtx = __assign(__assign({}, ctx), { issues: [], parent: null });
|
|
2082
|
-
_a = {};
|
|
2083
|
-
return [4 /*yield*/, option._parseAsync({
|
|
2084
|
-
data: ctx.data,
|
|
2085
|
-
path: ctx.path,
|
|
2086
|
-
parent: childCtx,
|
|
2087
|
-
})];
|
|
2088
|
-
case 1: return [2 /*return*/, (_a.result = _b.sent(),
|
|
2089
|
-
_a.ctx = childCtx,
|
|
2090
|
-
_a)];
|
|
2091
|
-
}
|
|
2092
|
-
});
|
|
2093
|
-
}); })).then(handleResults);
|
|
2094
|
-
}
|
|
2095
|
-
else {
|
|
2096
|
-
var optionResults = options.map(function (option) {
|
|
2097
|
-
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
|
+
};
|
|
2098
1669
|
return {
|
|
2099
|
-
result: option.
|
|
1670
|
+
result: await option._parseAsync({
|
|
2100
1671
|
data: ctx.data,
|
|
2101
1672
|
path: ctx.path,
|
|
2102
1673
|
parent: childCtx,
|
|
2103
1674
|
}),
|
|
2104
1675
|
ctx: childCtx,
|
|
2105
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,
|
|
2106
1711
|
});
|
|
2107
|
-
return
|
|
1712
|
+
return INVALID;
|
|
2108
1713
|
}
|
|
2109
|
-
}
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
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),
|
|
2116
1724
|
});
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
}(ZodType));
|
|
2122
|
-
var ZodDiscriminatedUnion = /** @class */ (function (_super) {
|
|
2123
|
-
__extends(ZodDiscriminatedUnion, _super);
|
|
2124
|
-
function ZodDiscriminatedUnion() {
|
|
2125
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
2126
|
-
}
|
|
2127
|
-
ZodDiscriminatedUnion.prototype._parse = function (input) {
|
|
2128
|
-
var ctx = this._processInputParams(input).ctx;
|
|
1725
|
+
};
|
|
1726
|
+
class ZodDiscriminatedUnion extends ZodType {
|
|
1727
|
+
_parse(input) {
|
|
1728
|
+
const { ctx } = this._processInputParams(input);
|
|
2129
1729
|
if (ctx.parsedType !== ZodParsedType.object) {
|
|
2130
1730
|
addIssueToContext(ctx, {
|
|
2131
1731
|
code: ZodIssueCode.invalid_type,
|
|
@@ -2134,9 +1734,9 @@ var ZodDiscriminatedUnion = /** @class */ (function (_super) {
|
|
|
2134
1734
|
});
|
|
2135
1735
|
return INVALID;
|
|
2136
1736
|
}
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
1737
|
+
const discriminator = this.discriminator;
|
|
1738
|
+
const discriminatorValue = ctx.data[discriminator];
|
|
1739
|
+
const option = this.options.get(discriminatorValue);
|
|
2140
1740
|
if (!option) {
|
|
2141
1741
|
addIssueToContext(ctx, {
|
|
2142
1742
|
code: ZodIssueCode.invalid_union_discriminator,
|
|
@@ -2159,28 +1759,16 @@ var ZodDiscriminatedUnion = /** @class */ (function (_super) {
|
|
|
2159
1759
|
parent: ctx,
|
|
2160
1760
|
});
|
|
2161
1761
|
}
|
|
2162
|
-
}
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
return Array.from(this.options.keys());
|
|
2173
|
-
},
|
|
2174
|
-
enumerable: false,
|
|
2175
|
-
configurable: true
|
|
2176
|
-
});
|
|
2177
|
-
Object.defineProperty(ZodDiscriminatedUnion.prototype, "options", {
|
|
2178
|
-
get: function () {
|
|
2179
|
-
return this._def.options;
|
|
2180
|
-
},
|
|
2181
|
-
enumerable: false,
|
|
2182
|
-
configurable: true
|
|
2183
|
-
});
|
|
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
|
+
}
|
|
2184
1772
|
/**
|
|
2185
1773
|
* The constructor of the discriminated union schema. Its behaviour is very similar to that of the normal z.union() constructor.
|
|
2186
1774
|
* However, it only allows a union of objects, all of which need to share a discriminator property. This property must
|
|
@@ -2189,12 +1777,12 @@ var ZodDiscriminatedUnion = /** @class */ (function (_super) {
|
|
|
2189
1777
|
* @param types an array of object schemas
|
|
2190
1778
|
* @param params
|
|
2191
1779
|
*/
|
|
2192
|
-
|
|
1780
|
+
static create(discriminator, types, params) {
|
|
2193
1781
|
// Get all the valid discriminator values
|
|
2194
|
-
|
|
1782
|
+
const options = new Map();
|
|
2195
1783
|
try {
|
|
2196
|
-
types.forEach(
|
|
2197
|
-
|
|
1784
|
+
types.forEach((type) => {
|
|
1785
|
+
const discriminatorValue = type.shape[discriminator].value;
|
|
2198
1786
|
options.set(discriminatorValue, type);
|
|
2199
1787
|
});
|
|
2200
1788
|
}
|
|
@@ -2205,39 +1793,32 @@ var ZodDiscriminatedUnion = /** @class */ (function (_super) {
|
|
|
2205
1793
|
if (options.size !== types.length) {
|
|
2206
1794
|
throw new Error("Some of the discriminator values are not unique");
|
|
2207
1795
|
}
|
|
2208
|
-
return new ZodDiscriminatedUnion(
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
1796
|
+
return new ZodDiscriminatedUnion({
|
|
1797
|
+
typeName: ZodFirstPartyTypeKind.ZodDiscriminatedUnion,
|
|
1798
|
+
discriminator,
|
|
1799
|
+
options,
|
|
1800
|
+
...processCreateParams(params),
|
|
1801
|
+
});
|
|
1802
|
+
}
|
|
1803
|
+
}
|
|
2212
1804
|
function mergeValues(a, b) {
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
var bType = getParsedType(b);
|
|
1805
|
+
const aType = getParsedType(a);
|
|
1806
|
+
const bType = getParsedType(b);
|
|
2216
1807
|
if (a === b) {
|
|
2217
1808
|
return { valid: true, data: a };
|
|
2218
1809
|
}
|
|
2219
1810
|
else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
|
|
2220
|
-
|
|
2221
|
-
|
|
1811
|
+
const bKeys = util.objectKeys(b);
|
|
1812
|
+
const sharedKeys = util
|
|
2222
1813
|
.objectKeys(a)
|
|
2223
|
-
.filter(
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
if (!sharedValue.valid) {
|
|
2230
|
-
return { valid: false };
|
|
2231
|
-
}
|
|
2232
|
-
newObj[key] = sharedValue.data;
|
|
2233
|
-
}
|
|
2234
|
-
}
|
|
2235
|
-
catch (e_11_1) { e_11 = { error: e_11_1 }; }
|
|
2236
|
-
finally {
|
|
2237
|
-
try {
|
|
2238
|
-
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 };
|
|
2239
1820
|
}
|
|
2240
|
-
|
|
1821
|
+
newObj[key] = sharedValue.data;
|
|
2241
1822
|
}
|
|
2242
1823
|
return { valid: true, data: newObj };
|
|
2243
1824
|
}
|
|
@@ -2245,11 +1826,11 @@ function mergeValues(a, b) {
|
|
|
2245
1826
|
if (a.length !== b.length) {
|
|
2246
1827
|
return { valid: false };
|
|
2247
1828
|
}
|
|
2248
|
-
|
|
2249
|
-
for (
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
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);
|
|
2253
1834
|
if (!sharedValue.valid) {
|
|
2254
1835
|
return { valid: false };
|
|
2255
1836
|
}
|
|
@@ -2266,18 +1847,14 @@ function mergeValues(a, b) {
|
|
|
2266
1847
|
return { valid: false };
|
|
2267
1848
|
}
|
|
2268
1849
|
}
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
}
|
|
2274
|
-
ZodIntersection.prototype._parse = function (input) {
|
|
2275
|
-
var _a = this._processInputParams(input), status = _a.status, ctx = _a.ctx;
|
|
2276
|
-
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) => {
|
|
2277
1854
|
if (isAborted(parsedLeft) || isAborted(parsedRight)) {
|
|
2278
1855
|
return INVALID;
|
|
2279
1856
|
}
|
|
2280
|
-
|
|
1857
|
+
const merged = mergeValues(parsedLeft.value, parsedRight.value);
|
|
2281
1858
|
if (!merged.valid) {
|
|
2282
1859
|
addIssueToContext(ctx, {
|
|
2283
1860
|
code: ZodIssueCode.invalid_intersection_types,
|
|
@@ -2301,10 +1878,7 @@ var ZodIntersection = /** @class */ (function (_super) {
|
|
|
2301
1878
|
path: ctx.path,
|
|
2302
1879
|
parent: ctx,
|
|
2303
1880
|
}),
|
|
2304
|
-
]).then(
|
|
2305
|
-
var _b = __read(_a, 2), left = _b[0], right = _b[1];
|
|
2306
|
-
return handleParsed(left, right);
|
|
2307
|
-
});
|
|
1881
|
+
]).then(([left, right]) => handleParsed(left, right));
|
|
2308
1882
|
}
|
|
2309
1883
|
else {
|
|
2310
1884
|
return handleParsed(this._def.left._parseSync({
|
|
@@ -2317,20 +1891,19 @@ var ZodIntersection = /** @class */ (function (_super) {
|
|
|
2317
1891
|
parent: ctx,
|
|
2318
1892
|
}));
|
|
2319
1893
|
}
|
|
2320
|
-
}
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
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);
|
|
2334
1907
|
if (ctx.parsedType !== ZodParsedType.array) {
|
|
2335
1908
|
addIssueToContext(ctx, {
|
|
2336
1909
|
code: ZodIssueCode.invalid_type,
|
|
@@ -2348,7 +1921,7 @@ var ZodTuple = /** @class */ (function (_super) {
|
|
|
2348
1921
|
});
|
|
2349
1922
|
return INVALID;
|
|
2350
1923
|
}
|
|
2351
|
-
|
|
1924
|
+
const rest = this._def.rest;
|
|
2352
1925
|
if (!rest && ctx.data.length > this._def.items.length) {
|
|
2353
1926
|
addIssueToContext(ctx, {
|
|
2354
1927
|
code: ZodIssueCode.too_big,
|
|
@@ -2358,63 +1931,54 @@ var ZodTuple = /** @class */ (function (_super) {
|
|
|
2358
1931
|
});
|
|
2359
1932
|
status.dirty();
|
|
2360
1933
|
}
|
|
2361
|
-
|
|
2362
|
-
.map(
|
|
2363
|
-
|
|
1934
|
+
const items = ctx.data
|
|
1935
|
+
.map((item, itemIndex) => {
|
|
1936
|
+
const schema = this._def.items[itemIndex] || this._def.rest;
|
|
2364
1937
|
if (!schema)
|
|
2365
1938
|
return null;
|
|
2366
1939
|
return schema._parse({
|
|
2367
1940
|
data: item,
|
|
2368
|
-
path:
|
|
1941
|
+
path: [...ctx.path, itemIndex],
|
|
2369
1942
|
parent: ctx,
|
|
2370
1943
|
});
|
|
2371
1944
|
})
|
|
2372
|
-
.filter(
|
|
1945
|
+
.filter((x) => !!x); // filter nulls
|
|
2373
1946
|
if (ctx.async) {
|
|
2374
|
-
return Promise.all(items).then(
|
|
1947
|
+
return Promise.all(items).then((results) => {
|
|
2375
1948
|
return ParseStatus.mergeArray(status, results);
|
|
2376
1949
|
});
|
|
2377
1950
|
}
|
|
2378
1951
|
else {
|
|
2379
1952
|
return ParseStatus.mergeArray(status, items);
|
|
2380
1953
|
}
|
|
2381
|
-
}
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
__extends(ZodRecord, _super);
|
|
2399
|
-
function ZodRecord() {
|
|
2400
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
2401
|
-
}
|
|
2402
|
-
Object.defineProperty(ZodRecord.prototype, "keySchema", {
|
|
2403
|
-
get: function () {
|
|
2404
|
-
return this._def.keyType;
|
|
2405
|
-
},
|
|
2406
|
-
enumerable: false,
|
|
2407
|
-
configurable: true
|
|
2408
|
-
});
|
|
2409
|
-
Object.defineProperty(ZodRecord.prototype, "valueSchema", {
|
|
2410
|
-
get: function () {
|
|
2411
|
-
return this._def.valueType;
|
|
2412
|
-
},
|
|
2413
|
-
enumerable: false,
|
|
2414
|
-
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),
|
|
2415
1971
|
});
|
|
2416
|
-
|
|
2417
|
-
|
|
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);
|
|
2418
1982
|
if (ctx.parsedType !== ZodParsedType.object) {
|
|
2419
1983
|
addIssueToContext(ctx, {
|
|
2420
1984
|
code: ZodIssueCode.invalid_type,
|
|
@@ -2423,19 +1987,19 @@ var ZodRecord = /** @class */ (function (_super) {
|
|
|
2423
1987
|
});
|
|
2424
1988
|
return INVALID;
|
|
2425
1989
|
}
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
for (
|
|
1990
|
+
const pairs = [];
|
|
1991
|
+
const keyType = this._def.keyType;
|
|
1992
|
+
const valueType = this._def.valueType;
|
|
1993
|
+
for (const key in ctx.data) {
|
|
2430
1994
|
pairs.push({
|
|
2431
1995
|
key: keyType._parse({
|
|
2432
1996
|
data: key,
|
|
2433
|
-
path:
|
|
1997
|
+
path: [...ctx.path, key],
|
|
2434
1998
|
parent: ctx,
|
|
2435
1999
|
}),
|
|
2436
2000
|
value: valueType._parse({
|
|
2437
2001
|
data: ctx.data[key],
|
|
2438
|
-
path:
|
|
2002
|
+
path: [...ctx.path, key],
|
|
2439
2003
|
parent: ctx,
|
|
2440
2004
|
}),
|
|
2441
2005
|
});
|
|
@@ -2446,31 +2010,30 @@ var ZodRecord = /** @class */ (function (_super) {
|
|
|
2446
2010
|
else {
|
|
2447
2011
|
return ParseStatus.mergeObjectSync(status, pairs);
|
|
2448
2012
|
}
|
|
2449
|
-
}
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
enumerable: false,
|
|
2455
|
-
configurable: true
|
|
2456
|
-
});
|
|
2457
|
-
ZodRecord.create = function (first, second, third) {
|
|
2013
|
+
}
|
|
2014
|
+
get element() {
|
|
2015
|
+
return this._def.valueType;
|
|
2016
|
+
}
|
|
2017
|
+
static create(first, second, third) {
|
|
2458
2018
|
if (second instanceof ZodType) {
|
|
2459
|
-
return new ZodRecord(
|
|
2019
|
+
return new ZodRecord({
|
|
2020
|
+
keyType: first,
|
|
2021
|
+
valueType: second,
|
|
2022
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
2023
|
+
...processCreateParams(third),
|
|
2024
|
+
});
|
|
2460
2025
|
}
|
|
2461
|
-
return new ZodRecord(
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
var _this = this;
|
|
2473
|
-
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);
|
|
2474
2037
|
if (ctx.parsedType !== ZodParsedType.map) {
|
|
2475
2038
|
addIssueToContext(ctx, {
|
|
2476
2039
|
code: ZodIssueCode.invalid_type,
|
|
@@ -2479,77 +2042,28 @@ var ZodMap = /** @class */ (function (_super) {
|
|
|
2479
2042
|
});
|
|
2480
2043
|
return INVALID;
|
|
2481
2044
|
}
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
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) => {
|
|
2486
2048
|
return {
|
|
2487
2049
|
key: keyType._parse({
|
|
2488
2050
|
data: key,
|
|
2489
|
-
path:
|
|
2051
|
+
path: [...ctx.path, index, "key"],
|
|
2490
2052
|
parent: ctx,
|
|
2491
2053
|
}),
|
|
2492
2054
|
value: valueType._parse({
|
|
2493
2055
|
data: value,
|
|
2494
|
-
path:
|
|
2056
|
+
path: [...ctx.path, index, "value"],
|
|
2495
2057
|
parent: ctx,
|
|
2496
2058
|
}),
|
|
2497
2059
|
};
|
|
2498
2060
|
});
|
|
2499
2061
|
if (ctx.async) {
|
|
2500
|
-
|
|
2501
|
-
return Promise.resolve().then(
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
switch (_b.label) {
|
|
2506
|
-
case 0:
|
|
2507
|
-
_b.trys.push([0, 6, 7, 8]);
|
|
2508
|
-
pairs_3 = __values(pairs), pairs_3_1 = pairs_3.next();
|
|
2509
|
-
_b.label = 1;
|
|
2510
|
-
case 1:
|
|
2511
|
-
if (!!pairs_3_1.done) return [3 /*break*/, 5];
|
|
2512
|
-
pair = pairs_3_1.value;
|
|
2513
|
-
return [4 /*yield*/, pair.key];
|
|
2514
|
-
case 2:
|
|
2515
|
-
key = _b.sent();
|
|
2516
|
-
return [4 /*yield*/, pair.value];
|
|
2517
|
-
case 3:
|
|
2518
|
-
value = _b.sent();
|
|
2519
|
-
if (key.status === "aborted" || value.status === "aborted") {
|
|
2520
|
-
return [2 /*return*/, INVALID];
|
|
2521
|
-
}
|
|
2522
|
-
if (key.status === "dirty" || value.status === "dirty") {
|
|
2523
|
-
status.dirty();
|
|
2524
|
-
}
|
|
2525
|
-
finalMap_1.set(key.value, value.value);
|
|
2526
|
-
_b.label = 4;
|
|
2527
|
-
case 4:
|
|
2528
|
-
pairs_3_1 = pairs_3.next();
|
|
2529
|
-
return [3 /*break*/, 1];
|
|
2530
|
-
case 5: return [3 /*break*/, 8];
|
|
2531
|
-
case 6:
|
|
2532
|
-
e_13_1 = _b.sent();
|
|
2533
|
-
e_13 = { error: e_13_1 };
|
|
2534
|
-
return [3 /*break*/, 8];
|
|
2535
|
-
case 7:
|
|
2536
|
-
try {
|
|
2537
|
-
if (pairs_3_1 && !pairs_3_1.done && (_a = pairs_3.return)) _a.call(pairs_3);
|
|
2538
|
-
}
|
|
2539
|
-
finally { if (e_13) throw e_13.error; }
|
|
2540
|
-
return [7 /*endfinally*/];
|
|
2541
|
-
case 8: return [2 /*return*/, { status: status.value, value: finalMap_1 }];
|
|
2542
|
-
}
|
|
2543
|
-
});
|
|
2544
|
-
}); });
|
|
2545
|
-
}
|
|
2546
|
-
else {
|
|
2547
|
-
var finalMap = new Map();
|
|
2548
|
-
try {
|
|
2549
|
-
for (var pairs_2 = __values(pairs), pairs_2_1 = pairs_2.next(); !pairs_2_1.done; pairs_2_1 = pairs_2.next()) {
|
|
2550
|
-
var pair = pairs_2_1.value;
|
|
2551
|
-
var key = pair.key;
|
|
2552
|
-
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;
|
|
2553
2067
|
if (key.status === "aborted" || value.status === "aborted") {
|
|
2554
2068
|
return INVALID;
|
|
2555
2069
|
}
|
|
@@ -2558,29 +2072,37 @@ var ZodMap = /** @class */ (function (_super) {
|
|
|
2558
2072
|
}
|
|
2559
2073
|
finalMap.set(key.value, value.value);
|
|
2560
2074
|
}
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
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();
|
|
2566
2088
|
}
|
|
2567
|
-
|
|
2089
|
+
finalMap.set(key.value, value.value);
|
|
2568
2090
|
}
|
|
2569
2091
|
return { status: status.value, value: finalMap };
|
|
2570
2092
|
}
|
|
2571
|
-
}
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
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);
|
|
2584
2106
|
if (ctx.parsedType !== ZodParsedType.set) {
|
|
2585
2107
|
addIssueToContext(ctx, {
|
|
2586
2108
|
code: ZodIssueCode.invalid_type,
|
|
@@ -2589,7 +2111,7 @@ var ZodSet = /** @class */ (function (_super) {
|
|
|
2589
2111
|
});
|
|
2590
2112
|
return INVALID;
|
|
2591
2113
|
}
|
|
2592
|
-
|
|
2114
|
+
const def = this._def;
|
|
2593
2115
|
if (def.minSize !== null) {
|
|
2594
2116
|
if (ctx.data.size < def.minSize.value) {
|
|
2595
2117
|
addIssueToContext(ctx, {
|
|
@@ -2614,66 +2136,61 @@ var ZodSet = /** @class */ (function (_super) {
|
|
|
2614
2136
|
status.dirty();
|
|
2615
2137
|
}
|
|
2616
2138
|
}
|
|
2617
|
-
|
|
2139
|
+
const valueType = this._def.valueType;
|
|
2618
2140
|
function finalizeSet(elements) {
|
|
2619
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
if (element.status === "dirty")
|
|
2627
|
-
status.dirty();
|
|
2628
|
-
parsedSet.add(element.value);
|
|
2629
|
-
}
|
|
2630
|
-
}
|
|
2631
|
-
catch (e_14_1) { e_14 = { error: e_14_1 }; }
|
|
2632
|
-
finally {
|
|
2633
|
-
try {
|
|
2634
|
-
if (elements_1_1 && !elements_1_1.done && (_a = elements_1.return)) _a.call(elements_1);
|
|
2635
|
-
}
|
|
2636
|
-
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);
|
|
2637
2148
|
}
|
|
2638
2149
|
return { status: status.value, value: parsedSet };
|
|
2639
2150
|
}
|
|
2640
|
-
|
|
2641
|
-
return valueType._parse({ data: item, path: __spreadArray(__spreadArray([], __read(ctx.path), false), [i], false), parent: ctx });
|
|
2642
|
-
});
|
|
2151
|
+
const elements = [...ctx.data.values()].map((item, i) => valueType._parse({ data: item, path: [...ctx.path, i], parent: ctx }));
|
|
2643
2152
|
if (ctx.async) {
|
|
2644
|
-
return Promise.all(elements).then(
|
|
2153
|
+
return Promise.all(elements).then((elements) => finalizeSet(elements));
|
|
2645
2154
|
}
|
|
2646
2155
|
else {
|
|
2647
2156
|
return finalizeSet(elements);
|
|
2648
2157
|
}
|
|
2649
|
-
}
|
|
2650
|
-
|
|
2651
|
-
return new ZodSet(
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
}
|
|
2656
|
-
|
|
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) {
|
|
2657
2172
|
return this.min(size, message).max(size, message);
|
|
2658
|
-
}
|
|
2659
|
-
|
|
2173
|
+
}
|
|
2174
|
+
nonempty(message) {
|
|
2660
2175
|
return this.min(1, message);
|
|
2661
|
-
}
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
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);
|
|
2677
2194
|
if (ctx.parsedType !== ZodParsedType.function) {
|
|
2678
2195
|
addIssueToContext(ctx, {
|
|
2679
2196
|
code: ZodIssueCode.invalid_type,
|
|
@@ -2691,7 +2208,7 @@ var ZodFunction = /** @class */ (function (_super) {
|
|
|
2691
2208
|
ctx.schemaErrorMap,
|
|
2692
2209
|
overrideErrorMap,
|
|
2693
2210
|
defaultErrorMap,
|
|
2694
|
-
].filter(
|
|
2211
|
+
].filter((x) => !!x),
|
|
2695
2212
|
issueData: {
|
|
2696
2213
|
code: ZodIssueCode.invalid_arguments,
|
|
2697
2214
|
argumentsError: error,
|
|
@@ -2707,131 +2224,106 @@ var ZodFunction = /** @class */ (function (_super) {
|
|
|
2707
2224
|
ctx.schemaErrorMap,
|
|
2708
2225
|
overrideErrorMap,
|
|
2709
2226
|
defaultErrorMap,
|
|
2710
|
-
].filter(
|
|
2227
|
+
].filter((x) => !!x),
|
|
2711
2228
|
issueData: {
|
|
2712
2229
|
code: ZodIssueCode.invalid_return_type,
|
|
2713
2230
|
returnTypeError: error,
|
|
2714
2231
|
},
|
|
2715
2232
|
});
|
|
2716
2233
|
}
|
|
2717
|
-
|
|
2718
|
-
|
|
2234
|
+
const params = { errorMap: ctx.contextualErrorMap };
|
|
2235
|
+
const fn = ctx.data;
|
|
2719
2236
|
if (this._def.returns instanceof ZodPromise) {
|
|
2720
|
-
return OK(
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
args
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
return __generator(this, function (_a) {
|
|
2728
|
-
switch (_a.label) {
|
|
2729
|
-
case 0:
|
|
2730
|
-
error = new ZodError([]);
|
|
2731
|
-
return [4 /*yield*/, this._def.args
|
|
2732
|
-
.parseAsync(args, params)
|
|
2733
|
-
.catch(function (e) {
|
|
2734
|
-
error.addIssue(makeArgsIssue(args, e));
|
|
2735
|
-
throw error;
|
|
2736
|
-
})];
|
|
2737
|
-
case 1:
|
|
2738
|
-
parsedArgs = _a.sent();
|
|
2739
|
-
return [4 /*yield*/, fn.apply(void 0, __spreadArray([], __read(parsedArgs), false))];
|
|
2740
|
-
case 2:
|
|
2741
|
-
result = _a.sent();
|
|
2742
|
-
return [4 /*yield*/, this._def.returns._def.type
|
|
2743
|
-
.parseAsync(result, params)
|
|
2744
|
-
.catch(function (e) {
|
|
2745
|
-
error.addIssue(makeReturnsIssue(result, e));
|
|
2746
|
-
throw error;
|
|
2747
|
-
})];
|
|
2748
|
-
case 3:
|
|
2749
|
-
parsedReturns = _a.sent();
|
|
2750
|
-
return [2 /*return*/, parsedReturns];
|
|
2751
|
-
}
|
|
2752
|
-
});
|
|
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;
|
|
2753
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;
|
|
2251
|
+
});
|
|
2252
|
+
return parsedReturns;
|
|
2754
2253
|
});
|
|
2755
2254
|
}
|
|
2756
2255
|
else {
|
|
2757
|
-
return OK(
|
|
2758
|
-
|
|
2759
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2760
|
-
args[_i] = arguments[_i];
|
|
2761
|
-
}
|
|
2762
|
-
var parsedArgs = _this._def.args.safeParse(args, params);
|
|
2256
|
+
return OK((...args) => {
|
|
2257
|
+
const parsedArgs = this._def.args.safeParse(args, params);
|
|
2763
2258
|
if (!parsedArgs.success) {
|
|
2764
2259
|
throw new ZodError([makeArgsIssue(args, parsedArgs.error)]);
|
|
2765
2260
|
}
|
|
2766
|
-
|
|
2767
|
-
|
|
2261
|
+
const result = fn(...parsedArgs.data);
|
|
2262
|
+
const parsedReturns = this._def.returns.safeParse(result, params);
|
|
2768
2263
|
if (!parsedReturns.success) {
|
|
2769
2264
|
throw new ZodError([makeReturnsIssue(result, parsedReturns.error)]);
|
|
2770
2265
|
}
|
|
2771
2266
|
return parsedReturns.data;
|
|
2772
2267
|
});
|
|
2773
2268
|
}
|
|
2774
|
-
}
|
|
2775
|
-
|
|
2269
|
+
}
|
|
2270
|
+
parameters() {
|
|
2776
2271
|
return this._def.args;
|
|
2777
|
-
}
|
|
2778
|
-
|
|
2272
|
+
}
|
|
2273
|
+
returnType() {
|
|
2779
2274
|
return this._def.returns;
|
|
2780
|
-
}
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
|
|
2784
|
-
items
|
|
2785
|
-
}
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
|
|
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);
|
|
2793
2290
|
return validatedFunc;
|
|
2794
|
-
}
|
|
2795
|
-
|
|
2796
|
-
|
|
2291
|
+
}
|
|
2292
|
+
strictImplement(func) {
|
|
2293
|
+
const validatedFunc = this.parse(func);
|
|
2797
2294
|
return validatedFunc;
|
|
2798
|
-
}
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
function ZodLazy() {
|
|
2809
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
2810
|
-
}
|
|
2811
|
-
Object.defineProperty(ZodLazy.prototype, "schema", {
|
|
2812
|
-
get: function () {
|
|
2813
|
-
return this._def.getter();
|
|
2814
|
-
},
|
|
2815
|
-
enumerable: false,
|
|
2816
|
-
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),
|
|
2817
2305
|
});
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
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();
|
|
2821
2314
|
return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx });
|
|
2822
|
-
}
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
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);
|
|
2835
2327
|
if (ctx.data !== this._def.value) {
|
|
2836
2328
|
addIssueToContext(ctx, {
|
|
2837
2329
|
code: ZodIssueCode.invalid_type,
|
|
@@ -2841,32 +2333,27 @@ var ZodLiteral = /** @class */ (function (_super) {
|
|
|
2841
2333
|
return INVALID;
|
|
2842
2334
|
}
|
|
2843
2335
|
return { status: status.value, value: ctx.data };
|
|
2844
|
-
}
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
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),
|
|
2851
2346
|
});
|
|
2852
|
-
|
|
2853
|
-
return new ZodLiteral(__assign({ value: value, typeName: ZodFirstPartyTypeKind.ZodLiteral }, processCreateParams(params)));
|
|
2854
|
-
};
|
|
2855
|
-
return ZodLiteral;
|
|
2856
|
-
}(ZodType));
|
|
2347
|
+
};
|
|
2857
2348
|
function createZodEnum(values) {
|
|
2858
2349
|
return new ZodEnum({
|
|
2859
2350
|
values: values,
|
|
2860
2351
|
typeName: ZodFirstPartyTypeKind.ZodEnum,
|
|
2861
2352
|
});
|
|
2862
2353
|
}
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
2867
|
-
}
|
|
2868
|
-
ZodEnum.prototype._parse = function (input) {
|
|
2869
|
-
var ctx = this._processInputParams(input).ctx;
|
|
2354
|
+
class ZodEnum extends ZodType {
|
|
2355
|
+
_parse(input) {
|
|
2356
|
+
const { ctx } = this._processInputParams(input);
|
|
2870
2357
|
if (this._def.values.indexOf(ctx.data) === -1) {
|
|
2871
2358
|
addIssueToContext(ctx, {
|
|
2872
2359
|
code: ZodIssueCode.invalid_enum_value,
|
|
@@ -2875,91 +2362,37 @@ var ZodEnum = /** @class */ (function (_super) {
|
|
|
2875
2362
|
return INVALID;
|
|
2876
2363
|
}
|
|
2877
2364
|
return OK(ctx.data);
|
|
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
|
-
|
|
2909
|
-
get: function () {
|
|
2910
|
-
var e_16, _a;
|
|
2911
|
-
var enumValues = {};
|
|
2912
|
-
try {
|
|
2913
|
-
for (var _b = __values(this._def.values), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
2914
|
-
var val = _c.value;
|
|
2915
|
-
enumValues[val] = val;
|
|
2916
|
-
}
|
|
2917
|
-
}
|
|
2918
|
-
catch (e_16_1) { e_16 = { error: e_16_1 }; }
|
|
2919
|
-
finally {
|
|
2920
|
-
try {
|
|
2921
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
2922
|
-
}
|
|
2923
|
-
finally { if (e_16) throw e_16.error; }
|
|
2924
|
-
}
|
|
2925
|
-
return enumValues;
|
|
2926
|
-
},
|
|
2927
|
-
enumerable: false,
|
|
2928
|
-
configurable: true
|
|
2929
|
-
});
|
|
2930
|
-
Object.defineProperty(ZodEnum.prototype, "Enum", {
|
|
2931
|
-
get: function () {
|
|
2932
|
-
var e_17, _a;
|
|
2933
|
-
var enumValues = {};
|
|
2934
|
-
try {
|
|
2935
|
-
for (var _b = __values(this._def.values), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
2936
|
-
var val = _c.value;
|
|
2937
|
-
enumValues[val] = val;
|
|
2938
|
-
}
|
|
2939
|
-
}
|
|
2940
|
-
catch (e_17_1) { e_17 = { error: e_17_1 }; }
|
|
2941
|
-
finally {
|
|
2942
|
-
try {
|
|
2943
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
2944
|
-
}
|
|
2945
|
-
finally { if (e_17) throw e_17.error; }
|
|
2946
|
-
}
|
|
2947
|
-
return enumValues;
|
|
2948
|
-
},
|
|
2949
|
-
enumerable: false,
|
|
2950
|
-
configurable: true
|
|
2951
|
-
});
|
|
2952
|
-
ZodEnum.create = createZodEnum;
|
|
2953
|
-
return ZodEnum;
|
|
2954
|
-
}(ZodType));
|
|
2955
|
-
var ZodNativeEnum = /** @class */ (function (_super) {
|
|
2956
|
-
__extends(ZodNativeEnum, _super);
|
|
2957
|
-
function ZodNativeEnum() {
|
|
2958
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
2959
|
-
}
|
|
2960
|
-
ZodNativeEnum.prototype._parse = function (input) {
|
|
2961
|
-
var ctx = this._processInputParams(input).ctx;
|
|
2962
|
-
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);
|
|
2963
2396
|
if (nativeEnumValues.indexOf(ctx.data) === -1) {
|
|
2964
2397
|
addIssueToContext(ctx, {
|
|
2965
2398
|
code: ZodIssueCode.invalid_enum_value,
|
|
@@ -2968,27 +2401,21 @@ var ZodNativeEnum = /** @class */ (function (_super) {
|
|
|
2968
2401
|
return INVALID;
|
|
2969
2402
|
}
|
|
2970
2403
|
return OK(ctx.data);
|
|
2971
|
-
}
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
|
|
2977
|
-
|
|
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),
|
|
2978
2414
|
});
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
}(ZodType));
|
|
2984
|
-
var ZodPromise = /** @class */ (function (_super) {
|
|
2985
|
-
__extends(ZodPromise, _super);
|
|
2986
|
-
function ZodPromise() {
|
|
2987
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
2988
|
-
}
|
|
2989
|
-
ZodPromise.prototype._parse = function (input) {
|
|
2990
|
-
var _this = this;
|
|
2991
|
-
var ctx = this._processInputParams(input).ctx;
|
|
2415
|
+
};
|
|
2416
|
+
class ZodPromise extends ZodType {
|
|
2417
|
+
_parse(input) {
|
|
2418
|
+
const { ctx } = this._processInputParams(input);
|
|
2992
2419
|
if (ctx.parsedType !== ZodParsedType.promise && ctx.async === false) {
|
|
2993
2420
|
addIssueToContext(ctx, {
|
|
2994
2421
|
code: ZodIssueCode.invalid_type,
|
|
@@ -2997,38 +2424,36 @@ var ZodPromise = /** @class */ (function (_super) {
|
|
|
2997
2424
|
});
|
|
2998
2425
|
return INVALID;
|
|
2999
2426
|
}
|
|
3000
|
-
|
|
2427
|
+
const promisified = ctx.parsedType === ZodParsedType.promise
|
|
3001
2428
|
? ctx.data
|
|
3002
2429
|
: Promise.resolve(ctx.data);
|
|
3003
|
-
return OK(promisified.then(
|
|
3004
|
-
return
|
|
2430
|
+
return OK(promisified.then((data) => {
|
|
2431
|
+
return this._def.type.parseAsync(data, {
|
|
3005
2432
|
path: ctx.path,
|
|
3006
2433
|
errorMap: ctx.contextualErrorMap,
|
|
3007
2434
|
});
|
|
3008
2435
|
}));
|
|
3009
|
-
}
|
|
3010
|
-
|
|
3011
|
-
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
|
|
3018
|
-
|
|
3019
|
-
|
|
3020
|
-
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() {
|
|
3021
2447
|
return this._def.schema;
|
|
3022
|
-
}
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
var effect = this._def.effect || null;
|
|
2448
|
+
}
|
|
2449
|
+
_parse(input) {
|
|
2450
|
+
const { status, ctx } = this._processInputParams(input);
|
|
2451
|
+
const effect = this._def.effect || null;
|
|
3027
2452
|
if (effect.type === "preprocess") {
|
|
3028
|
-
|
|
2453
|
+
const processed = effect.transform(ctx.data);
|
|
3029
2454
|
if (ctx.async) {
|
|
3030
|
-
return Promise.resolve(processed).then(
|
|
3031
|
-
return
|
|
2455
|
+
return Promise.resolve(processed).then((processed) => {
|
|
2456
|
+
return this._def.schema._parseAsync({
|
|
3032
2457
|
data: processed,
|
|
3033
2458
|
path: ctx.path,
|
|
3034
2459
|
parent: ctx,
|
|
@@ -3044,8 +2469,8 @@ var ZodEffects = /** @class */ (function (_super) {
|
|
|
3044
2469
|
}
|
|
3045
2470
|
}
|
|
3046
2471
|
if (effect.type === "refinement") {
|
|
3047
|
-
|
|
3048
|
-
addIssue:
|
|
2472
|
+
const checkCtx = {
|
|
2473
|
+
addIssue: (arg) => {
|
|
3049
2474
|
addIssueToContext(ctx, arg);
|
|
3050
2475
|
if (arg.fatal) {
|
|
3051
2476
|
status.abort();
|
|
@@ -3058,11 +2483,11 @@ var ZodEffects = /** @class */ (function (_super) {
|
|
|
3058
2483
|
return ctx.path;
|
|
3059
2484
|
},
|
|
3060
2485
|
};
|
|
3061
|
-
|
|
3062
|
-
|
|
2486
|
+
checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
|
|
2487
|
+
const executeRefinement = (acc
|
|
3063
2488
|
// effect: RefinementEffect<any>
|
|
3064
|
-
) {
|
|
3065
|
-
|
|
2489
|
+
) => {
|
|
2490
|
+
const result = effect.refinement(acc, checkCtx);
|
|
3066
2491
|
if (ctx.async) {
|
|
3067
2492
|
return Promise.resolve(result);
|
|
3068
2493
|
}
|
|
@@ -3072,7 +2497,7 @@ var ZodEffects = /** @class */ (function (_super) {
|
|
|
3072
2497
|
return acc;
|
|
3073
2498
|
};
|
|
3074
2499
|
if (ctx.async === false) {
|
|
3075
|
-
|
|
2500
|
+
const inner = this._def.schema._parseSync({
|
|
3076
2501
|
data: ctx.data,
|
|
3077
2502
|
path: ctx.path,
|
|
3078
2503
|
parent: ctx,
|
|
@@ -3082,18 +2507,18 @@ var ZodEffects = /** @class */ (function (_super) {
|
|
|
3082
2507
|
if (inner.status === "dirty")
|
|
3083
2508
|
status.dirty();
|
|
3084
2509
|
// return value is ignored
|
|
3085
|
-
|
|
2510
|
+
executeRefinement(inner.value);
|
|
3086
2511
|
return { status: status.value, value: inner.value };
|
|
3087
2512
|
}
|
|
3088
2513
|
else {
|
|
3089
2514
|
return this._def.schema
|
|
3090
2515
|
._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx })
|
|
3091
|
-
.then(
|
|
2516
|
+
.then((inner) => {
|
|
3092
2517
|
if (inner.status === "aborted")
|
|
3093
2518
|
return INVALID;
|
|
3094
2519
|
if (inner.status === "dirty")
|
|
3095
2520
|
status.dirty();
|
|
3096
|
-
return
|
|
2521
|
+
return executeRefinement(inner.value).then(() => {
|
|
3097
2522
|
return { status: status.value, value: inner.value };
|
|
3098
2523
|
});
|
|
3099
2524
|
});
|
|
@@ -3101,7 +2526,7 @@ var ZodEffects = /** @class */ (function (_super) {
|
|
|
3101
2526
|
}
|
|
3102
2527
|
if (effect.type === "transform") {
|
|
3103
2528
|
if (ctx.async === false) {
|
|
3104
|
-
|
|
2529
|
+
const base = this._def.schema._parseSync({
|
|
3105
2530
|
data: ctx.data,
|
|
3106
2531
|
path: ctx.path,
|
|
3107
2532
|
parent: ctx,
|
|
@@ -3112,16 +2537,16 @@ var ZodEffects = /** @class */ (function (_super) {
|
|
|
3112
2537
|
// }
|
|
3113
2538
|
if (!isValid(base))
|
|
3114
2539
|
return base;
|
|
3115
|
-
|
|
2540
|
+
const result = effect.transform(base.value);
|
|
3116
2541
|
if (result instanceof Promise) {
|
|
3117
|
-
throw new Error(
|
|
2542
|
+
throw new Error(`Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.`);
|
|
3118
2543
|
}
|
|
3119
2544
|
return OK(result);
|
|
3120
2545
|
}
|
|
3121
2546
|
else {
|
|
3122
2547
|
return this._def.schema
|
|
3123
2548
|
._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx })
|
|
3124
|
-
.then(
|
|
2549
|
+
.then((base) => {
|
|
3125
2550
|
if (!isValid(base))
|
|
3126
2551
|
return base;
|
|
3127
2552
|
// if (base.status === "aborted") return INVALID;
|
|
@@ -3133,22 +2558,27 @@ var ZodEffects = /** @class */ (function (_super) {
|
|
|
3133
2558
|
}
|
|
3134
2559
|
}
|
|
3135
2560
|
util.assertNever(effect);
|
|
3136
|
-
}
|
|
3137
|
-
|
|
3138
|
-
|
|
3139
|
-
|
|
3140
|
-
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
}
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
|
|
3149
|
-
|
|
3150
|
-
|
|
3151
|
-
|
|
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);
|
|
3152
2582
|
if (ctx.parsedType === ZodParsedType.undefined) {
|
|
3153
2583
|
return OK(undefined);
|
|
3154
2584
|
}
|
|
@@ -3157,22 +2587,21 @@ var ZodOptional = /** @class */ (function (_super) {
|
|
|
3157
2587
|
path: ctx.path,
|
|
3158
2588
|
parent: ctx,
|
|
3159
2589
|
});
|
|
3160
|
-
}
|
|
3161
|
-
|
|
2590
|
+
}
|
|
2591
|
+
unwrap() {
|
|
3162
2592
|
return this._def.innerType;
|
|
3163
|
-
}
|
|
3164
|
-
|
|
3165
|
-
|
|
3166
|
-
|
|
3167
|
-
|
|
3168
|
-
|
|
3169
|
-
|
|
3170
|
-
|
|
3171
|
-
|
|
3172
|
-
|
|
3173
|
-
|
|
3174
|
-
|
|
3175
|
-
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);
|
|
3176
2605
|
if (ctx.parsedType === ZodParsedType.null) {
|
|
3177
2606
|
return OK(null);
|
|
3178
2607
|
}
|
|
@@ -3181,47 +2610,45 @@ var ZodNullable = /** @class */ (function (_super) {
|
|
|
3181
2610
|
path: ctx.path,
|
|
3182
2611
|
parent: ctx,
|
|
3183
2612
|
});
|
|
3184
|
-
}
|
|
3185
|
-
|
|
2613
|
+
}
|
|
2614
|
+
unwrap() {
|
|
3186
2615
|
return this._def.innerType;
|
|
3187
|
-
}
|
|
3188
|
-
|
|
3189
|
-
|
|
3190
|
-
|
|
3191
|
-
|
|
3192
|
-
|
|
3193
|
-
|
|
3194
|
-
|
|
3195
|
-
|
|
3196
|
-
|
|
3197
|
-
|
|
3198
|
-
|
|
3199
|
-
|
|
3200
|
-
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;
|
|
3201
2629
|
if (ctx.parsedType === ZodParsedType.undefined) {
|
|
3202
2630
|
data = this._def.defaultValue();
|
|
3203
2631
|
}
|
|
3204
2632
|
return this._def.innerType._parse({
|
|
3205
|
-
data
|
|
2633
|
+
data,
|
|
3206
2634
|
path: ctx.path,
|
|
3207
2635
|
parent: ctx,
|
|
3208
2636
|
});
|
|
3209
|
-
}
|
|
3210
|
-
|
|
2637
|
+
}
|
|
2638
|
+
removeDefault() {
|
|
3211
2639
|
return this._def.innerType;
|
|
3212
|
-
}
|
|
3213
|
-
|
|
3214
|
-
|
|
3215
|
-
|
|
3216
|
-
|
|
3217
|
-
|
|
3218
|
-
|
|
3219
|
-
|
|
3220
|
-
|
|
3221
|
-
|
|
3222
|
-
|
|
3223
|
-
|
|
3224
|
-
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);
|
|
3225
2652
|
if (ctx.parsedType !== ZodParsedType.nan) {
|
|
3226
2653
|
addIssueToContext(ctx, {
|
|
3227
2654
|
code: ZodIssueCode.invalid_type,
|
|
@@ -3231,18 +2658,20 @@ var ZodNaN = /** @class */ (function (_super) {
|
|
|
3231
2658
|
return INVALID;
|
|
3232
2659
|
}
|
|
3233
2660
|
return { status: status.value, value: ctx.data };
|
|
3234
|
-
}
|
|
3235
|
-
|
|
3236
|
-
|
|
3237
|
-
|
|
3238
|
-
|
|
3239
|
-
|
|
3240
|
-
|
|
2661
|
+
}
|
|
2662
|
+
}
|
|
2663
|
+
ZodNaN.create = (params) => {
|
|
2664
|
+
return new ZodNaN({
|
|
2665
|
+
typeName: ZodFirstPartyTypeKind.ZodNaN,
|
|
2666
|
+
...processCreateParams(params),
|
|
2667
|
+
});
|
|
2668
|
+
};
|
|
2669
|
+
const custom = (check, params) => {
|
|
3241
2670
|
if (check)
|
|
3242
2671
|
return ZodAny.create().refine(check, params);
|
|
3243
2672
|
return ZodAny.create();
|
|
3244
2673
|
};
|
|
3245
|
-
|
|
2674
|
+
const late = {
|
|
3246
2675
|
object: ZodObject.lazycreate,
|
|
3247
2676
|
};
|
|
3248
2677
|
var ZodFirstPartyTypeKind;
|
|
@@ -3279,47 +2708,44 @@ var ZodFirstPartyTypeKind;
|
|
|
3279
2708
|
ZodFirstPartyTypeKind["ZodDefault"] = "ZodDefault";
|
|
3280
2709
|
ZodFirstPartyTypeKind["ZodPromise"] = "ZodPromise";
|
|
3281
2710
|
})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
|
|
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
|
-
|
|
3313
|
-
|
|
3314
|
-
|
|
3315
|
-
|
|
3316
|
-
|
|
3317
|
-
|
|
3318
|
-
|
|
3319
|
-
|
|
3320
|
-
var ostring = function () { return stringType().optional(); };
|
|
3321
|
-
var onumber = function () { return numberType().optional(); };
|
|
3322
|
-
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();
|
|
3323
2749
|
|
|
3324
2750
|
var mod = /*#__PURE__*/Object.freeze({
|
|
3325
2751
|
__proto__: null,
|