-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
164 lines (148 loc) · 4.04 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
var validate = require('validate.js'),
Promise = require('bluebird'),
merge = require('deepmerge'),
sanitize = require('./sanitize'),
utils = require('./utils');
validate.Promise = function(callback) {
return new Promise(callback);
};
module.exports = function(options) {
return function(record) {
var self = this;
this.results = deepClone(record);
this.validator = validate;
this.errors = null;
var customValidators = options.customValidators || {};
for(var validator in customValidators) {
if(customValidators.hasOwnProperty(validator) && typeof customValidators[validator] === 'function') {
this.validator.validators[validator] = customValidators[validator];
}
}
this.validate = function() {
if(typeof options.validate !== 'object') {
return self;
}
if(options.async) {
return new Promise(function(resolve, reject) {
validate.async(self.results, options.validate, options.valOptions || {})
.then(function() {
resolve(self.results);
}, function(err) {
self.errors = err;
reject(err);
});
});
} else {
self.errors = validate(self.results, options.validate, options.valOptions || {}) || null;
return self;
}
};
this.sanitize = function() {
if(options.sanitize) {
self.results = sanitize(self.results, options.sanitize || {}, options.customSanitizers || {});
}
if(options.async) {
return Promise.resolve(self.results);
} else {
return self;
}
};
this.whitelist = function() {
if(options.whitelist) {
self.results = whitelist(this.results, options.whitelist);
}
if(options.async) {
return Promise.resolve(self.results);
} else {
return self;
}
};
this.blacklist = function() {
if(options.blacklist) {
self.results = blacklist(self.results, options.blacklist);
}
if(options.async) {
return Promise.resolve(self.results);
} else {
return self;
}
};
this.rename = function() {
if(options.rename) {
self.results = rename(self.results, options.rename);
}
if(options.async) {
return Promise.resolve(self.results);
} else {
return self;
}
};
this.static = function() {
if(options.static) {
self.results = merge(self.results, options.static);
}
if(options.async) {
return Promise.resolve(self.results);
} else {
return self;
}
};
this.merge = function(doc) {
self.results = merge(doc, self.results);
if(options.async) {
return Promise.resolve(self.results);
} else {
return self;
}
};
this.process = function() {
if(options.async) {
return self.whitelist()
.then(self.blacklist)
.then(self.sanitize)
.then(self.validate)
.then(self.rename)
.then(self.static);
} else {
return self.whitelist()
.blacklist()
.sanitize()
.validate()
.rename()
.static();
}
};
};
};
var whitelist = function(doc, list) {
var results = {};
for(var item in list) {
var selected = utils.getObjectRef(doc, list[item]);
if(typeof selected !== 'undefined') {
utils.setObjectRef(results, list[item], selected);
}
}
return results;
};
var blacklist = function(doc, list) {
for(var item in list) {
var selected = utils.getObjectRef(doc, item);
if(typeof selected !== 'undefined') {
utils.delObjectRef(doc, item);
}
}
return doc;
};
var rename = function(doc, list) {
for(var item in list) {
var selected = utils.getObjectRef(doc, item);
if(typeof selected !== 'undefined') {
utils.setObjectRef(doc, list[item], selected);
utils.delObjectRef(doc, item);
}
}
return doc;
};
var deepClone = function(obj) {
return JSON.parse(JSON.stringify(obj));
};