mde/model — pull request #150 by franksrevenge (2014-01-08 13:29:54+00:00)
Title: Laxer validation for properties and beforeValidate() as filter

Assuming...

```
User = function() {

this.property( 'username', 'string', { required: true } );
this.property( 'password', 'string', { required: false } );

this.validatesFormat( 'username', /^[^a-z]$/ );
this.validatesFormat( 'password', /^[^a-z]$/ );
};
```

And:

```
var u = new User();

u.setProperties( { username: 'testuser' } );
u.save();
```

...Current validation system will fail, because undefined property `password` will not validate against the regular expression validator I have created. However, typical database design allows default values for fields not mentioned in the insert query, not to mention use of `null`.

Laxer validation checks if the property's `required` parameter has been explicitly been set to `false`. If that is the case, the validation test will not be run against the empty value, because we're signalling that empty/null is safe.

-------------------------------

Secondly, properties are passed to `beforeValidate()` callback as its first parameter. This is to allow `beforeValidate()` function to be used as a filter:

```
User.prototype.beforeValidate = function( params )
{
if( params.hasOwnProperty( 'username' ) === true )
{
params.username = params.username.toLowerCase();
}
};

var u = new User();

u.setProperties( { username: 'TESTuser' } );
u.save();
```

Currently, validation would fail, because User has a validation tester which allows only lowercase letters through. In many cases -- particularly with data received from users -- we could safely enforce data formatting rules so that we won't have to worry about the format of the data we received.