Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. Object.setPrototypeOf(this, actualProto);
  41. }
  42. else {
  43. this.__proto__ = actualProto;
  44. }
  45. this.name = "ZodError";
  46. this.issues = issues;
  47. }
  48. get errors() {
  49. return this.issues;
  50. }
  51. format(_mapper) {
  52. const mapper = _mapper ||
  53. function (issue) {
  54. return issue.message;
  55. };
  56. const fieldErrors = { _errors: [] };
  57. const processError = (error) => {
  58. for (const issue of error.issues) {
  59. if (issue.code === "invalid_union") {
  60. issue.unionErrors.map(processError);
  61. }
  62. else if (issue.code === "invalid_return_type") {
  63. processError(issue.returnTypeError);
  64. }
  65. else if (issue.code === "invalid_arguments") {
  66. processError(issue.argumentsError);
  67. }
  68. else if (issue.path.length === 0) {
  69. fieldErrors._errors.push(mapper(issue));
  70. }
  71. else {
  72. let curr = fieldErrors;
  73. let i = 0;
  74. while (i < issue.path.length) {
  75. const el = issue.path[i];
  76. const terminal = i === issue.path.length - 1;
  77. if (!terminal) {
  78. curr[el] = curr[el] || { _errors: [] };
  79. }
  80. else {
  81. curr[el] = curr[el] || { _errors: [] };
  82. curr[el]._errors.push(mapper(issue));
  83. }
  84. curr = curr[el];
  85. i++;
  86. }
  87. }
  88. }
  89. };
  90. processError(this);
  91. return fieldErrors;
  92. }
  93. toString() {
  94. return this.message;
  95. }
  96. get message() {
  97. return JSON.stringify(this.issues, util_1.util.jsonStringifyReplacer, 2);
  98. }
  99. get isEmpty() {
  100. return this.issues.length === 0;
  101. }
  102. flatten(mapper = (issue) => issue.message) {
  103. const fieldErrors = {};
  104. const formErrors = [];
  105. for (const sub of this.issues) {
  106. if (sub.path.length > 0) {
  107. fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
  108. fieldErrors[sub.path[0]].push(mapper(sub));
  109. }
  110. else {
  111. formErrors.push(mapper(sub));
  112. }
  113. }
  114. return { formErrors, fieldErrors };
  115. }
  116. get formErrors() {
  117. return this.flatten();
  118. }
  119. }
  120. exports.ZodError = ZodError;
  121. ZodError.create = (issues) => {
  122. const error = new ZodError(issues);
  123. return error;
  124. };