-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (134 loc) · 3.87 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
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
function params(func) {
var fnStr = func.toString().replace(STRIP_COMMENTS, '');
var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(/([^\s,]+)/g);
if(result === null)
result = [];
return result
}
var cache = {
equals: ['comparison'],
contains: ['elem'],
matches: ['pattern', 'modifiers'],
isEmail: [],
isURL: ['options'],
isIP: ['version'],
isAlpha: [],
isAlphanumeric: [],
isNumeric: [],
isHexadecimal: [],
isHexColor: [],
isLowercase: [],
isUppercase: [],
isInt: [],
isFloat: [],
isDivisibleBy: ['num'],
isNull: [],
isLength: ['min', 'max'],
isUUID: [],
isDate: [],
isAfter: ['date'],
isBefore: ['date'],
isIn: ['options'],
isCreditCard: [],
isISBN: ['version'],
isJSON: [],
ltrim: ['chars'],
rtrim: ['chars'],
trim: ['chars'],
escape: [],
whitelist: ['chars'],
blacklist: ['chars'],
toString: [],
toDate: [],
toFloat: [],
toInt: ['radix'],
toBoolean: ['strict']
};
module.exports = function(validator) {
if (!validator) {
validator = require('validator');
}
if (validator.sanitizer) {
return validator;
}
var extend = validator.extend;
function convert(schema) {
var calls = [];
for (var i = 0, l = schema.length; i < l; i++) {
var item = schema[i],
name = item['name'],
func = validator[name];
if (!cache.hasOwnProperty(name)) {
throw 'Invalid function name specified';
}
var keys = cache[name],
args = [],
options = item.options || {};
for (var j = 0; j < keys.length; j++) {
args.push(options[keys[j]]);
}
calls.push({
func: func,
funcName: name,
funcArgs: args,
schema: item
});
}
return calls;
}
validator.extend = function (name, fn) {
var args = params(fn);
args.shift();
cache[name] = args;
return extend.call(validator, name, fn);
};
validator.messages = {};
validator.formatMessage = function(func, value, message) {
if (!message) {
message = validator.messages[func] || '';
}
return message.replace(/\{\{value\}\}/g, value);
};
validator.validator = function(schema) {
if (!Array.isArray(schema)) {
throw 'Invalid schema';
}
var calls = convert(schema);
return {
validate: function(value) {
var errors = [];
for (var i = 0; i < calls.length; i++) {
var item = calls[i],
funcArgs = item['funcArgs'].slice();
funcArgs.unshift(value);
if (!item['func'].apply(validator, funcArgs)) {
errors.push(validator.formatMessage(item['funcName'], value, item['schema'].message));
}
}
if (errors.length == 0) {
return true;
}
return errors;
}
};
};
validator.sanitizer = function(schema) {
if (!Array.isArray(schema)) {
throw 'Invalid schema';
}
var calls = convert(schema);
return {
sanitize: function(value) {
for (var i = 0; i < calls.length; i++) {
var item = calls[i],
funcArgs = item['funcArgs'].slice();
funcArgs.unshift(value);
value = item['func'].apply(validator, funcArgs);
}
return value;
}
};
};
return validator;
};