-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (49 loc) · 1.88 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
var _ = require('underscore');
exports = module.exports = function mongooseFilterProperties(schema, opts) {
schema.static('filter', function (properties, filterType, cb) {
filterProperties(properties, filterType, function (err, properties) {
return cb(err, properties);
});
});
schema.method('filter', function (filterType, cb) {
var properties = this;
filterProperties(properties, filterType, function (err, properties) {
return cb(err, properties);
});
});
function filterProperties(properties, filterType, cb) {
// Ensure filterType is present. Should be 'w', 'r', 'readable', or
// 'writeable'. Otherwise an error will be thrown.
if (!filterType) { return cb(null, properties); }
if (filterType === 'w') { filterType = 'writeable'; }
if (filterType === 'r') { filterType = 'readable'; }
if (filterType !== 'readable' && filterType !== 'writeable') {
return cb(null, properties);
}
// Ensure 'properties' is an object. Otherwise, an error will be thrown.
// This is for cases where the method is called as a static.
if (typeof properties !== 'object') {
throw new Error("The first parameter must be an object.");
}
var whitelist = _.filter(Object.keys(schema.tree), function (pathName) {
if (typeof schema.tree[pathName][filterType] === 'undefined') {
return true;
} else {
if (schema.tree[pathName][filterType] === false) {
return false;
} else {
return true;
}
}
});
// Default behavior is a whitelist; properties without a filterType
// attribute are passed through. I'll add a strict mode to the schema
// options at some point.
var newProps = {};
for (var i = 0; i < whitelist.length; i++) {
var prop = whitelist[i];
newProps[prop] = properties[prop];
}
return cb(null, newProps);
}
};