UNPKG

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