-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.verify.js
72 lines (59 loc) · 1.54 KB
/
util.verify.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
'use strict';
module.exports.bool = function () {
return function (bool) {
return typeof bool === 'boolean';
};
};
module.exports.int = function () {
return function (num) {
return typeof num === 'number' && isFinite(num) && Math.round(num) === num;
};
};
module.exports.num = function () {
return function (num) {
return typeof num === 'number' && isFinite(num);
};
};
module.exports.str = function (re) {
return function (str) {
return typeof str === 'string' && str.length <= 32 && re.test(str);
};
};
module.exports.text = function () {
return function (str) {
return typeof str === 'string' && str.length <= 256;
};
};
module.exports.hash = function (len) {
return function (str) {
return typeof str === 'string' && str.length === len;
};
};
module.exports.arr = function (verifier) {
return function (arr) {
if (!(arr instanceof Array)) {
return false;
}
for (var i in arr) {
if (!verifier(arr[i])) {
return false;
}
}
return true;
};
};
module.exports.obj = function (keyVerifier, valueVerifier) {
// notice: use verified objects carefully
// {__proto__: 123, ...}
return function (obj) {
if (!(obj instanceof Object)) {
return false;
}
for (var i in obj) {
if (!keyVerifier(i) || !valueVerifier(obj[i])) {
return false;
}
}
return true;
};
};