mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-15 21:57:22 +01:00
allow for additional transform functions for toJSON and sanitize
This commit is contained in:
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user