en.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const util_1 = require("../helpers/util");
  4. const ZodError_1 = require("../ZodError");
  5. const errorMap = (issue, _ctx) => {
  6. let message;
  7. switch (issue.code) {
  8. case ZodError_1.ZodIssueCode.invalid_type:
  9. if (issue.received === util_1.ZodParsedType.undefined) {
  10. message = "Required";
  11. }
  12. else {
  13. message = `Expected ${issue.expected}, received ${issue.received}`;
  14. }
  15. break;
  16. case ZodError_1.ZodIssueCode.invalid_literal:
  17. message = `Invalid literal value, expected ${JSON.stringify(issue.expected, util_1.util.jsonStringifyReplacer)}`;
  18. break;
  19. case ZodError_1.ZodIssueCode.unrecognized_keys:
  20. message = `Unrecognized key(s) in object: ${util_1.util.joinValues(issue.keys, ", ")}`;
  21. break;
  22. case ZodError_1.ZodIssueCode.invalid_union:
  23. message = `Invalid input`;
  24. break;
  25. case ZodError_1.ZodIssueCode.invalid_union_discriminator:
  26. message = `Invalid discriminator value. Expected ${util_1.util.joinValues(issue.options)}`;
  27. break;
  28. case ZodError_1.ZodIssueCode.invalid_enum_value:
  29. message = `Invalid enum value. Expected ${util_1.util.joinValues(issue.options)}, received '${issue.received}'`;
  30. break;
  31. case ZodError_1.ZodIssueCode.invalid_arguments:
  32. message = `Invalid function arguments`;
  33. break;
  34. case ZodError_1.ZodIssueCode.invalid_return_type:
  35. message = `Invalid function return type`;
  36. break;
  37. case ZodError_1.ZodIssueCode.invalid_date:
  38. message = `Invalid date`;
  39. break;
  40. case ZodError_1.ZodIssueCode.invalid_string:
  41. if (typeof issue.validation === "object") {
  42. if ("includes" in issue.validation) {
  43. message = `Invalid input: must include "${issue.validation.includes}"`;
  44. if (typeof issue.validation.position === "number") {
  45. message = `${message} at one or more positions greater than or equal to ${issue.validation.position}`;
  46. }
  47. }
  48. else if ("startsWith" in issue.validation) {
  49. message = `Invalid input: must start with "${issue.validation.startsWith}"`;
  50. }
  51. else if ("endsWith" in issue.validation) {
  52. message = `Invalid input: must end with "${issue.validation.endsWith}"`;
  53. }
  54. else {
  55. util_1.util.assertNever(issue.validation);
  56. }
  57. }
  58. else if (issue.validation !== "regex") {
  59. message = `Invalid ${issue.validation}`;
  60. }
  61. else {
  62. message = "Invalid";
  63. }
  64. break;
  65. case ZodError_1.ZodIssueCode.too_small:
  66. if (issue.type === "array")
  67. message = `Array must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `more than`} ${issue.minimum} element(s)`;
  68. else if (issue.type === "string")
  69. message = `String must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
  70. else if (issue.type === "number")
  71. message = `Number must be ${issue.exact
  72. ? `exactly equal to `
  73. : issue.inclusive
  74. ? `greater than or equal to `
  75. : `greater than `}${issue.minimum}`;
  76. else if (issue.type === "date")
  77. message = `Date must be ${issue.exact
  78. ? `exactly equal to `
  79. : issue.inclusive
  80. ? `greater than or equal to `
  81. : `greater than `}${new Date(Number(issue.minimum))}`;
  82. else
  83. message = "Invalid input";
  84. break;
  85. case ZodError_1.ZodIssueCode.too_big:
  86. if (issue.type === "array")
  87. message = `Array must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `less than`} ${issue.maximum} element(s)`;
  88. else if (issue.type === "string")
  89. message = `String must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
  90. else if (issue.type === "number")
  91. message = `Number must be ${issue.exact
  92. ? `exactly`
  93. : issue.inclusive
  94. ? `less than or equal to`
  95. : `less than`} ${issue.maximum}`;
  96. else if (issue.type === "bigint")
  97. message = `BigInt must be ${issue.exact
  98. ? `exactly`
  99. : issue.inclusive
  100. ? `less than or equal to`
  101. : `less than`} ${issue.maximum}`;
  102. else if (issue.type === "date")
  103. message = `Date must be ${issue.exact
  104. ? `exactly`
  105. : issue.inclusive
  106. ? `smaller than or equal to`
  107. : `smaller than`} ${new Date(Number(issue.maximum))}`;
  108. else
  109. message = "Invalid input";
  110. break;
  111. case ZodError_1.ZodIssueCode.custom:
  112. message = `Invalid input`;
  113. break;
  114. case ZodError_1.ZodIssueCode.invalid_intersection_types:
  115. message = `Intersection results could not be merged`;
  116. break;
  117. case ZodError_1.ZodIssueCode.not_multiple_of:
  118. message = `Number must be a multiple of ${issue.multipleOf}`;
  119. break;
  120. case ZodError_1.ZodIssueCode.not_finite:
  121. message = "Number must be finite";
  122. break;
  123. default:
  124. message = _ctx.defaultError;
  125. util_1.util.assertNever(issue);
  126. }
  127. return { message };
  128. };
  129. exports.default = errorMap;