-
Notifications
You must be signed in to change notification settings - Fork 118
/
index.js
492 lines (427 loc) · 9.6 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/**
* parameter - index.js
*
* Copyright(c) node-modules and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com>
* dead_horse <dead_horse@qq.com>
*/
'use strict';
var util = require('util');
/**
* Module exports
* @type {Function}
*/
module.exports = validate;
validate.addRule = addRule;
validate.translate = null;
/**
* Regexps
*/
var DATE_TYPE_RE = /^\d{4}\-\d{2}\-\d{2}$/;
var DATETIME_TYPE_RE = /^\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}$/;
var ID_RE = /^\d+$/;
// http://www.regular-expressions.info/email.html
var EMAIL_RE = /^[a-z0-9\!\#\$\%\&\'\*\+\/\=\?\^\_\`\{\|\}\~\-]+(?:\.[a-z0-9\!\#\$\%\&\'\*\+\/\=\?\^\_\`\{\|\}\~\-]+)*@(?:[a-z0-9](?:[a-z0-9\-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9\-]*[a-z0-9])?$/;
var PASSWORD_RE = /^[\w\`\~\!\@\#\$\%\^\&\*\(\)\-\_\=\+\[\]\{\}\|\;\:\'\"\,\<\.\>\/\?]+$/;
// https://gist.github.com/dperini/729294
var URL_RE = /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/i;
/**
* Simple type map
* @type {Object}
*/
var TYPE_MAP = validate.TYPE_MAP = {
number: checkNumber,
int: checkInt,
integer: checkInt,
string: checkString,
id: checkId,
date: checkDate,
dateTime: checkDateTime,
datetime: checkDateTime,
boolean: checkBoolean,
bool: checkBoolean,
array: checkArray,
object: checkObject,
enum: checkEnum,
email: checkEmail,
password: checkPassword,
url: checkUrl,
};
/**
* validate
*
* @param {Object} rules
* @return {Object} obj
* @api public
*/
function validate(rules, obj) {
if (typeof rules !== 'object') {
throw new TypeError('need object type rule');
}
var errors = [];
for (var key in rules) {
var rule = formatRule(rules[key]);
var has = obj.hasOwnProperty(key);
if (!has) {
if (rule.required !== false) {
errors.push({
message: t('required'),
field: key,
code: t('missing_field')
});
}
continue;
}
var checker = TYPE_MAP[rule.type];
if (!checker) {
throw new TypeError('rule type must be one of ' + Object.keys(TYPE_MAP).join(', ') +
', but the following type was passed: ' + rule.type);
}
var msg = checker(rule, obj[key], obj);
if (typeof msg === 'string') {
errors.push({
message: t(msg),
code: t('invalid'),
field: key
});
}
if (Array.isArray(msg)) {
msg.forEach(function (e) {
var dot = rule.type === 'object' ? '.' : '';
e.message = t(e.message);
e.field = key + dot + e.field;
errors.push(e);
});
}
}
if (errors.length) {
return errors;
}
}
/**
* add custom rule
*
* @param {String} type
* @param {Function | RegExp} check
* @api public
*/
function addRule(type, check) {
if (!type) {
throw new TypeError('`type` required');
}
if (typeof check === 'function') {
TYPE_MAP[type] = check;
return;
}
if (check instanceof RegExp) {
TYPE_MAP[type] = function (rule, value) {
return checkString({format: check}, value);
};
return;
}
throw new TypeError('check must be function or regexp');
}
/**
* format a rule
*
* @param {Mixed} rule
* @return {Object}
* @api private
*/
function formatRule(rule) {
if (typeof rule === 'string') {
return { type: rule };
}
if (Array.isArray(rule)) {
return { type: 'enum', values: rule };
}
if (rule instanceof RegExp) {
return { type: 'string', format: rule };
}
return rule || {};
}
/**
* check interger
* {
* max: 100,
* min: 0
* }
*
* @param {Object} rule
* @param {Mixed} value
* @return {Boolean}
* @api private
*/
function checkInt(rule, value) {
if (typeof value !== 'number' || value % 1 !== 0) {
return t('should be an integer');
}
if (rule.hasOwnProperty('max') && value > rule.max) {
return t('should smaller than %s', rule.max);
}
if (rule.hasOwnProperty('min') && value < rule.min) {
return t('should bigger than %s', rule.min);
}
}
/**
* check number
* {
* max: 100,
* min: 0
* }
*
* @param {Object} rule
* @param {Mixed} value
* @return {Boolean}
* @api private
*/
function checkNumber(rule, value) {
if (typeof value !== 'number') {
return t('should be a number');
}
if (rule.hasOwnProperty('max') && value > rule.max) {
return t('should smaller than %s', rule.max);
}
if (rule.hasOwnProperty('min') && value < rule.min) {
return t('should bigger than %s', rule.min);
}
}
/**
* check string
* {
* allowEmpty: true, // (default to false, alias to empty)
* format: /^\d+$/,
* max: 100,
* min: 0
* }
*
* @param {Object} rule
* @param {Mixed} value
* @return {Boolean}
* @api private
*/
function checkString(rule, value) {
if (typeof value !== 'string') {
return t('should be a string');
}
var allowEmpty = rule.hasOwnProperty('allowEmpty')
? rule.allowEmpty
: rule.empty;
if (rule.hasOwnProperty('max') && value.length > rule.max) {
return t('length should smaller than %s', rule.max);
}
if (rule.hasOwnProperty('min') && value.length < rule.min) {
return t('length should bigger than %s', rule.min);
}
if (!allowEmpty && value === '') {
return t('should not be empty');
}
if (rule.format && !rule.format.test(value)) {
return rule.message || t('should match %s', rule.format);
}
}
/**
* check id format
* format: /^\d+/
*
* @param {Object} rule
* @param {Mixed} value
* @return {Boolean}
* @api private
*/
function checkId(rule, value) {
return checkString({format: ID_RE}, value);
}
/**
* check date format
* format: YYYY-MM-DD
*
* @param {Object} rule
* @param {Mixed} value
* @return {Boolean}
* @api private
*/
function checkDate(rule, value) {
return checkString({format: DATE_TYPE_RE}, value);
}
/**
* check date time format
* format: YYYY-MM-DD HH:mm:ss
*
* @param {Object} rule
* @param {Mixed} value
* @return {Boolean}
* @api private
*/
function checkDateTime(rule, value) {
return checkString({format: DATETIME_TYPE_RE}, value);
}
/**
* check boolean
*
* @param {Object} rule
* @param {Mixed} value
* @return {Boolean}
* @api private
*/
function checkBoolean(rule, value) {
if (typeof value !== 'boolean') {
return t('should be a boolean');
}
}
/**
* check enum
* {
* values: [0, 1, 2]
* }
*
* @param {Object} rule
* @param {Mixed} value
* @return {Boolean}
* @api private
*/
function checkEnum(rule, value) {
if (!Array.isArray(rule.values)) {
throw new TypeError('check enum need array type values');
}
if (rule.values.indexOf(value) === -1) {
return t('should be one of %s', rule.values.join(', '));
}
}
/**
* check email
*
* @param {Object} rule
* @param {Mixed} value
* @return {Boolean}
* @api private
*/
function checkEmail(rule, value) {
return checkString({
format: EMAIL_RE,
message: rule.message || 'should be an email'
}, value);
}
/**
* check password
* @param {Object} rule
* @param {Object} value
* @param {Object} obj
* @return {Boolean}
*
* @api private
*/
function checkPassword(rule, value, obj) {
if (!rule.min) {
rule.min = 6;
}
rule.format = PASSWORD_RE;
var error = checkString(rule, value);
if (error) {
return error;
}
if (rule.compare && obj[rule.compare] !== value) {
return t('should equal to %s', rule.compare);
}
}
/**
* check url
*
* @param {Object} rule
* @param {Object} value
* @return {Boolean}
* @api private
*/
function checkUrl(rule, value) {
return checkString({
format: URL_RE,
message: rule.message || 'should be a url'
}, value);
}
/**
* check object
* {
* rule: {}
* }
*
* @param {Object} rule
* @param {Mixed} value
* @return {Boolean}
* @api private
*/
function checkObject(rule, value) {
if (typeof value !== 'object') {
return t('should be an object');
}
if (rule.rule) {
return validate(rule.rule, value);
}
}
/**
* check array
* {
* type: 'array',
* itemType: 'string'
* rule: {type: 'string', allowEmpty: true}
* }
*
* {
* type: 'array'.
* itemType: 'object',
* rule: {
* name: 'string'
* }
* }
*
* @param {Object} rule
* @param {Mixed} value
* @return {Boolean}
* @api private
*/
function checkArray(rule, value) {
if (!Array.isArray(value)) {
return t('should be an array');
}
if (!rule.itemType) {
return;
}
var checker = TYPE_MAP[rule.itemType];
if (!checker) {
throw new TypeError('rule type must be one of ' + Object.keys(TYPE_MAP).join(', ') +
', but the following type was passed: ' + rule.itemType);
}
var errors = [];
var subRule = rule.itemType === 'object'
? rule
: rule.rule || formatRule(rule.itemType);
value.forEach(function (v, i) {
var index = '[' + i + ']';
var errs = checker(subRule, v);
if (typeof errs === 'string') {
errors.push({
field: index,
message: errs,
code: t('invalid')
});
}
if (Array.isArray(errs)) {
errors = errors.concat(errs.map(function (e) {
e.field = index + '.' + e.field;
e.message = e.message;
return e;
}));
}
});
return errors;
}
function t() {
var args = Array.prototype.slice.call(arguments);
if (typeof validate.translate === 'function') {
return validate.translate.apply(validate, args);
} else {
return util.format.apply(util, args);
}
}