errors.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import * as core from "zod/v4/core";
  2. import { $ZodError } from "zod/v4/core";
  3. const initializer = (inst, issues) => {
  4. $ZodError.init(inst, issues);
  5. inst.name = "ZodError";
  6. Object.defineProperties(inst, {
  7. format: {
  8. value: (mapper) => core.formatError(inst, mapper),
  9. // enumerable: false,
  10. },
  11. flatten: {
  12. value: (mapper) => core.flattenError(inst, mapper),
  13. // enumerable: false,
  14. },
  15. addIssue: {
  16. value: (issue) => inst.issues.push(issue),
  17. // enumerable: false,
  18. },
  19. addIssues: {
  20. value: (issues) => inst.issues.push(...issues),
  21. // enumerable: false,
  22. },
  23. isEmpty: {
  24. get() {
  25. return inst.issues.length === 0;
  26. },
  27. // enumerable: false,
  28. },
  29. });
  30. // Object.defineProperty(inst, "isEmpty", {
  31. // get() {
  32. // return inst.issues.length === 0;
  33. // },
  34. // });
  35. };
  36. export const ZodError = core.$constructor("ZodError", initializer);
  37. export const ZodRealError = core.$constructor("ZodError", initializer, {
  38. Parent: Error,
  39. });
  40. // /** @deprecated Use `z.core.$ZodErrorMapCtx` instead. */
  41. // export type ErrorMapCtx = core.$ZodErrorMapCtx;