UNPKG

2.47 kBJavaScriptView Raw
1/**
2 * Validation error description.
3 */
4var ValidationError = /** @class */ (function () {
5 function ValidationError() {
6 }
7 /**
8 *
9 * @param shouldDecorate decorate the message with ANSI formatter escape codes for better readability
10 * @param hasParent true when the error is a child of an another one
11 * @param parentPath path as string to the parent of this property
12 */
13 ValidationError.prototype.toString = function (shouldDecorate, hasParent, parentPath) {
14 var _this = this;
15 if (shouldDecorate === void 0) { shouldDecorate = false; }
16 if (hasParent === void 0) { hasParent = false; }
17 if (parentPath === void 0) { parentPath = ""; }
18 var boldStart = shouldDecorate ? "\u001B[1m" : "";
19 var boldEnd = shouldDecorate ? "\u001B[22m" : "";
20 var propConstraintFailed = function (propertyName) {
21 return " - property ".concat(boldStart).concat(parentPath).concat(propertyName).concat(boldEnd, " has failed the following constraints: ").concat(boldStart).concat(Object.keys(_this.constraints).join(", ")).concat(boldEnd, " \n");
22 };
23 if (!hasParent) {
24 return ("An instance of ".concat(boldStart).concat(this.target ? this.target.constructor.name : 'an object').concat(boldEnd, " has failed the validation:\n") +
25 (this.constraints ? propConstraintFailed(this.property) : "") +
26 (this.children
27 ? this.children.map(function (childError) { return childError.toString(shouldDecorate, true, _this.property); }).join("")
28 : ""));
29 }
30 else {
31 // we format numbers as array indexes for better readability.
32 var formattedProperty_1 = Number.isInteger(+this.property)
33 ? "[".concat(this.property, "]")
34 : "".concat(parentPath ? "." : "").concat(this.property);
35 if (this.constraints) {
36 return propConstraintFailed(formattedProperty_1);
37 }
38 else {
39 return this.children
40 ? this.children
41 .map(function (childError) { return childError.toString(shouldDecorate, true, "".concat(parentPath).concat(formattedProperty_1)); })
42 .join("")
43 : "";
44 }
45 }
46 };
47 return ValidationError;
48}());
49export { ValidationError };
50//# sourceMappingURL=ValidationError.js.map
\No newline at end of file