errors.js 1.5 KB

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