fr.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import * as util from "../core/util.js";
  2. const Sizable = {
  3. string: { unit: "caractères", verb: "avoir" },
  4. file: { unit: "octets", verb: "avoir" },
  5. array: { unit: "éléments", verb: "avoir" },
  6. set: { unit: "éléments", verb: "avoir" },
  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" : "nombre";
  16. }
  17. case "object": {
  18. if (Array.isArray(data)) {
  19. return "tableau";
  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: "entrée",
  33. email: "adresse e-mail",
  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: "date et heure ISO",
  47. date: "date ISO",
  48. time: "heure ISO",
  49. duration: "durée ISO",
  50. ipv4: "adresse IPv4",
  51. ipv6: "adresse IPv6",
  52. cidrv4: "plage IPv4",
  53. cidrv6: "plage IPv6",
  54. base64: "chaîne encodée en base64",
  55. base64url: "chaîne encodée en base64url",
  56. json_string: "chaîne JSON",
  57. e164: "numéro E.164",
  58. jwt: "JWT",
  59. template_literal: "entrée",
  60. };
  61. const error = (issue) => {
  62. switch (issue.code) {
  63. case "invalid_type":
  64. return `Entrée invalide : ${issue.expected} attendu, ${parsedType(issue.input)} reçu`;
  65. case "invalid_value":
  66. if (issue.values.length === 1)
  67. return `Entrée invalide : ${util.stringifyPrimitive(issue.values[0])} attendu`;
  68. return `Option invalide : une valeur parmi ${util.joinValues(issue.values, "|")} attendue`;
  69. case "too_big": {
  70. const adj = issue.inclusive ? "<=" : "<";
  71. const sizing = getSizing(issue.origin);
  72. if (sizing)
  73. return `Trop grand : ${issue.origin ?? "valeur"} doit ${sizing.verb} ${adj}${issue.maximum.toString()} ${sizing.unit ?? "élément(s)"}`;
  74. return `Trop grand : ${issue.origin ?? "valeur"} doit être ${adj}${issue.maximum.toString()}`;
  75. }
  76. case "too_small": {
  77. const adj = issue.inclusive ? ">=" : ">";
  78. const sizing = getSizing(issue.origin);
  79. if (sizing) {
  80. return `Trop petit : ${issue.origin} doit ${sizing.verb} ${adj}${issue.minimum.toString()} ${sizing.unit}`;
  81. }
  82. return `Trop petit : ${issue.origin} doit être ${adj}${issue.minimum.toString()}`;
  83. }
  84. case "invalid_format": {
  85. const _issue = issue;
  86. if (_issue.format === "starts_with")
  87. return `Chaîne invalide : doit commencer par "${_issue.prefix}"`;
  88. if (_issue.format === "ends_with")
  89. return `Chaîne invalide : doit se terminer par "${_issue.suffix}"`;
  90. if (_issue.format === "includes")
  91. return `Chaîne invalide : doit inclure "${_issue.includes}"`;
  92. if (_issue.format === "regex")
  93. return `Chaîne invalide : doit correspondre au modèle ${_issue.pattern}`;
  94. return `${Nouns[_issue.format] ?? issue.format} invalide`;
  95. }
  96. case "not_multiple_of":
  97. return `Nombre invalide : doit être un multiple de ${issue.divisor}`;
  98. case "unrecognized_keys":
  99. return `Clé${issue.keys.length > 1 ? "s" : ""} non reconnue${issue.keys.length > 1 ? "s" : ""} : ${util.joinValues(issue.keys, ", ")}`;
  100. case "invalid_key":
  101. return `Clé invalide dans ${issue.origin}`;
  102. case "invalid_union":
  103. return "Entrée invalide";
  104. case "invalid_element":
  105. return `Valeur invalide dans ${issue.origin}`;
  106. default:
  107. return `Entrée invalide`;
  108. }
  109. };
  110. export { error };
  111. export default function () {
  112. return {
  113. localeError: error,
  114. };
  115. }