import * as core from "../core/index.js"; import { $ZodError } from "../core/index.js"; import * as util from "../core/util.js"; /** @deprecated Use `z.core.$ZodIssue` from `@zod/core` instead, especially if you are building a library on top of Zod. */ export type ZodIssue = core.$ZodIssue; /** An Error-like class used to store Zod validation issues. */ export interface ZodError extends $ZodError { /** @deprecated Use the `z.treeifyError(err)` function instead. */ format(): core.$ZodFormattedError; format(mapper: (issue: core.$ZodIssue) => U): core.$ZodFormattedError; /** @deprecated Use the `z.treeifyError(err)` function instead. */ flatten(): core.$ZodFlattenedError; flatten(mapper: (issue: core.$ZodIssue) => U): core.$ZodFlattenedError; /** @deprecated Push directly to `.issues` instead. */ addIssue(issue: core.$ZodIssue): void; /** @deprecated Push directly to `.issues` instead. */ addIssues(issues: core.$ZodIssue[]): void; /** @deprecated Check `err.issues.length === 0` instead. */ isEmpty: boolean; } /* Prototypes that already carry the lazy helper methods. Seeded with the * intrinsics so that `init` on a foreign object — it accepts any object — * can never install an accessor onto a prototype we do not own. */ const _installedErrorProtos = /* @__PURE__ */ new WeakSet([Object.prototype, Error.prototype]); /* Helper methods live as non-enumerable lazy getters on the shared * prototype instead of own properties on every instance. On first * access the getter allocates the per-instance closure and caches it * as a non-enumerable own property, so detached usage still works and * the allocation only happens for methods actually touched. */ function _lazyMethod(proto: object, key: string, make: (self: ZodError) => unknown): void { Object.defineProperty(proto, key, { configurable: true, enumerable: false, get(this: ZodError) { const value = make(this); Object.defineProperty(this, key, { value, configurable: true, writable: true }); return value; }, set(this: ZodError, value: unknown) { Object.defineProperty(this, key, { value, configurable: true, writable: true }); }, }); } const initializer = (inst: ZodError, issues: core.$ZodIssue[]) => { $ZodError.init(inst, issues); inst.name = "ZodError"; const proto = Object.getPrototypeOf(inst); if (_installedErrorProtos.has(proto)) return; _installedErrorProtos.add(proto); _lazyMethod(proto, "format", (self) => (mapper: any) => core.formatError(self, mapper)); _lazyMethod(proto, "flatten", (self) => (mapper: any) => core.flattenError(self, mapper)); _lazyMethod(proto, "addIssue", (self) => (issue: any) => { self.issues.push(issue); self.message = JSON.stringify(self.issues, util.jsonStringifyReplacer, 2); }); _lazyMethod(proto, "addIssues", (self) => (issues: any) => { self.issues.push(...issues); self.message = JSON.stringify(self.issues, util.jsonStringifyReplacer, 2); }); Object.defineProperty(proto, "isEmpty", { configurable: true, enumerable: false, get(this: ZodError) { return this.issues.length === 0; }, }); }; export const ZodError: core.$constructor = /*@__PURE__*/ core.$constructor("ZodError", initializer); export const ZodRealError: core.$constructor = /*@__PURE__*/ core.$constructor( "ZodError", initializer, undefined, { Parent: Error, } ); /** @deprecated Use `z.core.$ZodFlattenedError` instead. */ export type ZodFlattenedError = core.$ZodFlattenedError; /** @deprecated Use `z.core.$ZodFormattedError` instead. */ export type ZodFormattedError = core.$ZodFormattedError; /** @deprecated Use `z.core.$ZodErrorMap` instead. */ export type ZodErrorMap = core.$ZodErrorMap; /** @deprecated Use `z.core.$ZodRawIssue` instead. */ export type IssueData = core.$ZodRawIssue; // /** @deprecated Use `z.core.$ZodErrorMapCtx` instead. */ // export type ErrorMapCtx = core.$ZodErrorMapCtx;