forked from neonexus/sails-react-bootstrap-webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeep-models-safe.js
More file actions
42 lines (34 loc) · 1.17 KB
/
Copy pathkeep-models-safe.js
File metadata and controls
42 lines (34 loc) · 1.17 KB
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
module.exports = {
friendlyName: 'Keep Model Safe',
description: 'Enforce custom .toJSON() is called recursively.',
sync: true, // function is not async
inputs: {
data: {
type: 'ref',
required: true
}
},
exits: {
success: {}
},
fn: (inputs, exits) => {
const dataCopy = _.merge({}, inputs.data);
// force all objects to their JSON formats, if it has said function
// this prevents accidental leaking of sensitive data, by utilizing customToJSON on models
(function findTheJson(data) {
_.forEach(data, (val, key) => {
if (_.isObject(val)) {
return findTheJson(val);
}
if (val && val.toJSON && typeof val.toJSON === 'function') {
if (val.toDate && typeof val.toDate === 'function' && typeof val.format === 'function') {
data[key] = val.format();
} else {
data[key] = val.toJSON();
}
}
});
})(dataCopy);
return exits.success(dataCopy);
}
};