Skip to content

Commit

Permalink
Merge pull request #91 from alejom99/validation
Browse files Browse the repository at this point in the history
Validation documentation and unit tests
  • Loading branch information
joeferner committed Sep 19, 2013
2 parents bc49fa2 + def99bf commit 602ae9a
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
49 changes: 49 additions & 0 deletions README.md
Expand Up @@ -75,6 +75,7 @@ You can install using Node Package Manager (npm):
* [defineClause](#modelDefineClause)
* [onSave](#modelOnSave)
* [onLoad](#modelOnLoad)
* [validate](#modelValidate)
* [Associated Object Properties](#associatedObjectProperties)
* [Model events](#modelEvents)

Expand Down Expand Up @@ -658,6 +659,54 @@ Person.onLoad = function(obj, connection, callback) {
callback();
};
```
<a name="modelValidate" />
### Model.validate(obj, callback, connection)

Model validation is loosely implemented. Instead, it's left to the developers to integrate any valiation library that fits their needs.
If present this function will be called during a save or update operation.

__Arguments__

* obj - The model object
* connection - The connection persist is currently using to do the save or update
* callback(success, message) - The callback to be called when the validate is complete.
* success - False if validation failed, True otherwise
* message - A string containing an error message, or a custom-defined object containing vaidation information

__Example__
```javascript
Person = persist.define("Person", {
"name": type.STRING,
"age": type.INTEGER
};

//Single message validation
Person.validate = function (obj, connection, callback) {
if (obj.name === 'bad name') {
return callback(false, 'You had a bad name');
}
return callback(true);
};

//Multiple message validation
Person.validate = function (obj, connection, callback) {
var errors = [];

if (obj.name === 'bad name') {
errors.push({name:'You had a bad name'});
}

if (obj.age < 0) {
errors.push({age:'Age must be greater than 0'});
}

if(errors.length > 0) {
return callback(false, errors);
}

return callback(true);
};
```
<a name="associatedObjectProperties" />
### Associated Object Properties
Expand Down
55 changes: 53 additions & 2 deletions test/insert.js
Expand Up @@ -8,6 +8,13 @@ exports['Insert'] = nodeunit.testCase({
setUp: function (callback) {
var self = this;

this.Address = persist.define("Address", {
"address": type.STRING,
"city": type.STRING,
"state": type.STRING,
"zip": type.STRING
});

this.Phone = persist.define("Phone", {
"number": { type: type.STRING, dbColumnName: 'numbr' }
});
Expand All @@ -27,6 +34,7 @@ exports['Insert'] = nodeunit.testCase({
"lastUpdated": { type: persist.DateTime }
})
.hasMany(this.Phone)
.hasMany(this.Address)
.on('beforeSave', function (obj) {
obj.lastUpdated = self.testDate2;
})
Expand All @@ -37,12 +45,39 @@ exports['Insert'] = nodeunit.testCase({
obj.updateCount++;
});

this.Person.validate = function (obj, callback) {
this.Person.validate = function (obj, connection, callback) {
if (obj.name === 'bad name') {
return callback(false, 'You had a bad name');
}
return callback(true);
};

this.Address.validate = function(obj, connection, callback) {
var errors = [];

if (obj.address === 'bad address') {
errors.push({address: 'You had a bad address'});
}

if (obj.city === '') {
errors.push({city: 'City cannot be blank'});
}

if (obj.state === 'ABC') {
errors.push({state: 'State is invalid'});
}

var zipCodePattern = /^\d{5}$|^\d{5}-\d{4}$/;
if (!zipCodePattern.test(obj.zip)) {
errors.push({zip: 'Invalid zip code'});
}

if (errors.length > 0) {
return callback(false, errors);
}

return callback(true);
};

testUtils.connect(persist, {}, function (err, connection) {
self.connection = connection;
Expand Down Expand Up @@ -161,14 +196,30 @@ exports['Insert'] = nodeunit.testCase({
});
},

"validation": function (test) {
"validation with single message": function (test) {
var self = this;
var person1 = new this.Person({ name: "bad name", age: 0 });

person1.save(self.connection, function (err, p) {
test.equals('Validation failed: You had a bad name', err.message);
test.done();
});
},

"validation with custom object": function (test) {
var self = this;
var address1 = new this.Address({address: 'bad address',
city: '',
state: 'ABC',
zip: 'abcde'});

address1.save(self.connection, function(err, p) {
test.equals('You had a bad address', err[0].address);
test.equals('City cannot be blank', err[1].city);
test.equals('State is invalid', err[2].state);
test.equals('Invalid zip code', err[3].zip);
test.done();
});
}

});

0 comments on commit 602ae9a

Please sign in to comment.