A validation plugin for patio models. This plugin adds a validate method to each patio.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:[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:[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 patio.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;

Avaiable validation methods are.

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

The options include

Static Properties
PropertyTypeDefault ValueDescription
validateOnSavepropertytrue

Set to false to prevent model validation when saving.

validateOnUpdatepropertytrue

Set to false to prevent model validation when updating.

Instance Properties
PropertyTypeDefault ValueDescription
errorsObject{}

the validation errors for this model.

Constructor

Defined plugins/validation.js Source
function (){
   this._super(arguments);
   this.errors = {};
           
}
            

validate Static Function Public


Defined plugins/validation.js

Sets up validation for a model.

To do single col validation



var Model = patio.addModel("validator", {
     plugins:[ValidatorPlugin]
});
//this ensures column assignment
Model.validate("col1").isDefined().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:[ValidatorPlugin]
});
Model.validate(function(validate){
     //this ensures column assignment
     validate("col1").isDefined().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();
});

Arguments Returns Throws Source
function (name){
   this.__initValidation();
   var ret;
   if (isFunction(name)) {
       name.call(this, this.__getValidator.bind(this));
       ret = this;
   } else if (isString(name)) {
       ret = this.__getValidator(name);
   } else {
       throw new ModelError("name is must be a string or function when validating");
   }
   return ret;
           
}
    

Documentation generated using coddoc.