-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
40 lines (32 loc) · 940 Bytes
/
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
module.exports = function(options, imports, register) {
var _ = require('underscore');
var sanitizer = require('validator').sanitize;
options = _.defaults(options, {sanitize: true});
var validate = function(data, reduce, done, sanitize) {
if (_.isUndefined(sanitize)) {
sanitize = options.sanitize;
}
var errors =
_.chain(data)
.map(function(value, key) {
if(sanitize) {
value = sanitizer(value).xss();
}
return {key: key, value: value};
})
.reduce(function(memo, input) {
try {
var result = reduce(input.key, input.value);
if (result == false) memo.push(input);
}
catch(e) {
if (!memo) memo=[];
memo.push(input);
}
return memo;
}, false)
.value();
done(errors);
}
register(null, {validator: validate});
}