CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

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