-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation-exception.ts
47 lines (43 loc) · 1.26 KB
/
validation-exception.ts
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
38
39
40
41
42
43
44
45
46
47
/**
* Classes representing the validation errors of a single value.
*/
import { ValueError } from '@sinclair/typebox/value';
/**
* Exception reporting the occurrence of one or more validation errors.
*/
export class ValidationException {
/**
* @param message Overall error message
* @param details The individual validation errors
*/
constructor(
readonly message: string,
readonly details: Readonly<ValueError[]> = []
) {}
/**
* Returns a string representation of the error. Provides the overall
* error message, followed by the specific error messages, one per line.
* @returns a string representation of the error.
*/
toString(): string {
let message = this.message;
if (this.details.length > 0) {
if (!message.endsWith(':')) {
message += ':';
}
for (const detail of this.details) {
message += '\n * ' + ValidationException.errorToString(detail);
}
}
return message;
}
/**
* Returns a string representation of a validation error, which precedes
* the error with its reference path if it occurs in an object.
*/
static errorToString(error: ValueError): string {
return error.path != ''
? `${error.path.substring(1)} - ${error.message}`
: error.message;
}
}