diff --git a/src/models/plugins/toJSON.plugin.js b/src/models/plugins/toJSON.plugin.js index 12af055f..d2cd22e2 100644 --- a/src/models/plugins/toJSON.plugin.js +++ b/src/models/plugins/toJSON.plugin.js @@ -6,7 +6,6 @@ * - replaces _id with id */ - const deleteAtPath = (obj, path, index) => { if (index === path.length - 1) { delete obj[path[index]]; diff --git a/tests/unit/models/plugins/toJSON.plugin.test.js b/tests/unit/models/plugins/toJSON.plugin.test.js index 7e7fb92d..7931828d 100644 --- a/tests/unit/models/plugins/toJSON.plugin.test.js +++ b/tests/unit/models/plugins/toJSON.plugin.test.js @@ -46,6 +46,25 @@ describe('toJSON plugin', () => { expect(doc.toJSON()).toHaveProperty('public'); }); + it('should remove any nested paths set as private', () => { + const schema = mongoose.Schema({ + public: { type: String }, + nested: { + private: { type: String, private: true }, + }, + }); + schema.plugin(toJSON); + const Model = connection.model('Model', schema); + const doc = new Model({ + public: 'some public value', + nested: { + private: 'some nested private value', + }, + }); + expect(doc.toJSON()).not.toHaveProperty('nested.private'); + expect(doc.toJSON()).toHaveProperty('public'); + }); + it('should also call the schema toJSON transform function', () => { const schema = mongoose.Schema( {