hu.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import * as util from "../core/util.js";
  2. const Sizable = {
  3. string: { unit: "karakter", verb: "legyen" },
  4. file: { unit: "byte", verb: "legyen" },
  5. array: { unit: "elem", verb: "legyen" },
  6. set: { unit: "elem", verb: "legyen" },
  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" : "szám";
  16. }
  17. case "object": {
  18. if (Array.isArray(data)) {
  19. return "tömb";
  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: "bemenet",
  33. email: "email cím",
  34. url: "URL",
  35. emoji: "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 időbélyeg",
  47. date: "ISO dátum",
  48. time: "ISO idő",
  49. duration: "ISO időintervallum",
  50. ipv4: "IPv4 cím",
  51. ipv6: "IPv6 cím",
  52. cidrv4: "IPv4 tartomány",
  53. cidrv6: "IPv6 tartomány",
  54. base64: "base64-kódolt string",
  55. base64url: "base64url-kódolt string",
  56. json_string: "JSON string",
  57. e164: "E.164 szám",
  58. jwt: "JWT",
  59. template_literal: "bemenet",
  60. };
  61. const error = (issue) => {
  62. switch (issue.code) {
  63. case "invalid_type":
  64. return `Érvénytelen bemenet: a várt érték ${issue.expected}, a kapott érték ${parsedType(issue.input)}`;
  65. // return `Invalid input: expected ${issue.expected}, received ${util.getParsedType(issue.input)}`;
  66. case "invalid_value":
  67. if (issue.values.length === 1)
  68. return `Érvénytelen bemenet: a várt érték ${util.stringifyPrimitive(issue.values[0])}`;
  69. return `Érvénytelen opció: valamelyik érték várt ${util.joinValues(issue.values, "|")}`;
  70. case "too_big": {
  71. const adj = issue.inclusive ? "<=" : "<";
  72. const sizing = getSizing(issue.origin);
  73. if (sizing)
  74. return `Túl nagy: ${issue.origin ?? "érték"} mérete túl nagy ${adj}${issue.maximum.toString()} ${sizing.unit ?? "elem"}`;
  75. return `Túl nagy: a bemeneti érték ${issue.origin ?? "érték"} túl nagy: ${adj}${issue.maximum.toString()}`;
  76. }
  77. case "too_small": {
  78. const adj = issue.inclusive ? ">=" : ">";
  79. const sizing = getSizing(issue.origin);
  80. if (sizing) {
  81. return `Túl kicsi: a bemeneti érték ${issue.origin} mérete túl kicsi ${adj}${issue.minimum.toString()} ${sizing.unit}`;
  82. }
  83. return `Túl kicsi: a bemeneti érték ${issue.origin} túl kicsi ${adj}${issue.minimum.toString()}`;
  84. }
  85. case "invalid_format": {
  86. const _issue = issue;
  87. if (_issue.format === "starts_with")
  88. return `Érvénytelen string: "${_issue.prefix}" értékkel kell kezdődnie`;
  89. if (_issue.format === "ends_with")
  90. return `Érvénytelen string: "${_issue.suffix}" értékkel kell végződnie`;
  91. if (_issue.format === "includes")
  92. return `Érvénytelen string: "${_issue.includes}" értéket kell tartalmaznia`;
  93. if (_issue.format === "regex")
  94. return `Érvénytelen string: ${_issue.pattern} mintának kell megfelelnie`;
  95. return `Érvénytelen ${Nouns[_issue.format] ?? issue.format}`;
  96. }
  97. case "not_multiple_of":
  98. return `Érvénytelen szám: ${issue.divisor} többszörösének kell lennie`;
  99. case "unrecognized_keys":
  100. return `Ismeretlen kulcs${issue.keys.length > 1 ? "s" : ""}: ${util.joinValues(issue.keys, ", ")}`;
  101. case "invalid_key":
  102. return `Érvénytelen kulcs ${issue.origin}`;
  103. case "invalid_union":
  104. return "Érvénytelen bemenet";
  105. case "invalid_element":
  106. return `Érvénytelen érték: ${issue.origin}`;
  107. default:
  108. return `Érvénytelen bemenet`;
  109. }
  110. };
  111. export { error };
  112. export default function () {
  113. return {
  114. localeError: error,
  115. };
  116. }