gstore-node
v8.x
v8.x
  • Introduction
  • Getting Started
    • Motivation
    • Installation
    • Create a gstore instance
  • Schema
    • About
    • Type validation
    • Value validation
    • Additional properties settings
    • Schema options
    • Joi Schema
    • Methods
      • path()
      • virtual()
    • Custom methods
  • Model
    • Creation
    • Methods
      • GET
      • UPDATE
      • DELETE
      • excludeFromIndexes()
      • key()
      • sanitize()
      • clearCache()
  • Entity
    • Creation
    • Properties
    • Methods
      • SAVE
      • plain()
      • populate()
      • model()
      • datastoreEntity()
      • validate()
  • Queries
    • @google-cloud Query
    • list()
    • findOne()
    • deleteAll()
    • findAround()
  • Populate
  • Middleware (hooks)
    • About
    • Pre hooks
    • Post hooks
  • Cache / Dataloader
    • Dataloader
    • Cache
  • gstore Methods
    • save()
  • Typescript
  • Appendix
    • Error Codes
    • Credits
Powered by GitBook
On this page

Was this helpful?

  1. Model
  2. Methods

sanitize()

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.js

const userSchema = new Schema({
    name : { type: String },
    createdOn : { type: Date, write: false } // write is not allowed
});

// or with a Joi schema
const schema = new Schema({
    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.js
const User = require('./user.model');

let data = req.body;
console.log(data);
/*
{
    createdOn: '2016-03-01T21:30:00',
    name: 'null',
    propertyNotDefined: 'abcdef',
}
*/

// Now sanitize the body request
data = User.sanitize(req.body);
console.log(data);

/*
{
    name: null,
}
*/
Previouskey()NextclearCache()

Last updated 6 years ago

Was this helpful?