Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

parseUtil.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.isAsync = exports.isValid = exports.isDirty = exports.isAborted = exports.OK = exports.DIRTY = exports.INVALID = exports.ParseStatus = exports.addIssueToContext = exports.EMPTY_PATH = exports.makeIssue = void 0;
  7. const errors_1 = require("../errors");
  8. const en_1 = __importDefault(require("../locales/en"));
  9. const makeIssue = (params) => {
  10. const { data, path, errorMaps, issueData } = params;
  11. const fullPath = [...path, ...(issueData.path || [])];
  12. const fullIssue = {
  13. ...issueData,
  14. path: fullPath,
  15. };
  16. let errorMessage = "";
  17. const maps = errorMaps
  18. .filter((m) => !!m)
  19. .slice()
  20. .reverse();
  21. for (const map of maps) {
  22. errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message;
  23. }
  24. return {
  25. ...issueData,
  26. path: fullPath,
  27. message: issueData.message || errorMessage,
  28. };
  29. };
  30. exports.makeIssue = makeIssue;
  31. exports.EMPTY_PATH = [];
  32. function addIssueToContext(ctx, issueData) {
  33. const issue = (0, exports.makeIssue)({
  34. issueData: issueData,
  35. data: ctx.data,
  36. path: ctx.path,
  37. errorMaps: [
  38. ctx.common.contextualErrorMap,
  39. ctx.schemaErrorMap,
  40. (0, errors_1.getErrorMap)(),
  41. en_1.default,
  42. ].filter((x) => !!x),
  43. });
  44. ctx.common.issues.push(issue);
  45. }
  46. exports.addIssueToContext = addIssueToContext;
  47. class ParseStatus {
  48. constructor() {
  49. this.value = "valid";
  50. }
  51. dirty() {
  52. if (this.value === "valid")
  53. this.value = "dirty";
  54. }
  55. abort() {
  56. if (this.value !== "aborted")
  57. this.value = "aborted";
  58. }
  59. static mergeArray(status, results) {
  60. const arrayValue = [];
  61. for (const s of results) {
  62. if (s.status === "aborted")
  63. return exports.INVALID;
  64. if (s.status === "dirty")
  65. status.dirty();
  66. arrayValue.push(s.value);
  67. }
  68. return { status: status.value, value: arrayValue };
  69. }
  70. static async mergeObjectAsync(status, pairs) {
  71. const syncPairs = [];
  72. for (const pair of pairs) {
  73. syncPairs.push({
  74. key: await pair.key,
  75. value: await pair.value,
  76. });
  77. }
  78. return ParseStatus.mergeObjectSync(status, syncPairs);
  79. }
  80. static mergeObjectSync(status, pairs) {
  81. const finalObject = {};
  82. for (const pair of pairs) {
  83. const { key, value } = pair;
  84. if (key.status === "aborted")
  85. return exports.INVALID;
  86. if (value.status === "aborted")
  87. return exports.INVALID;
  88. if (key.status === "dirty")
  89. status.dirty();
  90. if (value.status === "dirty")
  91. status.dirty();
  92. if (typeof value.value !== "undefined" || pair.alwaysSet) {
  93. finalObject[key.value] = value.value;
  94. }
  95. }
  96. return { status: status.value, value: finalObject };
  97. }
  98. }
  99. exports.ParseStatus = ParseStatus;
  100. exports.INVALID = Object.freeze({
  101. status: "aborted",
  102. });
  103. const DIRTY = (value) => ({ status: "dirty", value });
  104. exports.DIRTY = DIRTY;
  105. const OK = (value) => ({ status: "valid", value });
  106. exports.OK = OK;
  107. const isAborted = (x) => x.status === "aborted";
  108. exports.isAborted = isAborted;
  109. const isDirty = (x) => x.status === "dirty";
  110. exports.isDirty = isDirty;
  111. const isValid = (x) => x.status === "valid";
  112. exports.isValid = isValid;
  113. const isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
  114. exports.isAsync = isAsync;