forked from colinskow/sofa-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanitize.js
111 lines (103 loc) · 2.96 KB
/
sanitize.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
const validator = require('validator'),
utils = require('./utils');
const append = function (data, options) {
if (typeof data === 'undefined') {
return;
}
let str = '';
if (typeof options !== 'undefined') {
str = str + options;
}
return data + str;
};
const prepend = function (data, options) {
if (typeof data === 'undefined') {
return;
}
let str = '';
if (typeof options !== 'undefined') {
str = str + options;
}
return str + data;
};
const upperCase = function (data) {
return data.toString().toUpperCase();
};
const lowerCase = function (data) {
return data.toString().toLowerCase();
};
const titleCase = function (data) {
return data.toString().replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
};
const sanitizeFunctions = {
toString: validator.toString,
toDate: validator.toDate,
toFloat: validator.toFloat,
toInt: validator.toInt,
toBoolean: validator.toBoolean,
trim: validator.trim,
escape: validator.escape,
stripLow: validator.stripLow,
whiteList: validator.whitelist,
blackList: validator.blacklist,
normalizeEmail: validator.normalizeEmail,
append: append,
prepend: prepend,
toUpperCase: upperCase,
toLowerCase: lowerCase,
toTitleCase: titleCase
};
const sanitizeField = function (data, fn, options) {
if (typeof sanitizeFunctions[fn] === 'function') {
return sanitizeFunctions[fn].call(null, data, options);
} else {
throw new Error('Sanitize function ' + fn + " doesn't exist.");
}
};
module.exports = function (doc, settings, sanitizers) {
// Add any custom sanitize functions that are supplied
if (typeof sanitizers === 'object') {
for (const sanitizer in sanitizers) {
if (
Object.prototype.hasOwnProperty.call(sanitizers, sanitizer) &&
typeof sanitizers[sanitizer] === 'function'
) {
sanitizeFunctions[sanitizer] = sanitizers[sanitizer];
}
}
}
// Process each key listed in the sanitize options
for (const key in settings) {
if (Object.prototype.hasOwnProperty.call(settings, key)) {
const thisOp = settings[key];
let output = utils.getObjectRef(doc, key);
let opArray, func;
if (typeof output !== 'undefined') {
if (utils.isArray(thisOp)) {
opArray = thisOp;
} else {
const arr = [];
arr[0] = thisOp;
opArray = arr;
}
for (let x = 0; x < opArray.length; x++) {
if (typeof opArray[x] === 'string') {
func = opArray[x];
output = sanitizeField(output, func);
} else if (typeof opArray[x] === 'object') {
for (const op in opArray[x]) {
if (Object.prototype.hasOwnProperty.call(opArray[x], op)) {
func = op;
output = sanitizeField(output, func, opArray[x][op]);
}
}
}
}
utils.setObjectRef(doc, key, output);
}
}
}
return doc;
};