validation-error.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. var __create = Object.create;
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __getProtoOf = Object.getPrototypeOf;
  6. var __hasOwnProp = Object.prototype.hasOwnProperty;
  7. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  8. var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
  9. var __export = (target, all) => {
  10. __markAsModule(target);
  11. for (var name in all)
  12. __defProp(target, name, { get: all[name], enumerable: true });
  13. };
  14. var __reExport = (target, module2, desc) => {
  15. if (module2 && typeof module2 === "object" || typeof module2 === "function") {
  16. for (let key of __getOwnPropNames(module2))
  17. if (!__hasOwnProp.call(target, key) && key !== "default")
  18. __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
  19. }
  20. return target;
  21. };
  22. var __toModule = (module2) => {
  23. return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
  24. };
  25. var __publicField = (obj, key, value) => {
  26. __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  27. return value;
  28. };
  29. __export(exports, {
  30. ValidationErrorItem: () => ValidationErrorItem,
  31. ValidationErrorItemOrigin: () => ValidationErrorItemOrigin,
  32. ValidationErrorItemType: () => ValidationErrorItemType,
  33. default: () => validation_error_default
  34. });
  35. var import_base_error = __toModule(require("./base-error"));
  36. var ValidationErrorItemType = /* @__PURE__ */ ((ValidationErrorItemType2) => {
  37. ValidationErrorItemType2["notnull violation"] = "CORE";
  38. ValidationErrorItemType2["string violation"] = "CORE";
  39. ValidationErrorItemType2["unique violation"] = "DB";
  40. ValidationErrorItemType2["validation error"] = "FUNCTION";
  41. return ValidationErrorItemType2;
  42. })(ValidationErrorItemType || {});
  43. var ValidationErrorItemOrigin = /* @__PURE__ */ ((ValidationErrorItemOrigin2) => {
  44. ValidationErrorItemOrigin2["CORE"] = "CORE";
  45. ValidationErrorItemOrigin2["DB"] = "DB";
  46. ValidationErrorItemOrigin2["FUNCTION"] = "FUNCTION";
  47. return ValidationErrorItemOrigin2;
  48. })(ValidationErrorItemOrigin || {});
  49. class ValidationErrorItem {
  50. constructor(message, type, path, value, instance, validatorKey, fnName, fnArgs) {
  51. __publicField(this, "message");
  52. __publicField(this, "type");
  53. __publicField(this, "path");
  54. __publicField(this, "value");
  55. __publicField(this, "origin");
  56. __publicField(this, "instance");
  57. __publicField(this, "validatorKey");
  58. __publicField(this, "validatorName");
  59. __publicField(this, "validatorArgs");
  60. this.message = message || "";
  61. this.type = null;
  62. this.path = path || null;
  63. this.value = value !== void 0 ? value : null;
  64. this.origin = null;
  65. this.instance = instance || null;
  66. this.validatorKey = validatorKey || null;
  67. this.validatorName = fnName || null;
  68. this.validatorArgs = fnArgs || [];
  69. if (type) {
  70. if (this.isValidationErrorItemOrigin(type)) {
  71. this.origin = type;
  72. } else {
  73. const lowercaseType = this.normalizeString(type);
  74. const realType = ValidationErrorItemType[lowercaseType];
  75. if (realType && ValidationErrorItemOrigin[realType]) {
  76. this.origin = realType;
  77. this.type = type;
  78. }
  79. }
  80. }
  81. }
  82. isValidationErrorItemOrigin(origin) {
  83. return ValidationErrorItemOrigin[origin] !== void 0;
  84. }
  85. normalizeString(str) {
  86. return str.toLowerCase().trim();
  87. }
  88. getValidatorKey(useTypeAsNS, NSSeparator) {
  89. const useTANS = useTypeAsNS === void 0 || !!useTypeAsNS;
  90. const NSSep = NSSeparator === void 0 ? "." : NSSeparator;
  91. const type = this.origin;
  92. const key = this.validatorKey || this.validatorName;
  93. const useNS = useTANS && type && ValidationErrorItemOrigin[type];
  94. if (useNS && (typeof NSSep !== "string" || !NSSep.length)) {
  95. throw new Error("Invalid namespace separator given, must be a non-empty string");
  96. }
  97. if (!(typeof key === "string" && key.length)) {
  98. return "";
  99. }
  100. return (useNS ? [this.origin, key].join(NSSep) : key).toLowerCase().trim();
  101. }
  102. }
  103. __publicField(ValidationErrorItem, "TypeStringMap", ValidationErrorItemType);
  104. __publicField(ValidationErrorItem, "Origins", ValidationErrorItemOrigin);
  105. class ValidationError extends import_base_error.default {
  106. constructor(message, errors, options = {}) {
  107. super(message);
  108. __publicField(this, "errors");
  109. this.name = "SequelizeValidationError";
  110. this.message = "Validation Error";
  111. this.errors = errors || [];
  112. if (message) {
  113. this.message = message;
  114. } else if (this.errors.length > 0 && this.errors[0].message) {
  115. this.message = this.errors.map((err) => `${err.type || err.origin}: ${err.message}`).join(",\n");
  116. }
  117. if (options.stack) {
  118. this.stack = options.stack;
  119. }
  120. }
  121. get(path) {
  122. return this.errors.reduce((reduced, error) => {
  123. if (error.path === path) {
  124. reduced.push(error);
  125. }
  126. return reduced;
  127. }, []);
  128. }
  129. }
  130. var validation_error_default = ValidationError;
  131. //# sourceMappingURL=validation-error.js.map