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