parseUtil.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { getErrorMap } from "../errors.js";
  2. import defaultErrorMap from "../locales/en.js";
  3. export const makeIssue = (params) => {
  4. const { data, path, errorMaps, issueData } = params;
  5. const fullPath = [...path, ...(issueData.path || [])];
  6. const fullIssue = {
  7. ...issueData,
  8. path: fullPath,
  9. };
  10. if (issueData.message !== undefined) {
  11. return {
  12. ...issueData,
  13. path: fullPath,
  14. message: issueData.message,
  15. };
  16. }
  17. let errorMessage = "";
  18. const maps = errorMaps
  19. .filter((m) => !!m)
  20. .slice()
  21. .reverse();
  22. for (const map of maps) {
  23. errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message;
  24. }
  25. return {
  26. ...issueData,
  27. path: fullPath,
  28. message: errorMessage,
  29. };
  30. };
  31. export const EMPTY_PATH = [];
  32. export function addIssueToContext(ctx, issueData) {
  33. const overrideMap = getErrorMap();
  34. const issue = makeIssue({
  35. issueData: issueData,
  36. data: ctx.data,
  37. path: ctx.path,
  38. errorMaps: [
  39. ctx.common.contextualErrorMap, // contextual error map is first priority
  40. ctx.schemaErrorMap, // then schema-bound map if available
  41. overrideMap, // then global override map
  42. overrideMap === defaultErrorMap ? undefined : defaultErrorMap, // then global default map
  43. ].filter((x) => !!x),
  44. });
  45. ctx.common.issues.push(issue);
  46. }
  47. export 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 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. const key = await pair.key;
  74. const value = await pair.value;
  75. syncPairs.push({
  76. key,
  77. value,
  78. });
  79. }
  80. return ParseStatus.mergeObjectSync(status, syncPairs);
  81. }
  82. static mergeObjectSync(status, pairs) {
  83. const finalObject = {};
  84. for (const pair of pairs) {
  85. const { key, value } = pair;
  86. if (key.status === "aborted")
  87. return INVALID;
  88. if (value.status === "aborted")
  89. return INVALID;
  90. if (key.status === "dirty")
  91. status.dirty();
  92. if (value.status === "dirty")
  93. status.dirty();
  94. if (key.value !== "__proto__" && (typeof value.value !== "undefined" || pair.alwaysSet)) {
  95. finalObject[key.value] = value.value;
  96. }
  97. }
  98. return { status: status.value, value: finalObject };
  99. }
  100. }
  101. export const INVALID = Object.freeze({
  102. status: "aborted",
  103. });
  104. export const DIRTY = (value) => ({ status: "dirty", value });
  105. export const OK = (value) => ({ status: "valid", value });
  106. export const isAborted = (x) => x.status === "aborted";
  107. export const isDirty = (x) => x.status === "dirty";
  108. export const isValid = (x) => x.status === "valid";
  109. export const isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;