-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
111 lines (82 loc) · 2.76 KB
/
example.js
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
110
111
const T = require(".")
const assert = require("assert")
const user = T.obj({
name: T.str.atLeast(4),
pass: T.str.match(/[0-9a-zA-Z]{4,}/),
age: T.int.atLeast(0),
sex: T.oneOf(["male", "female"])
})
user.validate({
name: "Beckey",
pass: "P4SS",
age: 19,
sex: "female"
})
assert.throws(() => user.validate({
name: "Beckey",
pass: "P",
age: 19,
sex: "female"
}))
// Types
T.num.validate(3.14)
T.int.validate(3)
T.str.validate("3")
T.bool.validate(true)
T.null.validate(null)
T.null.validate(undefined)
T.array(T.int).validate([1,2,3])
T.obj({i: T.int}).validate({i: 3})
T.option(T.int).validate(null)
T.option(T.int).validate(undefined)
T.option(T.int).validate(3)
T.oneOf([1,2,3]).validate(3)
assert.throws(() => T.num.validate("45"))
assert.throws(() => T.num.validate({i: 5}))
assert.throws(() => T.int.validate(3.01))
assert.throws(() => T.int.validate("98"))
assert.throws(() => T.str.validate(7))
assert.throws(() => T.bool.validate(1))
assert.throws(() => T.null.validate(0))
assert.throws(() => T.array(T.any).validate({0: "v0"}))
assert.throws(() => T.obj({s: T.str}).validate({}))
assert.throws(() => T.obj({}).validate([]))
assert.throws(() => T.option(T.int).validate("string"))
assert.throws(() => T.oneOf([1, "a"]).validate("1"))
// Options
// Options can be chained
assert(T.str.atLeast(3).bellow(10).match(/0x[0-9a-f]*/).validate("0x181cd"))
assert.throws(() => T.num.atLeast(10).validate(9))
assert(T.num.atLeast(10).validate(10))
assert(T.num.atLeast(10).validate(11))
assert(T.num.atMost(10).validate(9))
assert(T.num.atMost(10).validate(10))
assert.throws(() => T.num.atMost(10).validate(11))
assert.throws(() => T.num.above(10).validate(9))
assert.throws(() => T.num.above(10).validate(10))
assert(T.num.above(10).validate(11))
assert(T.num.bellow(10).validate(9))
assert.throws(() => T.num.bellow(10).validate(10))
assert.throws(() => T.num.bellow(10).validate(11))
assert.throws(() => T.num.between(1, 3).validate(0))
assert(T.num.between(1, 3).validate(1))
assert(T.num.between(1, 3).validate(3))
assert.throws(() => T.num.between(1, 3).validate(4))
// Arrays and strings is compared with its length
T.array(T.int).atLeast(3).validate([1, 2, 3])
T.str.atLeast(7).validate("1234567")
assert(T.str.match(/[0-2][0-9]:[0-5][0-9]*/).validate("12:55"))
assert.throws(() => T.str.match(/[0-2][0-9]:[0-5][0-9]*/).validate("17:67"))
assert.throws(() => T.str.notMatch(/[0-2][0-9]:[0-5][0-9]*/).validate("12:55"))
assert(T.str.notMatch(/[0-2][0-9]:[0-5][0-9]*/).validate("17:67"))
// Custom validator
assert(T.addValidator((x, path) => {
if (x % 2 != 0) {
throw new Error(`${path} shoud be even`)
}
}))
// map
assert(T.int.map(x => x * 2).validate(10) === 20)
// catch
assert(T.int.catch((e, x) => Math.floor(x) || 0).validate(12.5) === 12)
console.log("All test passed.")