ja.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import * as util from "../core/util.js";
  2. const Sizable = {
  3. string: { unit: "文字", verb: "である" },
  4. file: { unit: "バイト", verb: "である" },
  5. array: { unit: "要素", verb: "である" },
  6. set: { unit: "要素", verb: "である" },
  7. };
  8. function getSizing(origin) {
  9. return Sizable[origin] ?? null;
  10. }
  11. export const parsedType = (data) => {
  12. const t = typeof data;
  13. switch (t) {
  14. case "number": {
  15. return Number.isNaN(data) ? "NaN" : "数値";
  16. }
  17. case "object": {
  18. if (Array.isArray(data)) {
  19. return "配列";
  20. }
  21. if (data === null) {
  22. return "null";
  23. }
  24. if (Object.getPrototypeOf(data) !== Object.prototype && data.constructor) {
  25. return data.constructor.name;
  26. }
  27. }
  28. }
  29. return t;
  30. };
  31. const Nouns = {
  32. regex: "入力値",
  33. email: "メールアドレス",
  34. url: "URL",
  35. emoji: "絵文字",
  36. uuid: "UUID",
  37. uuidv4: "UUIDv4",
  38. uuidv6: "UUIDv6",
  39. nanoid: "nanoid",
  40. guid: "GUID",
  41. cuid: "cuid",
  42. cuid2: "cuid2",
  43. ulid: "ULID",
  44. xid: "XID",
  45. ksuid: "KSUID",
  46. datetime: "ISO日時",
  47. date: "ISO日付",
  48. time: "ISO時刻",
  49. duration: "ISO期間",
  50. ipv4: "IPv4アドレス",
  51. ipv6: "IPv6アドレス",
  52. cidrv4: "IPv4範囲",
  53. cidrv6: "IPv6範囲",
  54. base64: "base64エンコード文字列",
  55. base64url: "base64urlエンコード文字列",
  56. json_string: "JSON文字列",
  57. e164: "E.164番号",
  58. jwt: "JWT",
  59. template_literal: "入力値",
  60. };
  61. const error = (issue) => {
  62. switch (issue.code) {
  63. case "invalid_type":
  64. return `無効な入力: ${issue.expected}が期待されましたが、${parsedType(issue.input)}が入力されました`;
  65. case "invalid_value":
  66. if (issue.values.length === 1)
  67. return `無効な入力: ${util.stringifyPrimitive(issue.values[0])}が期待されました`;
  68. return `無効な選択: ${util.joinValues(issue.values, "、")}のいずれかである必要があります`;
  69. case "too_big": {
  70. const adj = issue.inclusive ? "<=" : "<";
  71. const sizing = getSizing(issue.origin);
  72. if (sizing)
  73. return `大きすぎる値: ${issue.origin ?? "値"}は${issue.maximum.toString()}${sizing.unit ?? "要素"}${adj}である必要があります`;
  74. return `大きすぎる値: ${issue.origin ?? "値"}は${issue.maximum.toString()}${adj}である必要があります`;
  75. }
  76. case "too_small": {
  77. const adj = issue.inclusive ? ">=" : ">";
  78. const sizing = getSizing(issue.origin);
  79. if (sizing)
  80. return `小さすぎる値: ${issue.origin}は${issue.minimum.toString()}${sizing.unit}${adj}である必要があります`;
  81. return `小さすぎる値: ${issue.origin}は${issue.minimum.toString()}${adj}である必要があります`;
  82. }
  83. case "invalid_format": {
  84. const _issue = issue;
  85. if (_issue.format === "starts_with")
  86. return `無効な文字列: "${_issue.prefix}"で始まる必要があります`;
  87. if (_issue.format === "ends_with")
  88. return `無効な文字列: "${_issue.suffix}"で終わる必要があります`;
  89. if (_issue.format === "includes")
  90. return `無効な文字列: "${_issue.includes}"を含む必要があります`;
  91. if (_issue.format === "regex")
  92. return `無効な文字列: パターン${_issue.pattern}に一致する必要があります`;
  93. return `無効な${Nouns[_issue.format] ?? issue.format}`;
  94. }
  95. case "not_multiple_of":
  96. return `無効な数値: ${issue.divisor}の倍数である必要があります`;
  97. case "unrecognized_keys":
  98. return `認識されていないキー${issue.keys.length > 1 ? "群" : ""}: ${util.joinValues(issue.keys, "、")}`;
  99. case "invalid_key":
  100. return `${issue.origin}内の無効なキー`;
  101. case "invalid_union":
  102. return "無効な入力";
  103. case "invalid_element":
  104. return `${issue.origin}内の無効な値`;
  105. default:
  106. return `無効な入力`;
  107. }
  108. };
  109. export { error };
  110. export default function () {
  111. return {
  112. localeError: error,
  113. };
  114. }