File: src/assets/js/validators.js
/**
* @module backbone-survey
*/
var BackboneSurvey = BackboneSurvey || {};
(function() {
/**
* @class ValidationResult
* @uses Extendable
* @constructor
* @param {String} msg A result message
*/
var ValidationResult = BackboneSurvey.ValidationResult = function(msg) {
this.message = msg || "";
};
ValidationResult.prototype = {
/**
* Check a result is valid.
* @property valid
* @type {Boolean}
*/
valid: true
};
_.extend(ValidationResult, BackboneSurvey.Extendable);
/**
* A valid result.
*
* @class ValidationResult.OK
* @extends {ValidationResult}
*/
ValidationResult.OK = ValidationResult.extend({ valid: true });
/**
* An invalid result.
*
* @class ValidationResult.Error
* @extends {ValidationResult}
*/
ValidationResult.Error = ValidationResult.extend({ valid: false });
/**
* @class Validator
* @uses Extendable
* @constructor
* @param {Object} attr Merge into
{{#crossLink "Validator/attributes:property"}}{{/crossLink}}
*/
var Validator = BackboneSurvey.Validator = function(attr) {
/**
* @property attributes
* @type {Object}
*/
this.attributes = attr || {};
};
Validator.prototype = {
/**
* Run validation.
*
* @method validate
* @param {Array} value An array of variables.
* @param {Object} data
* @return {ValidationResult}
*/
validate: function(value, data) {
return new ValidationResult.OK();
}
/**
* Returns a validation message.
*
* @method message
* @param {Object} [attr] Any template variables.
* @return {String}
*/
, message: function(attr) {
attr = attr || {};
return (_.isString(this.attributes.template)) ?
_.template(this.attributes.template)(_.extend(this.attributes, attr)) :
(this.attributes.message || "");
}
};
_.extend(Validator, BackboneSurvey.Extendable);
/**
*
<pre><code>var validator = new RequiredValidator({
message: "Name is Required."
});
</code></pre>
*
* @class RequiredValidator
* @extends {Validator}
*/
BackboneSurvey.RequiredValidator = Validator.extend({
validate: function(value, data) {
var vs = _.isArray(value) ? value : [value];
var result;
if (vs.length === 0) {
result = new ValidationResult.Error(this.message());
} else {
var me = this;
_.each(vs, function(v) {
if (result) return;
if (_.isNumber(v)) v = v.toString();
if (_.isEmpty(v)) {
result = new ValidationResult.Error(me.message());
}
});
if (!result) {
result = new ValidationResult.OK();
}
}
return result;
}
});
/**
*
<pre><code>var validator = new PatternValidator({
message: "Name must be [a-z] characters"
, pattern: "^[a-z]+$" // new RegExp(...)
});</code></pre>
*
* @class PatternValidator
* @extends {Validator}
*/
BackboneSurvey.PatternValidator = Validator.extend({
validate: function(value, data) {
var vs = _.isArray(value) ? value : [value];
var result;
var regex = new RegExp(this.attributes.pattern);
var me = this;
_.each(vs, function(v) {
if (result) return;
if (!_.isString(v)) v = (typeof(v) !== "undefined" && v !== null) ? v.toString() : "";
if (!v.match(regex)) {
result = new ValidationResult.Error(me.message());
}
});
if (!result) {
result = new ValidationResult.OK();
}
return result;
}
});
/**
*
<pre><code>var validator = new RangeLengthValidator({
message: "Name must be between 8 and 32."
, min: 8
, max: 32
});</code></pre>
*
* @class RangeLengthValidator
* @extends {Validator}
*/
BackboneSurvey.RangeLengthValidator = Validator.extend({
validate: function(value, data) {
var vs = _.isArray(value) ? value : [value];
var result;
var me = this;
_.each(vs, function(v) {
if (result) return;
if (!_.isString(v)) v = (typeof(v) !== "undefined" && v !== null) ? v.toString() : "";
var min = _.isNumber(me.attributes.min) ? me.attributes.min : null;
var max = _.isNumber(me.attributes.max) ? me.attributes.max : null;
if ((min && min > v.length) || (max && max < v.length)) {
result = new ValidationResult.Error(me.message());
}
});
if (!result) {
result = new ValidationResult.OK();
}
return result;
}
});
})();