ZodError.js
12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setErrorMap = exports.overrideErrorMap = exports.defaultErrorMap = exports.ZodError = exports.quotelessJson = exports.ZodIssueCode = void 0;
var util_1 = require("./helpers/util");
exports.ZodIssueCode = util_1.util.arrayToEnum([
"invalid_type",
"custom",
"invalid_union",
"invalid_union_discriminator",
"invalid_enum_value",
"unrecognized_keys",
"invalid_arguments",
"invalid_return_type",
"invalid_date",
"invalid_string",
"too_small",
"too_big",
"invalid_intersection_types",
"not_multiple_of",
]);
var quotelessJson = function (obj) {
var json = JSON.stringify(obj, null, 2);
return json.replace(/"([^"]+)":/g, "$1:");
};
exports.quotelessJson = quotelessJson;
var ZodError = /** @class */ (function (_super) {
__extends(ZodError, _super);
function ZodError(issues) {
var _newTarget = this.constructor;
var _this = _super.call(this) || this;
_this.issues = [];
_this.format = function () {
var fieldErrors = { _errors: [] };
var processError = function (error) {
var e_1, _a;
try {
for (var _b = __values(error.issues), _c = _b.next(); !_c.done; _c = _b.next()) {
var issue = _c.value;
if (issue.code === "invalid_union") {
issue.unionErrors.map(processError);
}
else if (issue.code === "invalid_return_type") {
processError(issue.returnTypeError);
}
else if (issue.code === "invalid_arguments") {
processError(issue.argumentsError);
}
else if (issue.path.length === 0) {
fieldErrors._errors.push(issue.message);
}
else {
var curr = fieldErrors;
var i = 0;
while (i < issue.path.length) {
var el = issue.path[i];
var terminal = i === issue.path.length - 1;
if (!terminal) {
if (typeof el === "string") {
curr[el] = curr[el] || { _errors: [] };
}
else if (typeof el === "number") {
var errorArray = [];
errorArray._errors = [];
curr[el] = curr[el] || errorArray;
}
}
else {
curr[el] = curr[el] || { _errors: [] };
curr[el]._errors.push(issue.message);
}
curr = curr[el];
i++;
}
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
};
processError(_this);
return fieldErrors;
};
_this.addIssue = function (sub) {
_this.issues = __spreadArray(__spreadArray([], __read(_this.issues), false), [sub], false);
};
_this.addIssues = function (subs) {
if (subs === void 0) { subs = []; }
_this.issues = __spreadArray(__spreadArray([], __read(_this.issues), false), __read(subs), false);
};
var actualProto = _newTarget.prototype;
if (Object.setPrototypeOf) {
// eslint-disable-next-line ban/ban
Object.setPrototypeOf(_this, actualProto);
}
else {
_this.__proto__ = actualProto;
}
_this.name = "ZodError";
_this.issues = issues;
return _this;
}
Object.defineProperty(ZodError.prototype, "errors", {
get: function () {
return this.issues;
},
enumerable: false,
configurable: true
});
ZodError.prototype.toString = function () {
return this.message;
};
Object.defineProperty(ZodError.prototype, "message", {
get: function () {
return JSON.stringify(this.issues, null, 2);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ZodError.prototype, "isEmpty", {
get: function () {
return this.issues.length === 0;
},
enumerable: false,
configurable: true
});
ZodError.prototype.flatten = function (mapper) {
var e_2, _a;
if (mapper === void 0) { mapper = function (issue) { return issue.message; }; }
var fieldErrors = {};
var formErrors = [];
try {
for (var _b = __values(this.issues), _c = _b.next(); !_c.done; _c = _b.next()) {
var sub = _c.value;
if (sub.path.length > 0) {
fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
fieldErrors[sub.path[0]].push(mapper(sub));
}
else {
formErrors.push(mapper(sub));
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
return { formErrors: formErrors, fieldErrors: fieldErrors };
};
Object.defineProperty(ZodError.prototype, "formErrors", {
get: function () {
return this.flatten();
},
enumerable: false,
configurable: true
});
ZodError.create = function (issues) {
var error = new ZodError(issues);
return error;
};
return ZodError;
}(Error));
exports.ZodError = ZodError;
var defaultErrorMap = function (issue, _ctx) {
var message;
switch (issue.code) {
case exports.ZodIssueCode.invalid_type:
if (issue.received === "undefined") {
message = "Required";
}
else {
message = "Expected ".concat(issue.expected, ", received ").concat(issue.received);
}
break;
case exports.ZodIssueCode.unrecognized_keys:
message = "Unrecognized key(s) in object: ".concat(issue.keys
.map(function (k) { return "'".concat(k, "'"); })
.join(", "));
break;
case exports.ZodIssueCode.invalid_union:
message = "Invalid input";
break;
case exports.ZodIssueCode.invalid_union_discriminator:
message = "Invalid discriminator value. Expected ".concat(issue.options
.map(function (val) { return (typeof val === "string" ? "'".concat(val, "'") : val); })
.join(" | "));
break;
case exports.ZodIssueCode.invalid_enum_value:
message = "Invalid enum value. Expected ".concat(issue.options
.map(function (val) { return (typeof val === "string" ? "'".concat(val, "'") : val); })
.join(" | "));
break;
case exports.ZodIssueCode.invalid_arguments:
message = "Invalid function arguments";
break;
case exports.ZodIssueCode.invalid_return_type:
message = "Invalid function return type";
break;
case exports.ZodIssueCode.invalid_date:
message = "Invalid date";
break;
case exports.ZodIssueCode.invalid_string:
if (issue.validation !== "regex")
message = "Invalid ".concat(issue.validation);
else
message = "Invalid";
break;
case exports.ZodIssueCode.too_small:
if (issue.type === "array")
message = "Array must contain ".concat(issue.inclusive ? "at least" : "more than", " ").concat(issue.minimum, " element(s)");
else if (issue.type === "string")
message = "String must contain ".concat(issue.inclusive ? "at least" : "over", " ").concat(issue.minimum, " character(s)");
else if (issue.type === "number")
message = "Number must be greater than ".concat(issue.inclusive ? "or equal to " : "").concat(issue.minimum);
else
message = "Invalid input";
break;
case exports.ZodIssueCode.too_big:
if (issue.type === "array")
message = "Array must contain ".concat(issue.inclusive ? "at most" : "less than", " ").concat(issue.maximum, " element(s)");
else if (issue.type === "string")
message = "String must contain ".concat(issue.inclusive ? "at most" : "under", " ").concat(issue.maximum, " character(s)");
else if (issue.type === "number")
message = "Number must be less than ".concat(issue.inclusive ? "or equal to " : "").concat(issue.maximum);
else
message = "Invalid input";
break;
case exports.ZodIssueCode.custom:
message = "Invalid input";
break;
case exports.ZodIssueCode.invalid_intersection_types:
message = "Intersection results could not be merged";
break;
case exports.ZodIssueCode.not_multiple_of:
message = "Number must be a multiple of ".concat(issue.multipleOf);
break;
default:
message = _ctx.defaultError;
util_1.util.assertNever(issue);
}
return { message: message };
};
exports.defaultErrorMap = defaultErrorMap;
exports.overrideErrorMap = exports.defaultErrorMap;
var setErrorMap = function (map) {
exports.overrideErrorMap = map;
};
exports.setErrorMap = setErrorMap;