-
Notifications
You must be signed in to change notification settings - Fork 330
/
PathReporter.ts
50 lines (44 loc) · 1.06 KB
/
PathReporter.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
/**
* @since 1.0.0
*/
import { Reporter } from './Reporter'
import { Context, getFunctionName, ValidationError } from '.'
import { fold } from 'fp-ts/lib/Either'
function stringify(v: any): string {
if (typeof v === 'function') {
return getFunctionName(v)
}
if (typeof v === 'number' && !isFinite(v)) {
if (isNaN(v)) {
return 'NaN'
}
return v > 0 ? 'Infinity' : '-Infinity'
}
return JSON.stringify(v)
}
function getContextPath(context: Context): string {
return context.map(({ key, type }) => `${key}: ${type.name}`).join('/')
}
function getMessage(e: ValidationError): string {
return e.message !== undefined
? e.message
: `Invalid value ${stringify(e.value)} supplied to ${getContextPath(e.context)}`
}
/**
* @since 1.0.0
*/
export function failure(es: Array<ValidationError>): Array<string> {
return es.map(getMessage)
}
/**
* @since 1.0.0
*/
export function success(): Array<string> {
return ['No errors!']
}
/**
* @since 1.0.0
*/
export const PathReporter: Reporter<Array<string>> = {
report: fold(failure, success)
}