CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
ZodError.cjs139 linesDownload Raw Back to v3
1"use strict";2Object.defineProperty(exports, "__esModule", { value: true });3exports.ZodError = exports.quotelessJson = exports.ZodIssueCode = void 0;4const util_js_1 = require("./helpers/util.cjs");5exports.ZodIssueCode = util_js_1.util.arrayToEnum([6    "invalid_type",7    "invalid_literal",8    "custom",9    "invalid_union",10    "invalid_union_discriminator",11    "invalid_enum_value",12    "unrecognized_keys",13    "invalid_arguments",14    "invalid_return_type",15    "invalid_date",16    "invalid_string",17    "too_small",18    "too_big",19    "invalid_intersection_types",20    "not_multiple_of",21    "not_finite",22]);23const quotelessJson = (obj) => {24    const json = JSON.stringify(obj, null, 2);25    return json.replace(/"([^"]+)":/g, "$1:");26};27exports.quotelessJson = quotelessJson;28class ZodError extends Error {29    get errors() {30        return this.issues;31    }32    constructor(issues) {33        super();34        this.issues = [];35        this.addIssue = (sub) => {36            this.issues = [...this.issues, sub];37        };38        this.addIssues = (subs = []) => {39            this.issues = [...this.issues, ...subs];40        };41        const actualProto = new.target.prototype;42        if (Object.setPrototypeOf) {43            // eslint-disable-next-line ban/ban44            Object.setPrototypeOf(this, actualProto);45        }46        else {47            this.__proto__ = actualProto;48        }49        this.name = "ZodError";50        this.issues = issues;51    }52    format(_mapper) {53        const mapper = _mapper ||54            function (issue) {55                return issue.message;56            };57        const fieldErrors = { _errors: [] };58        const processError = (error) => {59            for (const issue of error.issues) {60                if (issue.code === "invalid_union") {61                    issue.unionErrors.map(processError);62                }63                else if (issue.code === "invalid_return_type") {64                    processError(issue.returnTypeError);65                }66                else if (issue.code === "invalid_arguments") {67                    processError(issue.argumentsError);68                }69                else if (issue.path.length === 0) {70                    fieldErrors._errors.push(mapper(issue));71                }72                else {73                    let curr = fieldErrors;74                    let i = 0;75                    while (i < issue.path.length) {76                        const el = issue.path[i];77                        const terminal = i === issue.path.length - 1;78                        if (!terminal) {79                            curr[el] = curr[el] || { _errors: [] };80                            // if (typeof el === "string") {81                            //   curr[el] = curr[el] || { _errors: [] };82                            // } else if (typeof el === "number") {83                            //   const errorArray: any = [];84                            //   errorArray._errors = [];85                            //   curr[el] = curr[el] || errorArray;86                            // }87                        }88                        else {89                            curr[el] = curr[el] || { _errors: [] };90                            curr[el]._errors.push(mapper(issue));91                        }92                        curr = curr[el];93                        i++;94                    }95                }96            }97        };98        processError(this);99        return fieldErrors;100    }101    static assert(value) {102        if (!(value instanceof ZodError)) {103            throw new Error(`Not a ZodError: ${value}`);104        }105    }106    toString() {107        return this.message;108    }109    get message() {110        return JSON.stringify(this.issues, util_js_1.util.jsonStringifyReplacer, 2);111    }112    get isEmpty() {113        return this.issues.length === 0;114    }115    flatten(mapper = (issue) => issue.message) {116        const fieldErrors = Object.create(null);117        const formErrors = [];118        for (const sub of this.issues) {119            if (sub.path.length > 0) {120                const firstEl = sub.path[0];121                fieldErrors[firstEl] = fieldErrors[firstEl] || [];122                fieldErrors[firstEl].push(mapper(sub));123            }124            else {125                formErrors.push(mapper(sub));126            }127        }128        return { formErrors, fieldErrors };129    }130    get formErrors() {131        return this.flatten();132    }133}134exports.ZodError = ZodError;135ZodError.create = (issues) => {136    const error = new ZodError(issues);137    return error;138};139