ZodError.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ZodError = exports.quotelessJson = exports.ZodIssueCode = void 0;
  4. const util_1 = require("./helpers/util");
  5. exports.ZodIssueCode = util_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. ]);
  23. const quotelessJson = (obj) => {
  24. const json = JSON.stringify(obj, null, 2);
  25. return json.replace(/"([^"]+)":/g, "$1:");
  26. };
  27. exports.quotelessJson = quotelessJson;
  28. class ZodError extends Error {
  29. constructor(issues) {
  30. super();
  31. this.issues = [];
  32. this.addIssue = (sub) => {
  33. this.issues = [...this.issues, sub];
  34. };
  35. this.addIssues = (subs = []) => {
  36. this.issues = [...this.issues, ...subs];
  37. };
  38. const actualProto = new.target.prototype;
  39. if (Object.setPrototypeOf) {
  40. // eslint-disable-next-line ban/ban
  41. Object.setPrototypeOf(this, actualProto);
  42. }
  43. else {
  44. this.__proto__ = actualProto;
  45. }
  46. this.name = "ZodError";
  47. this.issues = issues;
  48. }
  49. get errors() {
  50. return this.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. toString() {
  102. return this.message;
  103. }
  104. get message() {
  105. return JSON.stringify(this.issues, util_1.util.jsonStringifyReplacer, 2);
  106. }
  107. get isEmpty() {
  108. return this.issues.length === 0;
  109. }
  110. flatten(mapper = (issue) => issue.message) {
  111. const fieldErrors = {};
  112. const formErrors = [];
  113. for (const sub of this.issues) {
  114. if (sub.path.length > 0) {
  115. fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
  116. fieldErrors[sub.path[0]].push(mapper(sub));
  117. }
  118. else {
  119. formErrors.push(mapper(sub));
  120. }
  121. }
  122. return { formErrors, fieldErrors };
  123. }
  124. get formErrors() {
  125. return this.flatten();
  126. }
  127. }
  128. exports.ZodError = ZodError;
  129. ZodError.create = (issues) => {
  130. const error = new ZodError(issues);
  131. return error;
  132. };