-
Notifications
You must be signed in to change notification settings - Fork 1
/
validators.js
82 lines (63 loc) · 2 KB
/
validators.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
var checkProps = require('./check');
function objectValidator(obj) {
return typeof obj === 'object' && !(obj instanceof Array);
}
function functionValidator(obj) {
return typeof obj === 'function';
}
function arrayValidator(arr) {
return arr instanceof Array;
}
function booleanValidator(num) {
return typeof num === 'boolean';
}
function numberValidator(num) {
return typeof num === 'number';
}
function stringValidator(str) {
return typeof str === 'string';
}
function objectWithValidator(object, propName) {
var passed = false;
if (objectValidator(object) && this._rules && objectValidator(this._rules)) {
passed = checkProps(object, propName)(this._rules);
}
return passed;
}
function arrayOfValidator(array, propName) {
var passed = true,
withoutError = false;
if (arrayValidator(array) && this._rules && objectValidator(this._rules)) {
var arrayItem,
i = 0,
len = array.length;
if (len === 0) {
passed = false;
console.warn('PropTypes warning: array ' + propName + ' is empty');
}
for( ; i < len; i++) {
arrayItem = array[i];
if (!objectValidator(arrayItem)) {
passed = false;
console.warn('PropTypes warning: array ' + propName + ' contains non-object element!', arrayItem);
break;
}
withoutError = checkProps(arrayItem, propName)(this._rules);
if (!withoutError) {
console.warn('Check array item with index = ' + i, arrayItem);
passed = false;
}
}
}
return passed;
}
module.exports = {
objectValidator: objectValidator,
functionValidator: functionValidator,
arrayValidator: arrayValidator,
booleanValidator: booleanValidator,
numberValidator: numberValidator,
stringValidator: stringValidator,
objectWithValidator: objectWithValidator,
arrayOfValidator: arrayOfValidator,
};