-
Notifications
You must be signed in to change notification settings - Fork 5
/
custom.ts
69 lines (60 loc) · 1.61 KB
/
custom.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import {
createFail,
Fail,
failSymbol,
getInternalRuntype,
isFail,
propagateFail,
Runtype,
setupInternalRuntype,
} from './runtype'
/**
* Create a validation error for custom runtypes
*/
export function createError(msg: string): Fail {
return createFail(failSymbol, msg)
}
/**
* Construct a custom runtype from a validation function.
*/
export function runtype<T>(fn: (v: unknown) => T | Fail): Runtype<T> {
return setupInternalRuntype<any>(
(v, failOrThrow) => {
const res = fn(v)
if (isFail(res)) {
return propagateFail(failOrThrow, res, v)
}
return res
},
{
isPure: false,
},
)
}
/**
* Explicit validation result containing the wrapped result or error.
*/
export type ValidationResult<T> =
| { ok: true; result: T }
| { ok: false; error: Fail }
/**
* Execute a runtype but do not throw Errors
*
* Return a ValidationResult instead.
* When its an Error, use `getFormattedError` on it to get a well formatted
* message for logging or reporting.
*
* Useful when writing your own runtypes and when the bad performance of
* exceptions and try-catch for error handling is of concern.
*/
export function use<T>(r: Runtype<T>, v: unknown): ValidationResult<T> {
const result = getInternalRuntype(r)(v, failSymbol)
if (isFail(result)) {
// we don't know who is using the result (passing error up the stack or
// consuming it with e.g. `st.getFormattedError`) so set the toplevel
// value (will be overwritten in case we're passed up anyways)
result.value = v
return { ok: false, error: result }
}
return { ok: true, result }
}