forked from javascript-obfuscator/javascript-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationErrorsFormatter.ts
More file actions
37 lines (32 loc) · 1.1 KB
/
Copy pathValidationErrorsFormatter.ts
File metadata and controls
37 lines (32 loc) · 1.1 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
import { ValidationError } from 'class-validator';
import { TObject } from '../types/TObject';
export class ValidationErrorsFormatter {
/**
* @param {ValidationError[]} errors
* @returns {string}
*/
public static format (errors: ValidationError[]): string {
return errors
.reduce(
(errorMessages: string[], error: ValidationError) => ([
...errorMessages,
ValidationErrorsFormatter.formatWithNestedConstraints(error)
]),
[]
)
.join('\n');
}
/**
* @param {ValidationError} error
* @returns {string}
*/
private static formatWithNestedConstraints (error: ValidationError): string {
const constraints: TObject<string> = error.constraints;
const rootError: string = `\`${error.property}\` errors:\n`;
const nestedErrors: string = Object
.keys(constraints)
.map((constraint: string) => ` - ${constraints[constraint]}\n`)
.join();
return `${rootError}${nestedErrors}`;
}
}