This methods will clean and do basic formatting of an entity data. You should always execute it on data coming from an untrusted source. MyModel.sanitize() will:
remove properties that are marked as not writable in schemas
convert 'null' (string) values to null
convert entity refs that have been populated back to their Datastore Key
// user.model.jsconstuserSchema=newSchema({ name : { type: String }, createdOn : { type: Date, write:false } // write is not allowed});// or with a Joi schemaconstschema=newSchema({ name: { joi:Joi.string() }, createdOn: { joi:Joi.date().strip() } // strip() will remove the property when Sanitizing}, { joi:true });module.exports=gstore.model('User', userSchema);
// user.controller.jsconstUser=require('./user.model');let data =req.body;console.log(data);/*{ createdOn: '2016-03-01T21:30:00', name: 'null', propertyNotDefined: 'abcdef',}*/// Now sanitize the body requestdata =User.sanitize(req.body);console.log(data);/*{ name: null,}*/