-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
231 lines (195 loc) · 4.43 KB
/
index.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
function type(obj) {
return Object.prototype.toString.call(obj).slice(8, -1)
}
class Schemable {
constructor(validator, map, error) {
this.validator = validator || []
this.mapper = map || []
this.error = error || []
}
addValidator(f) {
return new Schemable(this.validator.concat(f), this.mapper, this.error)
}
map(f) {
return new Schemable(this.validator, this.mapper.concat(f), this.error)
}
catch(f) {
return new Schemable(this.validator, this.mapper, this.error.concat(f))
}
get any() {
return this
}
get num() {
return this.addValidator((x, path) => {
if (type(x) != "Number") {
throw new Error(`${path} should be a number`)
}
})
}
get number() {
return this.num
}
get int() {
return this.addValidator((x, path) => {
if (Math.floor(x) !== x) {
throw new Error(`${path} should be an interger`)
}
})
}
get interger() {
return this.int
}
get str() {
return this.addValidator((x, path) => {
if (type(x) != "String") {
throw new Error(`${path} should be a string`)
}
})
}
get string() {
return this.str
}
get bool() {
return this.addValidator((x, path) => {
if (type(x) != "Boolean") {
throw new Error(`${path} should be a boolean`)
}
})
}
get null() {
return this.addValidator((x, path) => {
if (type(x) != "Null" && type(x) != "Undefined") {
throw new Error(`${path} shoud be null or undefined`)
}
})
}
get undefined() {
return this.null
}
array(obj) {
return this.addValidator((x, path) => {
if (type(x) != "Array") {
throw new Error(`${path} should be an array`)
}
for (var i = 0; i < x.length; i++) {
obj.validate(x[i], `${path}[${i}]`)
}
})
}
obj(obj) {
return this.addValidator((x, path) => {
if (type(x) != "Object") throw new Error(`${path} should be an object`)
Object.keys(obj).forEach(key => {
const p = path == "It" ? key : `${path}.${key}`
obj[key].validate(x[key], p)
})
})
}
object(obj) {
return this.obj(obj)
}
option(nullable) {
return this.addValidator((x, path) => {
if (x != null) nullable.validate(x)
})
}
oneOf(array) {
return this.addValidator((x, path) => {
if (array.find(v => v === x) == undefined) {
throw new Error(`${path} shoud be one of ${array}`)
}
})
}
atLeast(n) {
return this.addValidator((x, path) => {
switch (type(x)) {
case "Number":
if (x < n) throw new Error(`${path} should be at least ${n}`)
case "Array":
case "String":
if (x.length < n) throw new Error(`${path}.length should be at least ${n}`)
}
})
}
atMost(n) {
return this.addValidator((x, path) => {
switch (type(x)) {
case "Number":
if (x > n) throw new Error(`${path} should be at most ${n}`)
case "Array":
case "String":
if (x.length > n) throw new Error(`${path}.length should be at most ${n}`)
}
})
}
above(n) {
return this.addValidator((x, path) => {
switch (type(x)) {
case "Number":
if (x <= n) throw new Error(`${path} should be above ${n}`)
case "Array":
case "String":
if (x.length <= n) throw new Error(`${path}.length should be above ${n}`)
}
})
}
bellow(n) {
return this.addValidator((x, path) => {
switch (type(x)) {
case "Number":
if (x >= n) throw new Error(`${path} should be bellow ${n}`)
case "Array":
case "String":
if (x.length >= n) throw new Error(`${path}.length should be bellow ${n}`)
}
})
}
between(a, b) {
return this.addValidator((x, path) => {
switch (type(x)) {
case "Number":
if (x < a || b < x) {
throw new Error(`${path} should be between ${a} and ${b}`)
}
case "Array":
case "String":
if (x.length < a || b < x.length) {
throw new Error(`${path}.length should be between ${a} and ${b}`)
}
}
})
}
match(regexp) {
return this.addValidator((x, path) => {
if (!regexp.test(x)) {
throw new Error(`${path} should match ${regexp}`)
}
})
}
notMatch(regexp) {
return this.addValidator((x, path) => {
if (regexp.test(x)) {
throw new Error(`${path} should not match ${regexp}`)
}
})
}
validate(x, path) {
try {
this.validator.forEach(f => f(x, path || "It"))
this.mapper.forEach(f => x = f(x))
} catch (e) {
if (this.error.length > 0) {
this.error.forEach(f => x = f(e, x))
} else {
throw e
}
}
return x
}
parse(json) {
const obj = JSON.parse(json)
this.validate(obj)
return obj
}
}
module.exports = new Schemable()