1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ValidationError = void 0;
|
4 |
|
5 |
|
6 |
|
7 | class ValidationError {
|
8 | |
9 |
|
10 |
|
11 |
|
12 |
|
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 |
|
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 | }
|
43 | exports.ValidationError = ValidationError;
|
44 |
|
\ | No newline at end of file |