# 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

```javascript
// 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);
```

```javascript
// 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,
}
*/
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sebloix.gitbook.io/gstore-node/v5.x+v6.x/model/methods/sanitize.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
