-
Notifications
You must be signed in to change notification settings - Fork 8
/
reporters.ts
54 lines (46 loc) · 1.45 KB
/
reporters.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
48
49
50
51
52
53
54
import { Context, ValidationError } from "io-ts";
import { Reporter } from "io-ts/lib/Reporter";
/**
* Translate a context to a more readable path.
*
* e.g.:
*
* "is not a non empty string"
* ".a is not a number"
* ".c.b is not a non empty string"
*/
function getContextPath(context: Context): string {
if (context.length === 0) {
return "] (decoder info n/a)";
}
const keysPath = context.map(({ key }) => key).join(".");
const lastType = context[context.length - 1].type;
if ("never" === lastType.name) {
return `${keysPath}] is not a known property`;
}
return `${keysPath}] is not a valid [${lastType.name}]`;
}
function getMessage(value: unknown, context: Context): string {
return `value [${JSON.stringify(value)}] at [root${getContextPath(context)}`;
}
/**
* Translates validation errors to more readable messages.
*/
export function errorsToReadableMessages(
es: ReadonlyArray<ValidationError>
): ReadonlyArray<string> {
return es.map(e => getMessage(e.value, e.context));
}
function success(): ReadonlyArray<string> {
return ["No errors!"];
}
export function readableReport(errors: ReadonlyArray<ValidationError>): string {
return errorsToReadableMessages(errors).join("\n");
}
/**
* A validation error reporter that translates validation errors to more
* readable messages.
*/
export const ReadableReporter: Reporter<ReadonlyArray<string>> = {
report: validation => validation.fold(errorsToReadableMessages, success)
};