v2
This commit is contained in:
+167
@@ -0,0 +1,167 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ZodError = exports.quotelessJson = exports.ZodIssueCode = void 0;
|
||||
const util_js_1 = require("./helpers/util.cjs");
|
||||
exports.ZodIssueCode = util_js_1.util.arrayToEnum([
|
||||
"invalid_type",
|
||||
"invalid_literal",
|
||||
"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",
|
||||
"not_finite",
|
||||
]);
|
||||
const quotelessJson = (obj) => {
|
||||
const json = JSON.stringify(obj, null, 2);
|
||||
return json.replace(/"([^"]+)":/g, "$1:");
|
||||
};
|
||||
exports.quotelessJson = quotelessJson;
|
||||
class ZodError extends Error {
|
||||
get errors() {
|
||||
return this.issues;
|
||||
}
|
||||
constructor(issues) {
|
||||
super();
|
||||
this.issues = [];
|
||||
this.addIssue = (sub) => {
|
||||
this.issues = [...this.issues, sub];
|
||||
};
|
||||
this.addIssues = (subs = []) => {
|
||||
this.issues = [...this.issues, ...subs];
|
||||
};
|
||||
const actualProto = new.target.prototype;
|
||||
if (Object.setPrototypeOf) {
|
||||
// eslint-disable-next-line ban/ban
|
||||
Object.setPrototypeOf(this, actualProto);
|
||||
}
|
||||
else {
|
||||
this.__proto__ = actualProto;
|
||||
}
|
||||
this.name = "ZodError";
|
||||
this.issues = issues;
|
||||
}
|
||||
format(_mapper) {
|
||||
const mapper = _mapper ||
|
||||
function (issue) {
|
||||
return issue.message;
|
||||
};
|
||||
const fieldErrors = { _errors: [] };
|
||||
const processError = (error) => {
|
||||
for (const issue of error.issues) {
|
||||
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(mapper(issue));
|
||||
}
|
||||
else {
|
||||
let curr = fieldErrors;
|
||||
let i = 0;
|
||||
while (i < issue.path.length) {
|
||||
const el = issue.path[i];
|
||||
const terminal = i === issue.path.length - 1;
|
||||
// `_errors` is reserved by this legacy format, so merge a matching path segment into the current node instead of treating its array as a child.
|
||||
if (el === "_errors") {
|
||||
if (terminal)
|
||||
curr._errors.push(mapper(issue));
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
// Create `el` as an own data property. A segment naming an inherited member ("toString", "constructor") would otherwise read through to the prototype, and assigning "__proto__" would hit the setter and walk into Object.prototype.
|
||||
if (!Object.prototype.hasOwnProperty.call(curr, el)) {
|
||||
if (el === "__proto__") {
|
||||
Object.defineProperty(curr, el, {
|
||||
value: { _errors: [] },
|
||||
writable: true,
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
});
|
||||
}
|
||||
else {
|
||||
curr[el] = { _errors: [] };
|
||||
}
|
||||
}
|
||||
curr = curr[el];
|
||||
if (terminal) {
|
||||
curr._errors.push(mapper(issue));
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
processError(this);
|
||||
return fieldErrors;
|
||||
}
|
||||
static assert(value) {
|
||||
if (!(value instanceof ZodError)) {
|
||||
throw new Error(`Not a ZodError: ${value}`);
|
||||
}
|
||||
}
|
||||
toString() {
|
||||
return this.message;
|
||||
}
|
||||
get message() {
|
||||
return JSON.stringify(this.issues, util_js_1.util.jsonStringifyReplacer, 2);
|
||||
}
|
||||
get isEmpty() {
|
||||
return this.issues.length === 0;
|
||||
}
|
||||
flatten(mapper = (issue) => issue.message) {
|
||||
const fieldErrors = Object.create(null);
|
||||
const formErrors = [];
|
||||
for (const sub of this.issues) {
|
||||
if (sub.path.length > 0) {
|
||||
const firstEl = sub.path[0];
|
||||
fieldErrors[firstEl] = fieldErrors[firstEl] || [];
|
||||
fieldErrors[firstEl].push(mapper(sub));
|
||||
}
|
||||
else {
|
||||
formErrors.push(mapper(sub));
|
||||
}
|
||||
}
|
||||
return { formErrors, fieldErrors };
|
||||
}
|
||||
get formErrors() {
|
||||
return this.flatten();
|
||||
}
|
||||
}
|
||||
exports.ZodError = ZodError;
|
||||
ZodError.create = (issues) => {
|
||||
const error = new ZodError(issues);
|
||||
return error;
|
||||
};
|
||||
|
||||
// seal-cjs-exports
|
||||
(function () {
|
||||
var keys = Object.getOwnPropertyNames(exports);
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
var desc = Object.getOwnPropertyDescriptor(exports, keys[i]);
|
||||
if (!desc || !desc.get || !desc.configurable) continue;
|
||||
var value;
|
||||
try {
|
||||
value = desc.get();
|
||||
} catch (e) {
|
||||
continue;
|
||||
}
|
||||
// a circular require may not have settled this one yet, so leave it live
|
||||
if (value === undefined) continue;
|
||||
Object.defineProperty(exports, keys[i], { value: value, writable: false, enumerable: desc.enumerable, configurable: false });
|
||||
}
|
||||
Object.freeze(exports);
|
||||
})();
|
||||
Reference in New Issue
Block a user