Skip to content

Commit

Permalink
Sets up transform functions even if toObject and toJSON properties ar…
Browse files Browse the repository at this point in the history
…en't set
  • Loading branch information
romanmt committed Nov 6, 2013
1 parent 878ee5a commit f130113
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions lib/schema_methods.js
Expand Up @@ -62,29 +62,29 @@ module.exports = function(schema, instance) {
/**
* Transform methods for protected fields
*/
if(schema.options.toObject)
if(schema.options.toObject.protectedFields) {
schema.options.toObject.transform = function(doc, ret, options) {
if(typeof doc.ownerDocument !== 'function') {
if (doc.schema !== schema)
return doc.toObject();
options.protectedFields.split(' ').forEach(function(prop) {
delete ret[prop];
})
}
}
if (!schema.options.toObject) schema.options.toObject = {}
schema.options.toObject.transform = function(doc, ret, options) {
if(typeof doc.ownerDocument !== 'function') {
if (doc.schema !== schema)
return doc.toObject();
var fields = options.protectedFields
? options.protectedFields.split(' ') : []
fields.forEach(function(prop) {
delete ret[prop];
})
}
}

if(schema.options.toJSON)
if(schema.options.toJSON.protectedFields) {
schema.options.toJSON.transform = function(doc, ret, options) {
if(typeof doc.ownerDocument !== 'function') {
if (doc.schema !== schema)
return doc.toJSON();
options.protectedFields.split(' ').forEach(function(prop) {
delete ret[prop];
})
}
}
if (!schema.options.toJSON) schema.options.toJSON = {}
schema.options.toJSON.transform = function(doc, ret, options) {
if(typeof doc.ownerDocument !== 'function') {
if (doc.schema !== schema)
return doc.toJSON();
var fields = options.protectedFields
? options.protectedFields.split(' ') : []
fields.forEach(function(prop) {
delete ret[prop];
})
}
}
}

0 comments on commit f130113

Please sign in to comment.