• Jump To … +
    modules/Behavior.js modules/Cell.js modules/Collection.js modules/Events.js modules/FormModel.js modules/FormView.js modules/ListView.js modules/Model.js modules/NestedCell.js modules/NestedModel.js modules/Router.js modules/ServiceCell.js modules/View.js modules/behaviors/DataBehavior.js modules/configure.js modules/handlebarsUtils.js modules/history.js modules/mixins/cacheMixin.js modules/mixins/cellMixin.js modules/mixins/loadingMixin.js modules/mixins/modelMixin.js modules/mixins/pollingMixin.js modules/registry.js modules/stickitUtils.js modules/templateRenderer.js modules/torso.js modules/validation.js
  • validation.js

  • ¶
    (function(root, factory) {
      if (typeof define === 'function' && define.amd) {
        define(['underscore', './NestedModel'], factory);
      } else if (typeof exports === 'object') {
        module.exports = factory(require('underscore'), require('./NestedModel'));
      } else {
        root.Torso = root.Torso || {};
        root.Torso.Mixins = root.Torso.Mixins || {};
        root.Torso.validation = factory(root._, root.Torso.NestedModel);
        root.Torso.Mixins.validation = root.Torso.validation.mixin;
      }
    }(this, function(_, NestedModel) {
      'use strict';
  • ¶

    Default options

  • ¶
      var defaultOptions = {
        forceUpdate: false,
        selector: 'name',
        labelFormatter: 'sentenceCase',
        messageFormatter: 'none',
        valid: Function.prototype,
        invalid: Function.prototype
      };
  • ¶

    Helper functions

  • ¶
  • ¶

    Formatting functions used for formatting error messages

      var formatFunctions = {
  • ¶

    Uses the configured label formatter to format the attribute name to make it more readable for the user

        formatLabel: function(attrName, model) {
          return defaultLabelFormatters[defaultOptions.labelFormatter](attrName, model);
        },
  • ¶

    Replaces nummeric placeholders like {0} in a string with arguments passed to the function

        format: function() {
          return defaultMessageFormatters[defaultOptions.messageFormatter].apply(defaultMessageFormatters, arguments);
        }
      };
  • ¶

    Flattens an object eg:

    var o = {
      owner: {
        name: 'Backbone',
        address: {
          street: 'Street',
          zip: 1234
        }
      }
    };

    becomes:

    var o = {
      'owner': {
        name: 'Backbone',
        address: {
          street: 'Street',
          zip: 1234
        }
      },
      'owner.name': 'Backbone',
      'owner.address': {
        street: 'Street',
        zip: 1234
      },
      'owner.address.street': 'Street',
      'owner.address.zip': 1234
    };

    This may seem redundant, but it allows for maximum flexibility in validation rules.

      var flatten = function (obj, into, prefix) {
        into = into || {};
        prefix = prefix || '';
    
        _.each(obj, function(val, key) {
          if(obj.hasOwnProperty(key)) {
            if (!!val && typeof val === 'object' && val.constructor === Object) {
              flatten(val, into, prefix + key + '.');
            }
  • ¶

    Register the current level object as well

            into[prefix + key] = val;
          }
        });
    
        return into;
      };
  • ¶

    Validation

  • ¶
      /**
       * Validation object containing validation mixin.
       *
       * @namespace Validation
       *
       * @author ariel.wexler@vecna.com, kent.willis@mostlyepic.com
       *
       * @see <a href="../annotated/modules/validation.html">validation Annotated Source</a>
       */
      var Validation = (function(){
  • ¶

    Returns an object with undefined properties for all attributes on the model that has defined one or more validation rules.

        var getValidatedAttrs = function(model, attrs) {
          attrs = attrs || _.keys(_.result(model, 'validation') || {});
          return _.reduce(attrs, function(memo, key) {
            memo[key] = void 0;
            return memo;
          }, {});
        };
  • ¶

    Returns an array with attributes passed through options

        var getOptionsAttrs = function(options) {
          var attrs = options.attributes;
          if (_.isFunction(attrs)) {
            attrs = attrs();
          }
          if (_.isArray(attrs)) {
            return attrs;
          }
        };
  • ¶

    Looks on the model for validations for a specified attribute. Returns an array of any validators defined, or an empty array if none is defined.

        var getValidators = function(model, attr) {
          var attrValidationSet = model.validation ? _.result(model, 'validation')[attr] || {} : {};
  • ¶

    If the validator is a function or a string, wrap it in a function validator

          if (_.isFunction(attrValidationSet) || _.isString(attrValidationSet)) {
            attrValidationSet = {
              fn: attrValidationSet
            };
          }
  • ¶

    Stick the validator object into an array

          if(!_.isArray(attrValidationSet)) {
            attrValidationSet = [attrValidationSet];
          }
  • ¶

    Reduces the array of validators into a new array with objects with a validation method to call, the value to validate against and the specified error message, if any

          return _.reduce(attrValidationSet, function(memo, attrValidation) {
            _.each(_.without(_.keys(attrValidation), 'msg', 'msgKey'), function(validator) {
              memo.push({
                fn: defaultValidators[validator],
                val: attrValidation[validator],
                msg: attrValidation.msg,
                msgKey: attrValidation.msgKey
              });
            });
            return memo;
          }, []);
        };
  • ¶

    Gets the indices out of an attr name: foo[0][1] -> [0, 1] and foo[2] -> [2]

        var extractIndices = function(attr) {
          var startIndex, endIndex,
            i = 0,
            hasEndBracket = true,
            indices = [];
          startIndex = attr.indexOf('[', i);
          while (startIndex > 0 && hasEndBracket) {
             endIndex = attr.indexOf(']', i);
             indices.push(parseInt(attr.substring(startIndex + 1, endIndex), 10));
             i = endIndex + 1;
             hasEndBracket = i > 0;
             startIndex = attr.indexOf('[', i);
          }
          return indices;
        };
  • ¶

    Generates an array of all the possible field accessors and their indices when using the “open” array notation foo[] -> [{attr: ‘foo[0]’, index: [0]}, {attr: ‘foo[1]’, index: [1]}]. Will also perform nested arrays: foo[][] -> [[{attr: ‘foo[0][0]’, index: [0][0]}], [{attr: ‘foo[1][0]’, index: [1][0]}]]

        var generateSubAttributes = function(attr, model, subIndices) {
          var i, attrName, remainder, subAttrs, newIndices,
            firstBracket = attr.indexOf('[]');
          if (_.isEmpty(subIndices)) {
            subIndices = [];
          }
          if (firstBracket === -1) {
            return {attr: attr, index: subIndices};
          } else {
            attrName = attr.substring(0, firstBracket);
            remainder = attr.substring(firstBracket + 2);
            subAttrs = [];
            var valueArray = model.get(attrName);
            _.each(valueArray, function(value, i) {
              newIndices = subIndices.slice();
              newIndices.push(i);
              subAttrs.push(generateSubAttributes(attrName + '[' + i + ']' + remainder, model, newIndices));
            });
            return subAttrs;
          }
        };
  • ¶

    Is this model a nested torso model

        var isNestedModel = function(model) {
          return NestedModel && model instanceof NestedModel;
        };
  • ¶

    Is the attribute using dot notation or array notation: foo.bar or foo[] or foo[1]

        var isNestedAttr = function(attr) {
          return attr.indexOf('.') > 0 || attr.indexOf(']') > 0;
        };
  • ¶

    Remove the indices for a field name: foo[1][2] -> foo[][]

        var stripIndices = function(attr) {
          var startIndex, newAttr,
            i = 0,
            hasEndBracket = true;
          startIndex = attr.indexOf('[', i);
          if (startIndex < 0) {
            return attr;
          }
          newAttr = attr.substring(0, startIndex + 1);
          while (startIndex > 0 && hasEndBracket) {
             i = attr.indexOf(']', i) + 1;
             hasEndBracket = i > 0;
             startIndex = attr.indexOf('[', i);
             if (startIndex > 0) {
               newAttr = newAttr + attr.substring(i - 1, startIndex + 1);
             }
          }
          newAttr = newAttr + attr.substring(i - 1);
          return newAttr;
        };
  • ¶

    Validates safely for a nested attribute. If the attr is an array, it will construct validation errors with the same array structure [‘foo[0]’, ‘foo[1]’] -> [false, ‘Error Example’] Also handles nested array structure

        var validateWithOpenArrayHelper = function(model, attrConfig, value, computed, validators, depth) {
          var attr, indices;
    
          if (_.isArray(attrConfig)) {
            return _.reduce(attrConfig, function(memo, nestedAttrConfig) {
              memo.push(validateWithOpenArrayHelper(model, nestedAttrConfig, value, computed, validators, depth + 1));
              return memo;
            }, []);
          } else {
            indices = attrConfig.index;
            attr = attrConfig.attr;
  • ¶

    if the value wasn’t passed in and the attribute is nested, get the value

            if (_.isUndefined(value) && isNestedAttr(attr)) {
              value = model.get(attr);
            }
            return invokeValidator(validators, model, value, attr, computed, indices);
          }
        };
  • ¶

    Invokes the validator set for an attr

        var invokeValidator = function(validators, model, value, attr, computed, indices) {
          return _.reduce(validators, function(memo, validator) {
  • ¶

    Pass the format functions plus the default validators as the context to the validator

              var context = _.extend({msgKey: validator.msgKey}, formatFunctions, defaultValidators),
                  result = validator.fn.call(context, value, attr, validator.val, model, computed, indices);
    
              if (result === false || memo === false) {
                return false;
              }
              if (result && !memo) {
                return _.result(_.extend({}, validator, formatFunctions, defaultValidators), 'msg') || result;
              }
              return memo;
            }, '');
        };
  • ¶

    Validates an attribute against all validators defined for that attribute. If one or more errors are found, the first error message is returned. If the attribute is valid, an empty string is returned.

        var validateAttrWithOpenArray = function(model, attr, value, computed) {
  • ¶

    Reduces the array of validators to an error message by applying all the validators and returning the first error message, if any.

          var hasErrors, subAttr, result,
            validators = getValidators(model, attr);
          subAttr = generateSubAttributes(attr, model);
          result = validateWithOpenArrayHelper(model, subAttr, value, computed, validators, 0);
          if (_.isArray(result)) {
            hasErrors = _.reduce(_.flatten(result), function(memo, val) {
              return memo || val;
            }, false);
            if (!hasErrors) {
              return '';
            }
          }
          return result;
        };
  • ¶

    Loops through the model’s attributes and validates the specified attrs. Returns and object containing names of invalid attributes as well as error messages.

        var validateModel = function(model, attrs, validatedAttrs) {
          var error,
              invalidAttrs = {},
              isValid = true,
              computed = _.clone(attrs);
    
          _.each(validatedAttrs, function(val, attr) {
            error = validateAttrWithOpenArray(model, attr, val, computed);
            if (error) {
              invalidAttrs[attr] = error;
              isValid = false;
            }
          });
    
          return {
            invalidAttrs: invalidAttrs,
            isValid: isValid
          };
        };
  • ¶

    Validates attribute without open array notation.

        var validateAttr = function(model, value, attr) {
          var indices, validators,
            validations = model.validation ? _.result(model, 'validation') || {} : {};
  • ¶

    If the validations hash contains an entry for the attr

          if (_.contains(_.keys(validations), attr)) {
            return validateAttrWithOpenArray(model, attr, value, _.extend({}, model.attributes));
          } else {
            indices = extractIndices(attr);
            attr = stripIndices(attr);
            validators = getValidators(model, attr);
            return invokeValidator(validators, model, value, attr, _.extend({}, model.attributes), indices);
          }
        };
    
        /**
         * Contains the methods that are mixed in on the model when binding
         *
         * @mixin validationMixin
         */
        var mixin = function(options) {
          return /** @lends validationMixin */ {
    
            /**
             * Check whether an attribute or a set of attributes are valid. It will default to use the model's current values but
             * you can pass in different values to use in the validation process instead.
             * @param {(string|Object|string[])} attr Either the name of the attribute, an array containing many attribute names, or
             * on object with attribute name to values
             * @param {Any} [value] a value to use for the attribute value instead of using the model's value.
             * @returns {(undefined|string|Object)} undefined if no errors, a validation exception if a single attribute, or an object with attribute name as key
             * and the error as the value
             */
            preValidate: function(attr, value) {
              var self = this,
                  result = {},
                  error;
              if (_.isArray(attr)) {
                _.each(attr, function(attr) {
                  error = self.preValidate(attr);
                  if (error) {
                    result[attr] = error;
                  }
                });
                return _.isEmpty(result) ? undefined : result;
              } else if (_.isObject(attr)) {
                _.each(attr, function(value, key) {
                  error = self.preValidate(key, value);
                  if (error) {
                    result[key] = error;
                  }
                });
                return _.isEmpty(result) ? undefined : result;
              } else {
                if (_.isUndefined(value) && isNestedModel(this)) {
                  value = this.get(attr);
                }
                return validateAttr(this, value, attr);
              }
            },
    
            /**
             * Check to see if an attribute, an array of attributes or the
             * entire model is valid. Passing true will force a validation
             * of the model.
             */
            isValid: function(option) {
              var flattened, attrs, error, invalidAttrs;
    
              option = option || getOptionsAttrs(options);
    
              if(_.isString(option)){
                attrs = [option];
              } else if(_.isArray(option)) {
                attrs = option;
              }
              if (attrs) {
  • ¶

    Loop through all attributes

                _.each(attrs, function (attr) {
                  var value;
                  if (isNestedModel(this)) {
                    value = this.get(attr);
                  } else {
                    value = flatten(this.attributes)[attr];
                  }
                  error = validateAttr(this, value, attr);
                  if (error) {
                    invalidAttrs = invalidAttrs || {};
                    invalidAttrs[attr] = error;
                  }
                }, this);
              }
    
              if (option === true) {
                invalidAttrs = this.validate();
              }
              if (invalidAttrs) {
                this.trigger('invalid', this, invalidAttrs, {validationError: invalidAttrs});
              }
              return attrs ? !invalidAttrs : this.validation ? this._isValid : true;
            },
    
            /**
             * This is called by Backbone when it needs to perform validation.
             * You can call it manually without any parameters to validate the
             * entire model.
             */
            validate: function(attrs, setOptions){
              var model = this;
              var opt = _.extend({}, options, setOptions);
              var validatedAttrs = getValidatedAttrs(model, getOptionsAttrs(options));
              var allAttrs = _.extend({}, validatedAttrs, model.attributes, attrs);
              var flattened = _.extend(flatten(allAttrs), attrs); // allow people to pass in nested attributes
              var changedAttrs = attrs ? flatten(attrs) : flattened;
              var result = validateModel(model, allAttrs, _.pick(flattened, _.keys(validatedAttrs)));
              model._isValid = result.isValid;
  • ¶

    Trigger validated events. Need to defer this so the model is actually updated before the event is triggered.

              _.defer(function() {
                model.trigger('validated', model._isValid, model, result.invalidAttrs);
                model.trigger('validated:' + (model._isValid ? 'valid' : 'invalid'), model, result.invalidAttrs);
              });
  • ¶

    Return any error messages to Backbone, unless the forceUpdate flag is set. Then we do not return anything and fools Backbone to believe the validation was a success. That way Backbone will update the model regardless.

              if (!opt.forceUpdate && _.intersection(_.keys(result.invalidAttrs), _.keys(changedAttrs)).length > 0) {
                return result.invalidAttrs;
              }
            }
          };
        };
  • ¶

    Returns the public methods on Backbone.Validation

        return /** @lends Validation */ {
    
          /**
           * Current version of the library
           * @type {string}
           */
          version: '0.11.3',
    
          /**
           * Called to augment configure the default options
           * @param options
           */
          configure: function(options) {
            _.extend(defaultOptions, options);
          },
    
          /**
           * Used to extend the Backbone.Model.prototype with validation
           *
           * @type {validationMixin}
           */
          mixin: mixin(defaultOptions)
        };
      }());
  • ¶

    Patterns

  • ¶
      var defaultPatterns = Validation.patterns = {
  • ¶

    Matches any digit(s) (i.e. 0-9)

        digits: /^\d+$/,
  • ¶

    Matches any number (e.g. 100.000)

        number: /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/,
  • ¶

    Matches a valid email address (e.g. mail@example.com)

        email: /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i,
  • ¶

    Mathes any valid url (e.g. http://www.xample.com)

        url: /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i
      };
  • ¶

    Error messages

  • ¶
  • ¶

    Error message for the build in validators. {x} gets swapped out with arguments form the validator.

      var defaultMessages = Validation.messages = {
        required: '{0} is required',
        acceptance: '{0} must be accepted',
        min: '{0} must be greater than or equal to {1}',
        max: '{0} must be less than or equal to {1}',
        range: '{0} must be between {1} and {2}',
        length: '{0} must be {1} characters',
        minLength: '{0} must be at least {1} characters',
        maxLength: '{0} must be at most {1} characters',
        rangeLength: '{0} must be between {1} and {2} characters',
        oneOf: '{0} must be one of: {1}',
        equalTo: '{0} must be the same as {1}',
        digits: '{0} must only contain digits',
        number: '{0} must be a number',
        email: '{0} must be a valid email',
        url: '{0} must be a valid url',
        inlinePattern: '{0} is invalid'
      };
  • ¶

    Label formatters

  • ¶
  • ¶

    Label formatters are used to convert the attribute name to a more human friendly label when using the built in error messages. Configure which one to use with a call to

    Backbone.Validation.configure({
      labelFormatter: 'label'
    });
      var defaultLabelFormatters = Validation.labelFormatters = {
  • ¶

    Returns the attribute name with applying any formatting

        none: function(attrName) {
          return attrName;
        },
  • ¶

    Converts attributeName or attribute_name to Attribute name

        sentenceCase: function(attrName) {
          return attrName.replace(/(?:^\w|[A-Z]|\b\w)/g, function(match, index) {
            return index === 0 ? match.toUpperCase() : ' ' + match.toLowerCase();
          }).replace(/_/g, ' ');
        },
  • ¶

    Looks for a label configured on the model and returns it

     var Model = Backbone.Model.extend({
       validation: {
         someAttribute: {
           required: true
         }
       },
    
       labels: {
         someAttribute: 'Custom label'
       }
     });
        label: function(attrName, model) {
          return (model.labels && model.labels[attrName]) || defaultLabelFormatters.sentenceCase(attrName, model);
        }
      };
  • ¶

    Message Formatters

  • ¶
      var defaultMessageFormatters = Validation.messageFormatters = {
        none: function() {
          var args = Array.prototype.slice.call(arguments),
            text = args.shift();
          return text.replace(/\{(\d+)\}/g, function(match, number) {
            return typeof args[number] !== 'undefined' ? args[number] : match;
          });
        }
      };
  • ¶

    Built in validators

  • ¶
      var defaultValidators = Validation.validators = (function(){
  • ¶

    Use native trim when defined

        var trim = String.prototype.trim ?
          function(text) {
            return text === null ? '' : String.prototype.trim.call(text);
          } :
          function(text) {
            var trimLeft = /^\s+/,
                trimRight = /\s+$/;
    
            return text === null ? '' : text.toString().replace(trimLeft, '').replace(trimRight, '');
          };
  • ¶

    Determines whether or not a value is a number

        var isNumber = function(value){
          return _.isNumber(value) || (_.isString(value) && value.match(defaultPatterns.number));
        };
  • ¶

    Determines whether or not a value is empty

        var hasValue = function(value) {
          return !(_.isNull(value) || _.isUndefined(value) || (_.isString(value) && trim(value) === '') || (_.isArray(value) && _.isEmpty(value)));
        };
    
        var getMessageKey = function(msgKey, defaultKey) {
          return msgKey ? msgKey : defaultKey;
        };
    
        return {
          format: formatFunctions.format,
          formatLabel: formatFunctions.formatLabel,
  • ¶

    Function validator Lets you implement a custom function used for validation

          fn: function(value, attr, fn, model, computed) {
            if(_.isString(fn)){
              fn = model[fn];
            }
            return fn.call(model, value, attr, computed);
          },
  • ¶

    Allows the creation of an inline function that uses the validators context instead of the model context.

          inlineFn: function(value, attr, fn, model, computed, indices) {
            return fn.call(this, value, attr, model, computed, indices);
          },
  • ¶

    Required validator Validates if the attribute is required or not This can be specified as either a boolean value or a function that returns a boolean value

          required: function(value, attr, required, model, computed) {
            var isRequired = _.isFunction(required) ? required.call(model, value, attr, computed) : required;
            if(!isRequired && !hasValue(value)) {
              return false; // overrides all other validators
            }
            if (isRequired && !hasValue(value)) {
              return this.format(getMessageKey(this.msgKey, defaultMessages.required), this.formatLabel(attr, model));
            }
          },
  • ¶

    Acceptance validator Validates that something has to be accepted, e.g. terms of use true or ‘true’ are valid

          acceptance: function(value, attr, accept, model) {
            if(value !== 'true' && (!_.isBoolean(value) || value === false)) {
              return this.format(getMessageKey(this.msgKey, defaultMessages.acceptance), this.formatLabel(attr, model));
            }
          },
  • ¶

    Min validator Validates that the value has to be a number and equal to or greater than the min value specified

          min: function(value, attr, minValue, model) {
            if (!isNumber(value) || value < minValue) {
              return this.format(getMessageKey(this.msgKey, defaultMessages.min), this.formatLabel(attr, model), minValue);
            }
          },
  • ¶

    Max validator Validates that the value has to be a number and equal to or less than the max value specified

          max: function(value, attr, maxValue, model) {
            if (!isNumber(value) || value > maxValue) {
              return this.format(getMessageKey(this.msgKey, defaultMessages.max), this.formatLabel(attr, model), maxValue);
            }
          },
  • ¶

    Range validator Validates that the value has to be a number and equal to or between the two numbers specified

          range: function(value, attr, range, model) {
            if(!isNumber(value) || value < range[0] || value > range[1]) {
              return this.format(getMessageKey(this.msgKey, defaultMessages.range), this.formatLabel(attr, model), range[0], range[1]);
            }
          },
  • ¶

    Length validator Validates that the value has to be a string with length equal to the length value specified

          length: function(value, attr, length, model) {
            if (!_.isString(value) || value.length !== length) {
              return this.format(getMessageKey(this.msgKey, defaultMessages.length), this.formatLabel(attr, model), length);
            }
          },
  • ¶

    Min length validator Validates that the value has to be a string with length equal to or greater than the min length value specified

          minLength: function(value, attr, minLength, model) {
            if (!_.isString(value) || value.length < minLength) {
              return this.format(getMessageKey(this.msgKey, defaultMessages.minLength), this.formatLabel(attr, model), minLength);
            }
          },
  • ¶

    Max length validator Validates that the value has to be a string with length equal to or less than the max length value specified

          maxLength: function(value, attr, maxLength, model) {
            if (!_.isString(value) || value.length > maxLength) {
              return this.format(getMessageKey(this.msgKey, defaultMessages.maxLength), this.formatLabel(attr, model), maxLength);
            }
          },
  • ¶

    Range length validator Validates that the value has to be a string and equal to or between the two numbers specified

          rangeLength: function(value, attr, range, model) {
            if (!_.isString(value) || value.length < range[0] || value.length > range[1]) {
              return this.format(getMessageKey(this.msgKey, defaultMessages.rangeLength), this.formatLabel(attr, model), range[0], range[1]);
            }
          },
  • ¶

    One of validator Validates that the value has to be equal to one of the elements in the specified array. Case sensitive matching

          oneOf: function(value, attr, values, model) {
            if(!_.include(values, value)){
              return this.format(getMessageKey(this.msgKey, defaultMessages.oneOf), this.formatLabel(attr, model), values.join(', '));
            }
          },
  • ¶

    Equal to validator Validates that the value has to be equal to the value of the attribute with the name specified

          equalTo: function(value, attr, equalTo, model, computed) {
            if(value !== computed[equalTo]) {
              return this.format(getMessageKey(this.msgKey, defaultMessages.equalTo), this.formatLabel(attr, model), this.formatLabel(equalTo, model));
            }
          },
  • ¶

    Pattern validator Validates that the value has to match the pattern specified. Can be a regular expression or the name of one of the built in patterns

          pattern: function(value, attr, pattern, model) {
            if (!hasValue(value) || !value.toString().match(defaultPatterns[pattern] || pattern)) {
              return this.format(getMessageKey(this.msgKey, defaultMessages[pattern]) || defaultMessages.inlinePattern, this.formatLabel(attr, model), pattern);
            }
          }
        };
      }());
    
      return Validation;
    }));