-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
256 lines (206 loc) · 5.4 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
module.exports = function(object, schema) {
return validate(object, schema);
};
// http://stackoverflow.com/questions/7065120/calling-a-javascript-function-recursively
var validate = function recursive(object, schema) {
var rules = schema._rules;
if (typeof rules === 'object') {
if (typeof rules.required === 'boolean') { // run required validator first if specified
var err1 = validators['required'](object, rules['required']);
if (err1)
return err1;
}
else {
if (typeof object === 'undefined') // skip any validation for this object since its undefined and not required
return null;
}
if (typeof rules.null === 'boolean') { // run null validator second if specified
var err2 = validators['null'](object, rules['null']);
if (err2)
return err2;
}
for (var rule in rules) {
if (rule == 'required' || rule == 'null')
continue;
var validator = validators[rule];
if (typeof validator === 'undefined')
throw new Error('validator for rule "' + rule + '" does not exist');
var err3 = validator(object, rules[rule]);
if (err3) {
return err3; // return the error and don't drill down any deeper
}
}
}
var error = null;
for (var prop in schema) {
if (prop == '_rules')
continue;
var subobject = object[prop];
var subschema = schema[prop];
if (typeof subschema !== 'object')
throw new TypeError('non-object for property in schema');
var subErr = recursive(subobject, subschema);
if (subErr) {
if (error == null)
error = {};
error[prop] = subErr;
}
}
return error;
};
var validateRequired = function(val, ruleVal) {
return (ruleVal === true && typeof val === 'undefined' ? 'Required' : null);
};
var validateNull = function(val, ruleVal) {
return (ruleVal === false && val == null ? 'Null' : null);
};
var validateType = function(val, ruleVal) {
if (val == null)
return null;
var errorMsg = 'Invalid type';
switch (ruleVal) {
case 'array':
if (!Array.isArray(val))
return errorMsg;
break;
case 'boolean':
if (typeof val != 'boolean')
return errorMsg;
break;
case 'date':
// http://stackoverflow.com/questions/643782/how-to-check-whether-an-object-is-a-date
if (Object.prototype.toString.call(val) !== '[object Date]')
return errorMsg;
break;
case 'dateString':
if (isNaN(Date.parse(val)))
return errorMsg;
break;
case 'jsonString':
try {
JSON.parse(val);
}
catch(e) {
return errorMsg;
}
break;
case 'number':
if (typeof val != 'number')
return errorMsg;
break;
case 'numberString':
if (isNaN(val))
return errorMsg;
break;
case 'object':
if (typeof val != 'object')
return errorMsg;
break;
case 'string':
if (typeof val != 'string')
return errorMsg;
break;
default:
throw new Error('unsupported type');
}
return null;
};
var validateArrayType = function(val, ruleVal) {
if (val == null)
return null;
for (var i=0; i < val.length; i++) {
var invalidElementType = validateType(val[i], ruleVal);
if (invalidElementType)
return 'Invalid element type';
}
return null;
};
var validateEq = function(val, ruleVal) {
if (val == null)
return null;
return (val !== ruleVal ? 'Not equal' : null);
};
var validateMin = function(val, ruleVal) {
if (val == null)
return null;
return (val < ruleVal ? 'Below minimum' : null);
};
var validateMax = function(val, ruleVal) {
if (val == null)
return null;
return (val > ruleVal ? 'Above maximum' : null);
};
var validateMinLength = function(val, ruleVal) {
if (val == null)
return null;
return (val.length < ruleVal ? 'Below minimum length' : null);
};
var validateMaxLength = function(val, ruleVal) {
if (val == null)
return null;
return (val.length > ruleVal ? 'Above maximum length' : null);
};
// Support array and non-array types
var validateEnum = function(val, ruleVal) {
if (val == null)
return null;
if (validateType(val, 'array') == null) {
for (var i=0; i < val.length; i++) {
if (ruleVal.indexOf(val[i]) < 0)
return 'Not in enumeration';
}
}
else {
if (ruleVal.indexOf(val) < 0)
return 'Not in enumeration';
}
return null;
};
var validateRegex = function(val, ruleVal) {
if (val == null)
return null;
if (validateType(val, 'array') == null) {
for (var i=0; i < val.length; i++) {
if (!ruleVal.test(val[i]))
return 'Regex mismatch';
}
return null;
}
else {
return (!ruleVal.test(val) ? 'Regex mismatch' : null);
}
};
var validators = {
required: validateRequired,
null: validateNull,
type: validateType,
arrayType: validateArrayType,
eq: validateEq,
min: validateMin,
max: validateMax,
minLength: validateMinLength,
maxLength: validateMaxLength,
enum: validateEnum,
regex: validateRegex
};
/**
* Add custom validator
* @param rule - Name of the rule to be used in _rule in schema. Cannot be 'required' or 'null'
* @param fn - Validator function. Must have signature (val, ruleVal)
*/
module.exports.add = function(rule, fn) {
if (rule === 'required' || rule === 'null')
throw new Error('argument rule must not be required or null');
if (typeof fn !== 'function')
throw new TypeError('argument fn must be a function');
validators[rule] = fn;
};
/**
* Remove a validator
* @param rule - Cannot be 'required' or 'null'
*/
module.exports.remove = function(rule) {
if (rule === 'required' || rule === 'null')
throw new Error('argument rule must not be required or null');
delete validators[rule];
};