mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 06:37:23 +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 objToTransform = {ok: true, amPrivate: true};
|
||||||
let privatized = schema.options.toJSON.transform({}, objToTransform);
|
let privatized = schema.options.toJSON.transform({}, objToTransform);
|
||||||
|
|
||||||
expect(objToTransform).to.have.property('ok');
|
expect(privatized).to.have.property('ok');
|
||||||
expect(objToTransform).not.to.have.property('amPrivate');
|
expect(privatized).not.to.have.property('amPrivate');
|
||||||
expect(objToTransform.amPrivate).to.equal(undefined);
|
});
|
||||||
|
|
||||||
|
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);
|
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 = {};
|
if (!schema.options.toJSON) schema.options.toJSON = {};
|
||||||
@@ -49,5 +50,8 @@ export default function baseModel (schema, options = {}) {
|
|||||||
privateFields.forEach((fieldPath) => {
|
privateFields.forEach((fieldPath) => {
|
||||||
objectPath.del(plainObj, fieldPath);
|
objectPath.del(plainObj, fieldPath);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Allow an additional toJSON transform function to be used
|
||||||
|
return options.toJSONTransform ? options.toJSONTransform(plainObj) : plainObj;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -477,15 +477,7 @@ export let schema = new Schema({
|
|||||||
schema.plugin(baseModel, {
|
schema.plugin(baseModel, {
|
||||||
noSet: ['_id', 'apikey', 'auth.blocked', 'auth.timestamps', 'lastCron', 'auth.local.hashed_password', 'auth.local.salt'],
|
noSet: ['_id', 'apikey', 'auth.blocked', 'auth.timestamps', 'lastCron', 'auth.local.hashed_password', 'auth.local.salt'],
|
||||||
private: ['auth.local.hashed_password', 'auth.local.salt'],
|
private: ['auth.local.hashed_password', 'auth.local.salt'],
|
||||||
});
|
toJSONTransform: function toJSON (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;
|
doc.id = doc._id;
|
||||||
|
|
||||||
// FIXME? Is this a reference to `doc.filters` or just disabled code? Remove?
|
// FIXME? Is this a reference to `doc.filters` or just disabled code? Remove?
|
||||||
@@ -493,6 +485,11 @@ schema.methods.toJSON = function toJSON () {
|
|||||||
doc._tmp = this._tmp; // be sure to send down drop notifs
|
doc._tmp = this._tmp; // be sure to send down drop notifs
|
||||||
|
|
||||||
return doc;
|
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.virtual('tasks').get(function () {
|
// schema.virtual('tasks').get(function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user