From d67836ee1fa77ce6c76f3ba9e0b44aa9558a441e Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Wed, 25 Nov 2015 15:42:29 +0100 Subject: [PATCH] allow for additional transform functions for toJSON and sanitize --- test/api/v3/unit/libs/baseModel.test.js | 36 ++++++++++++++++++++++--- website/src/libs/api-v3/baseModel.js | 6 ++++- website/src/models/user.js | 21 +++++++-------- 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/test/api/v3/unit/libs/baseModel.test.js b/test/api/v3/unit/libs/baseModel.test.js index 5c9f2a95dc..3b50a7f4f5 100644 --- a/test/api/v3/unit/libs/baseModel.test.js +++ b/test/api/v3/unit/libs/baseModel.test.js @@ -52,8 +52,38 @@ describe('Base model plugin', () => { let objToTransform = {ok: true, amPrivate: true}; let privatized = schema.options.toJSON.transform({}, objToTransform); - expect(objToTransform).to.have.property('ok'); - expect(objToTransform).not.to.have.property('amPrivate'); - expect(objToTransform.amPrivate).to.equal(undefined); + expect(privatized).to.have.property('ok'); + expect(privatized).not.to.have.property('amPrivate'); + }); + + it('accepts a further transform function for toJSON', () => { + let options = { + private: ['amPrivate'], + toJSONTransform: sandbox.stub().returns(true) + }; + + baseModel(schema, options); + + let objToTransform = {ok: true, amPrivate: true}; + let privatized = schema.options.toJSON.transform({}, objToTransform); + + expect(privatized).to.equals(true); + expect(options.toJSONTransform).to.be.calledWith(objToTransform); + }); + + it('accepts a transform function for sanitize', () => { + let options = { + private: ['amPrivate'], + sanitizeTransform: sandbox.stub().returns(true) + }; + + baseModel(schema, options); + + expect(schema.options.toJSON.transform).to.exist; + let objToSanitize = {ok: true, noUpdateForMe: true}; + let sanitized = schema.statics.sanitize(objToSanitize); + + expect(sanitized).to.equals(true); + expect(options.sanitizeTransform).to.be.calledWith(objToSanitize); }); }); diff --git a/website/src/libs/api-v3/baseModel.js b/website/src/libs/api-v3/baseModel.js index ebff377ff2..2de770226b 100644 --- a/website/src/libs/api-v3/baseModel.js +++ b/website/src/libs/api-v3/baseModel.js @@ -40,7 +40,8 @@ export default function baseModel (schema, options = {}) { objectPath.del(objToSanitize, fieldPath); }); - return objToSanitize; + // Allow a sanitize transform function to be used + return options.sanitizeTransform ? options.sanitizeTransform(objToSanitize) : objToSanitize; }; if (!schema.options.toJSON) schema.options.toJSON = {}; @@ -49,5 +50,8 @@ export default function baseModel (schema, options = {}) { privateFields.forEach((fieldPath) => { objectPath.del(plainObj, fieldPath); }); + + // Allow an additional toJSON transform function to be used + return options.toJSONTransform ? options.toJSONTransform(plainObj) : plainObj; }; } diff --git a/website/src/models/user.js b/website/src/models/user.js index 63eea0c504..e5d9e63c84 100644 --- a/website/src/models/user.js +++ b/website/src/models/user.js @@ -477,24 +477,21 @@ export let schema = new Schema({ schema.plugin(baseModel, { noSet: ['_id', 'apikey', 'auth.blocked', 'auth.timestamps', 'lastCron', 'auth.local.hashed_password', 'auth.local.salt'], private: ['auth.local.hashed_password', 'auth.local.salt'], + toJSONTransform: function toJSON (doc) { + doc.id = doc._id; + + // FIXME? Is this a reference to `doc.filters` or just disabled code? Remove? + doc.filters = {}; + doc._tmp = this._tmp; // be sure to send down drop notifs + + return doc; + }, }); schema.methods.deleteTask = function deleteTask (tid) { this.ops.deleteTask({params: {id: tid}}, () => {}); // TODO remove this whole method, since it just proxies, and change all references to this method }; -schema.methods.toJSON = function toJSON () { - let doc = this.toObject(); - - doc.id = doc._id; - - // FIXME? Is this a reference to `doc.filters` or just disabled code? Remove? - doc.filters = {}; - doc._tmp = this._tmp; // be sure to send down drop notifs - - return doc; -}; - // schema.virtual('tasks').get(function () { // var tasks = this.habits.concat(this.dailys).concat(this.todos).concat(this.rewards); // var tasks = _.object(_.pluck(tasks,'id'), tasks);