ru.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.error = exports.parsedType = void 0;
  27. exports.default = default_1;
  28. const util = __importStar(require("../core/util.js"));
  29. function getRussianPlural(count, one, few, many) {
  30. const absCount = Math.abs(count);
  31. const lastDigit = absCount % 10;
  32. const lastTwoDigits = absCount % 100;
  33. if (lastTwoDigits >= 11 && lastTwoDigits <= 19) {
  34. return many;
  35. }
  36. if (lastDigit === 1) {
  37. return one;
  38. }
  39. if (lastDigit >= 2 && lastDigit <= 4) {
  40. return few;
  41. }
  42. return many;
  43. }
  44. const Sizable = {
  45. string: {
  46. unit: {
  47. one: "символ",
  48. few: "символа",
  49. many: "символов",
  50. },
  51. verb: "иметь",
  52. },
  53. file: {
  54. unit: {
  55. one: "байт",
  56. few: "байта",
  57. many: "байт",
  58. },
  59. verb: "иметь",
  60. },
  61. array: {
  62. unit: {
  63. one: "элемент",
  64. few: "элемента",
  65. many: "элементов",
  66. },
  67. verb: "иметь",
  68. },
  69. set: {
  70. unit: {
  71. one: "элемент",
  72. few: "элемента",
  73. many: "элементов",
  74. },
  75. verb: "иметь",
  76. },
  77. };
  78. function getSizing(origin) {
  79. return Sizable[origin] ?? null;
  80. }
  81. const parsedType = (data) => {
  82. const t = typeof data;
  83. switch (t) {
  84. case "number": {
  85. return Number.isNaN(data) ? "NaN" : "число";
  86. }
  87. case "object": {
  88. if (Array.isArray(data)) {
  89. return "массив";
  90. }
  91. if (data === null) {
  92. return "null";
  93. }
  94. if (Object.getPrototypeOf(data) !== Object.prototype && data.constructor) {
  95. return data.constructor.name;
  96. }
  97. }
  98. }
  99. return t;
  100. };
  101. exports.parsedType = parsedType;
  102. const Nouns = {
  103. regex: "ввод",
  104. email: "email адрес",
  105. url: "URL",
  106. emoji: "эмодзи",
  107. uuid: "UUID",
  108. uuidv4: "UUIDv4",
  109. uuidv6: "UUIDv6",
  110. nanoid: "nanoid",
  111. guid: "GUID",
  112. cuid: "cuid",
  113. cuid2: "cuid2",
  114. ulid: "ULID",
  115. xid: "XID",
  116. ksuid: "KSUID",
  117. datetime: "ISO дата и время",
  118. date: "ISO дата",
  119. time: "ISO время",
  120. duration: "ISO длительность",
  121. ipv4: "IPv4 адрес",
  122. ipv6: "IPv6 адрес",
  123. cidrv4: "IPv4 диапазон",
  124. cidrv6: "IPv6 диапазон",
  125. base64: "строка в формате base64",
  126. base64url: "строка в формате base64url",
  127. json_string: "JSON строка",
  128. e164: "номер E.164",
  129. jwt: "JWT",
  130. template_literal: "ввод",
  131. };
  132. const error = (issue) => {
  133. switch (issue.code) {
  134. case "invalid_type":
  135. return `Неверный ввод: ожидалось ${issue.expected}, получено ${(0, exports.parsedType)(issue.input)}`;
  136. case "invalid_value":
  137. if (issue.values.length === 1)
  138. return `Неверный ввод: ожидалось ${util.stringifyPrimitive(issue.values[0])}`;
  139. return `Неверный вариант: ожидалось одно из ${util.joinValues(issue.values, "|")}`;
  140. case "too_big": {
  141. const adj = issue.inclusive ? "<=" : "<";
  142. const sizing = getSizing(issue.origin);
  143. if (sizing) {
  144. const maxValue = Number(issue.maximum);
  145. const unit = getRussianPlural(maxValue, sizing.unit.one, sizing.unit.few, sizing.unit.many);
  146. return `Слишком большое значение: ожидалось, что ${issue.origin ?? "значение"} будет иметь ${adj}${issue.maximum.toString()} ${unit}`;
  147. }
  148. return `Слишком большое значение: ожидалось, что ${issue.origin ?? "значение"} будет ${adj}${issue.maximum.toString()}`;
  149. }
  150. case "too_small": {
  151. const adj = issue.inclusive ? ">=" : ">";
  152. const sizing = getSizing(issue.origin);
  153. if (sizing) {
  154. const minValue = Number(issue.minimum);
  155. const unit = getRussianPlural(minValue, sizing.unit.one, sizing.unit.few, sizing.unit.many);
  156. return `Слишком маленькое значение: ожидалось, что ${issue.origin} будет иметь ${adj}${issue.minimum.toString()} ${unit}`;
  157. }
  158. return `Слишком маленькое значение: ожидалось, что ${issue.origin} будет ${adj}${issue.minimum.toString()}`;
  159. }
  160. case "invalid_format": {
  161. const _issue = issue;
  162. if (_issue.format === "starts_with")
  163. return `Неверная строка: должна начинаться с "${_issue.prefix}"`;
  164. if (_issue.format === "ends_with")
  165. return `Неверная строка: должна заканчиваться на "${_issue.suffix}"`;
  166. if (_issue.format === "includes")
  167. return `Неверная строка: должна содержать "${_issue.includes}"`;
  168. if (_issue.format === "regex")
  169. return `Неверная строка: должна соответствовать шаблону ${_issue.pattern}`;
  170. return `Неверный ${Nouns[_issue.format] ?? issue.format}`;
  171. }
  172. case "not_multiple_of":
  173. return `Неверное число: должно быть кратным ${issue.divisor}`;
  174. case "unrecognized_keys":
  175. return `Нераспознанн${issue.keys.length > 1 ? "ые" : "ый"} ключ${issue.keys.length > 1 ? "и" : ""}: ${util.joinValues(issue.keys, ", ")}`;
  176. case "invalid_key":
  177. return `Неверный ключ в ${issue.origin}`;
  178. case "invalid_union":
  179. return "Неверные входные данные";
  180. case "invalid_element":
  181. return `Неверное значение в ${issue.origin}`;
  182. default:
  183. return `Неверные входные данные`;
  184. }
  185. };
  186. exports.error = error;
  187. function default_1() {
  188. return {
  189. localeError: error,
  190. };
  191. }