parseUtil.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 (key.value !== "__proto__" &&
  93. (typeof value.value !== "undefined" || pair.alwaysSet)) {
  94. finalObject[key.value] = value.value;
  95. }
  96. }
  97. return { status: status.value, value: finalObject };
  98. }
  99. }
  100. exports.ParseStatus = ParseStatus;
  101. exports.INVALID = Object.freeze({
  102. status: "aborted",
  103. });
  104. const DIRTY = (value) => ({ status: "dirty", value });
  105. exports.DIRTY = DIRTY;
  106. const OK = (value) => ({ status: "valid", value });
  107. exports.OK = OK;
  108. const isAborted = (x) => x.status === "aborted";
  109. exports.isAborted = isAborted;
  110. const isDirty = (x) => x.status === "dirty";
  111. exports.isDirty = isDirty;
  112. const isValid = (x) => x.status === "valid";
  113. exports.isValid = isValid;
  114. const isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
  115. exports.isAsync = isAsync;