-
Notifications
You must be signed in to change notification settings - Fork 89
/
runtype.ts
109 lines (89 loc) · 2.59 KB
/
runtype.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { Result, Union, Union2, Intersect, Intersect2, Constraint, ConstraintCheck } from './index'
import { Reflect } from './reflect'
import show from './show'
/**
* A runtype determines at runtime whether a value conforms to a type specification.
*/
export interface Runtype<A> {
/**
* Verifies that a value conforms to this runtype. If so, returns the same value,
* statically typed. Otherwise throws an exception.
*/
check(x: any): A
/**
* Validates that a value conforms to this type, and returns a result indicating
* success or failure (does not throw).
*/
validate(x: any): Result<A>
/**
* A type guard for this runtype.
*/
guard(x: any): x is A
/**
* Union this Runtype with another.
*/
Or<B extends Rt>(B: B): Union2<this, B>
/**
* Intersect this Runtype with another.
*/
And<B extends Rt>(B: B): Intersect2<this, B>
/**
* Provide a function which validates some arbitrary constraint,
* returning true if the constraint is met, false if it failed
* for some reason. May also return a string which indicates an
* error and provides a descriptive message.
*/
withConstraint<K>(constraint: ConstraintCheck<this>, args?: K): Constraint<this, K>
/**
* Convert this to a Reflect, capable of introspecting the structure of the type.
*/
reflect: Reflect
/* @internal */ _falseWitness: A
}
/**
* Just a convenient synonym for internal use in defining new Runtypes.
*/
export type Rt = Runtype<any>
/**
* Obtains the static type associated with a Runtype.
*/
export type Static<A extends Rt> = A['_falseWitness']
export function create<A extends Rt>(check: (x: {}) => Static<A>, A: any): A {
A.check = check
A.validate = validate
A.guard = guard
A.Or = Or
A.And = And
A.withConstraint = withConstraint
A.reflect = A
A.toString = () => `Runtype<${show(A)}>`
return A
function validate(value: any): Result<A> {
try {
check(value)
return { success: true, value }
} catch ({ message }) {
return { success: false, message }
}
}
function guard(x: any): x is A {
return validate(x).success
}
function Or<B extends Rt>(B: B): Union2<A, B> {
return Union(A, B)
}
function And<B extends Rt>(B: B): Intersect2<A, B> {
return Intersect(A, B)
}
function withConstraint<K>(constraint: ConstraintCheck<A>, args?: K): Constraint<A, K> {
return Constraint(A, constraint, args)
}
}
export class ValidationError extends Error {
constructor(message: string) {
super(message)
}
}
export function validationError(message: string) {
return new ValidationError(message)
}