balderdashy/waterline — pull request #482 by mphasize (2014-06-18 14:54:16+00:00)
Title: Make 'reserved keywords' in validations extendable

This PR relates to #444:

> I think the "ignored property keys" in waterline/core/validations should be exposed and extendable by an application developer. That would make it easier to write custom blueprints with specialized model definitions.

> Example: I am using an async keyword in my model attributes to define which associations I want to have populated by default and which should be excluded by default. My customized blueprints handle this attribute, but I have to modify waterline/core/validations to prevent it from choking on the async property.

Now I modified the waterline core a little bit, to make the default keywords in core/validations extendable through setting a configuration object in `sails.config.models`. More info below.

However, after doing all the work, I came back and read about the [custom types](https://github.com/balderdashy/waterline#custom-types) that waterline allows and now I am wondering just how different the use cases and possible benefits for each approach are.

Comments welcome!

------

### Configuration option

In `config/models` I am adding a configuration key "validations" like this:

// These properties should be ignored in Waterline validations
validations: {
ignoreProperties: [ 'async', 'special']
}

### Changes in waterline core
In `waterline/core/index.js` I make sure that the configuration gets passed down to the initializer function of the Validator:

this._validator.initialize(_validations, this.types, this.defaults.validations); // extend the initializer

After that I modified `waterline/core/validations.js` to accept the config and concatenate any keywords in `ignoreProperties` to the `reservedProperties`.

this.reservedProperties = ['defaultsTo', 'primaryKey', 'autoIncrement', 'unique', 'index', 'collection', 'dominant', 'through',
'columnName', 'foreignKey', 'references', 'on', 'groupKey', 'model', 'via', 'size',
'example', 'validationMessage', 'validations', 'populateSettings', 'onKey', 'protected'];

if(defaults.ignoreProperties && Array.isArray(defaults.ignoreProperties)) {
this.reservedProperties = this.reservedProperties.concat(defaults.ignoreProperties);
}

....

// If property is reserved don't do anything with it
if(self.reservedProperties.indexOf(prop) > -1) return;

### Test

Added a unit test for this behaviour to be found in `test/unit/validations/validations.ignoreProperties.js`.