Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: LearnBoost/mongoose
...
head fork: olalonde/mongoose
  • 6 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 76 additions and 1 deletion.
  1. +52 −0 lib/document.js
  2. +24 −1 test/document.test.js
52 lib/document.js
View
@@ -1230,6 +1230,7 @@ Document.prototype._doQueue = function () {
* - `getters` apply all getters (path and virtual getters)
* - `virtuals` apply virtual getters (can override `getters` option)
* - `minimize` remove empty objects (defaults to true)
+ * - `hide` string of paths to hide (can override `getters` and `virtuals` options)
*
* Example of only applying path getters
*
@@ -1243,10 +1244,25 @@ Document.prototype._doQueue = function () {
*
* doc.toObject({ getters: true })
*
+ * Example of hiding paths
+ *
+ * doc.toObject({ hide: 'password salt' })
+ *
+ * Example of using hide with arrays
+ *
+ * // Assuming doc.users = [{username: '', password: '', salt: ''}, {username: '', password: '', salt: ''}, ...]
+ * // The following code will hide the password and salt field of all objects in the users array.
+ * doc.toObject({ hide: 'user.password user.salt' })
+ *
+ * Example of hiding nested paths
+ *
+ * doc.toObject({ hide: 'user.password user.salt' })
+ *
* To apply these options to every document of your schema by default, set your [schemas](#schema_Schema) `toObject` option to the same argument.
*
* schema.set('toObject', { virtuals: true })
*
+ *
* See [schema options](/docs/guide.html#toObject) for details.
*
* @param {Object} [options]
@@ -1275,10 +1291,46 @@ Document.prototype.toObject = function (options) {
applyGetters(this, ret, 'paths', options);
}
+ if (options.json && options.hide) {
+ options.hide.split(/\s+/).forEach(function (path) {
+ if (!path) return;
+ deletePath(ret, path);
+ });
+ }
return ret;
};
/*!
+ * Removes property identified by path from object obj.
+ *
+ * @param {Object} obj
+ * @param {String} path (can also be an {Array})
+ */
+
+function deletePath (obj, path) {
+ if (typeof obj === 'undefined') return;
+ if (typeof path === 'string')
+ path = path.split('.');
+
+ if (path.length > 1) {
+ var key = path.shift();
+ if (!obj[key]) return obj;
+ if (obj[key] instanceof Array) {
+ obj[key].forEach(function(value, index) {
+ deletePath(obj[key][index], path.slice(0));
+ });
+ }
+ else {
+ deletePath(obj[key], path);
+ }
+ }
+ else {
+ if (obj[path[0]]) delete obj[path[0]];
+ return;
+ }
+}
+
+/*!
* Applies virtuals properties to `json`.
*
* @param {Document} self
25 test/document.test.js
View
@@ -330,7 +330,20 @@ describe('document:', function(){
, cool : DocumentObjectId.fromString('4c6c2d6240ced95d0e00003c')
, path : 'my path'
}
+ , hiddenField1 : 'hidden data 1'
+ , hiddenField2 : 'hidden data 2'
, nested2: {}
+ , nested3 : { hideMe: 'nested hidden data', hideMeDeep: { amI: 'hidden?' } }
+ , nested4: [
+ { secret: 'shh', notSecret: 'ok la' }
+ , { secret: 'shh', notSecret: 'ok la2' }
+ , { secret: 'shh', notSecret: 'ok la3' }
+ , { secret: 'shh', notSecret: 'ok la4' }
+ ]
+ , nested5: [
+ { ok: 'not secret...', veryDeepSecret: { secret: 'shh', ok: 'ok!' } }
+ , { ok: 'not secret...', veryDeepSecret: { secret: 'shh', ok: 'ok!' } }
+ ]
});
// override to check if toJSON gets fired
@@ -339,8 +352,18 @@ describe('document:', function(){
return {};
}
- doc.schema.options.toJSON = { virtuals: true };
+ var pathsToHide = 'hiddenField1 hiddenField2 nested3.hideMe nested3.hideMeDeep.amI nested4.secret nested5.veryDeepSecret.secret';
+ doc.schema.options.toJSON = { virtuals: true, hide: pathsToHide };
var clone = doc.toJSON();
+ assert.equal(undefined, clone.hiddenField1);
+ assert.equal(undefined, clone.hiddenField2);
+ assert.equal(undefined, clone.nested3.hideMe);
+ clone.nested4.forEach(function(obj) {
+ assert.equal(undefined, obj.secret);
+ });
+ clone.nested5.forEach(function(obj) {
+ assert.equal(undefined, obj.veryDeepSecret.secret);
+ });
assert.equal('test', clone.test);
assert.ok(clone.oids instanceof Array);
assert.equal(5, clone.nested.age);

No commit comments for this range

Something went wrong with that request. Please try again.