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.
isAfter
: check that a date is after a specified dateisBefore
: check that a date is after before a specified date isDefined
: ensure that a column is definedisNotDefined
: ensure that a column is not definedisNotNull
: ensure that a column is defined and not nullisNull
: ensure that a column is not defined or nullisEq
: ensure that a column equals a value this uses deep equalisNeq
: ensure that a column does not equal a value this uses deep equalisLike
: ensure that a column is like a value, can be a regexp or stringisNotLike
: ensure that a column is not like a value, can be a regexp or stringisLt
: ensure that a column is less than a valueisGt
: ensure that a column is greater than a valueisLte
: ensure that a column is less than or equal to a valueisGte
: ensure that a column is greater than or equal to a valueisIn
: ensure that a column is contained in an array of valuesisNotIn
: ensure that a column is not contained in an array of valuesisMacAddress
: ensure that a column is a valid MAC addressisIPAddress
: ensure that a column is a valid IPv4 or IPv6 addressisIPv4Address
: ensure that a column is a valid IPv4 addressisIPv6Address
: ensure that a column is a valid IPv6 addressisUUID
: ensure that a column is a valid UUIDisEmail
: ensure that a column is a valid email addressisUrl
: ensure than a column is a valid URLisAlpha
: ensure than a column is all lettersisAlphaNumeric
: ensure than a column is all letters or numbershasLength
: ensure than a column is fits within the specified length accepts a min and optional max valueisLowercase
: ensure than a column is lowercaseisUppercase
: ensure than a column is uppercaseisEmpty
: ensure than a column empty (i.e. a blank string)isNotEmpty
: ensure than a column not empty (i.e. not a blank string)isCreditCard
: ensure than a is a valid credit card numbercheck
: accepts a function to perform validationAll of the validation methods are chainable, and accept an options argument.
The options include
message
: a message to return if a column fails validation. The message can include {val}
and {col}
replacements which will insert the invalid value and the column name.
[onlyDefined=true]
: set to false to run the method even if the column value is not defined.[onlyNotNull=true]
: set to false to run the method even if the column value is null.Property | Type | Default Value | Description |
validateOnSave | property | true
| Set to false to prevent model validation when saving. |
validateOnUpdate | property | true
| Set to false to prevent model validation when updating. |
Property | Type | Default Value | Description |
errors | Object | {} | the validation errors for this model. |
function (){ this._super(arguments); this.errors = {}; }
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
the name of the column, or a callback that accepts a function to create validators.
patio.Model|Validator
returns a validator if name is a string, other wise returns this for chaining.
patio.ModelError
if name is not a function or string.
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; }