patio.plugins.ValidatorPlugin

A validation plugin for patio models. This plugin adds a validate method to each Model class that adds it as a plugin. This plugin does not include most typecast checks as patio already checks types upon column assignment.

To do single col validation

var Model = patio.addModel("validator", {                                                                                                                  
     plugins:[patio.plugins.ValidatorPlugin]
});                                                                                                                                                        
//this ensures column assignment                                                                                                                           
Model.validate("col1").isNotNull().isAlphaNumeric().hasLength(1, 10);                                                                                      
//col2 does not have to be assigned but if it is it must match /hello/ig.                                                                                  
Model.validate("col2").like(/hello/ig);                                                                                                                    
//Ensures that the emailAddress column is a valid email address.                                                                                           
Model.validate("emailAddress").isEmailAddress();

Or you can do a mass validation through a callback.

var Model = patio.addModel("validator", {                                                                                                                  
     plugins:[patio.plugins.ValidatorPlugin]
});                                                                                                                                                        
Model.validate(function(validate){                                                                                                                         
     //this ensures column assignment                                                                                                                      
     validate("col1").isNotNull().isAlphaNumeric().hasLength(1, 10);                                                                                       
     //col2 does not have to be assigned but if it is it must match /hello/ig.                                                                             
     validate("col2").isLike(/hello/ig);                                                                                                                   
     //Ensures that the emailAddress column is a valid email address.                                                                                      
     validate("emailAddress").isEmail();                                                                                                                   
});

To check if a Model is valid you can run the isValid method.

var model1 = new Model({col2 : 'grape', emailAddress : "test"}),                                                                                           
    model2 = new Model({col1 : "grape", col2 : "hello", emailAddress : "test@test.com"});                                                                  

model1.isValid() //false                                                                                                                                   
model2.isValid() //true

To get the errors associated with an invalid model you can access the errors property

model1.errors; //{ col1: [ 'col1 must be defined.' ],                                                                                                      
               //  col2: [ 'col2 must be like /hello/gi got grape.' ],                                                                                     
               //  emailAddress: [ 'emailAddress must be a valid Email Address got test' ] }

Validation is also run pre save and pre update. To prevent this you can specify the validate option

model1.save(null, {validate : false});                                                                                                                     
model2.save(null, {validate : false});

Or you can specify the class level properties validateOnSave and validateOnUpdate to false respectively

Model.validateOnSave = false;                                                                                                                              
Model.validateOnUpdate = false;

Available validation methods are.

All of the validation methods are chainable, and accept an options argument.

The options include


Documentation generated using coddoc.