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

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

Or you can do a mass validation through a callback.

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

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

  1. var model1 = new Model({col2 : 'grape', emailAddress : "test"}),
  2. model2 = new Model({col1 : "grape", col2 : "hello", emailAddress : "test@test.com"});
  3. model1.isValid() //false
  4. model2.isValid() //true

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

  1. model1.errors; //{ col1: [ 'col1 must be defined.' ],
  2. // col2: [ 'col2 must be like /hello/gi got grape.' ],
  3. // 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

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

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

  1. Model.validateOnSave = false;
  2. 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.