UNPKG

2.98 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 * @param showConstraintMessages show constraint messages instead of constraint names
13 */
14 ValidationError.prototype.toString = function (shouldDecorate, hasParent, parentPath, showConstraintMessages) {
15 var _this = this;
16 if (shouldDecorate === void 0) { shouldDecorate = false; }
17 if (hasParent === void 0) { hasParent = false; }
18 if (parentPath === void 0) { parentPath = ""; }
19 if (showConstraintMessages === void 0) { showConstraintMessages = false; }
20 var boldStart = shouldDecorate ? "\u001B[1m" : "";
21 var boldEnd = shouldDecorate ? "\u001B[22m" : "";
22 var constraintsToString = function () { var _a; return (showConstraintMessages ? Object.values : Object.keys)((_a = _this.constraints) !== null && _a !== void 0 ? _a : {}).join(", "); };
23 var propConstraintFailed = function (propertyName) {
24 return " - property ".concat(boldStart).concat(parentPath).concat(propertyName).concat(boldEnd, " has failed the following constraints: ").concat(boldStart).concat(constraintsToString()).concat(boldEnd, " \n");
25 };
26 if (!hasParent) {
27 return ("An instance of ".concat(boldStart).concat(this.target ? this.target.constructor.name : 'an object').concat(boldEnd, " has failed the validation:\n") +
28 (this.constraints ? propConstraintFailed(this.property) : "") +
29 (this.children
30 ? this.children
31 .map(function (childError) { return childError.toString(shouldDecorate, true, _this.property, showConstraintMessages); })
32 .join("")
33 : ""));
34 }
35 else {
36 // we format numbers as array indexes for better readability.
37 var formattedProperty_1 = Number.isInteger(+this.property)
38 ? "[".concat(this.property, "]")
39 : "".concat(parentPath ? "." : "").concat(this.property);
40 if (this.constraints) {
41 return propConstraintFailed(formattedProperty_1);
42 }
43 else {
44 return this.children
45 ? this.children
46 .map(function (childError) {
47 return childError.toString(shouldDecorate, true, "".concat(parentPath).concat(formattedProperty_1), showConstraintMessages);
48 })
49 .join("")
50 : "";
51 }
52 }
53 };
54 return ValidationError;
55}());
56export { ValidationError };
57//# sourceMappingURL=ValidationError.js.map
\No newline at end of file