Andrey Pavlov | 9b47f32 | 2020-04-05 22:31:40 +0300 | [diff] [blame] | 1 | // Backbone.Validation v0.9.1 |
| 2 | // |
| 3 | // Copyright (c) 2011-2014 Thomas Pedersen |
| 4 | // Distributed under MIT License |
| 5 | // |
| 6 | // Documentation and full license available at: |
| 7 | // http://thedersen.com/projects/backbone-validation |
| 8 | (function (factory) { |
| 9 | if (typeof exports === 'object') { |
| 10 | module.exports = factory(require('backbone'), require('underscore')); |
| 11 | } else if (typeof define === 'function' && define.amd) { |
| 12 | define(['backbone', 'underscore'], factory); |
| 13 | } |
| 14 | }(function (Backbone, _) { |
| 15 | Backbone.Validation = (function(_){ |
| 16 | 'use strict'; |
| 17 | |
| 18 | // Default options |
| 19 | // --------------- |
| 20 | |
| 21 | var defaultOptions = { |
| 22 | forceUpdate: false, |
| 23 | selector: 'name', |
| 24 | labelFormatter: 'sentenceCase', |
| 25 | valid: Function.prototype, |
| 26 | invalid: Function.prototype |
| 27 | }; |
| 28 | |
| 29 | |
| 30 | // Helper functions |
| 31 | // ---------------- |
| 32 | |
| 33 | // Formatting functions used for formatting error messages |
| 34 | var formatFunctions = { |
| 35 | // Uses the configured label formatter to format the attribute name |
| 36 | // to make it more readable for the user |
| 37 | formatLabel: function(attrName, model) { |
| 38 | return defaultLabelFormatters[defaultOptions.labelFormatter](attrName, model); |
| 39 | }, |
| 40 | |
| 41 | // Replaces nummeric placeholders like {0} in a string with arguments |
| 42 | // passed to the function |
| 43 | format: function() { |
| 44 | var args = Array.prototype.slice.call(arguments), |
| 45 | text = args.shift(); |
| 46 | return text.replace(/\{(\d+)\}/g, function(match, number) { |
| 47 | return typeof args[number] !== 'undefined' ? args[number] : match; |
| 48 | }); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | // Flattens an object |
| 53 | // eg: |
| 54 | // |
| 55 | // var o = { |
| 56 | // address: { |
| 57 | // street: 'Street', |
| 58 | // zip: 1234 |
| 59 | // } |
| 60 | // }; |
| 61 | // |
| 62 | // becomes: |
| 63 | // |
| 64 | // var o = { |
| 65 | // 'address.street': 'Street', |
| 66 | // 'address.zip': 1234 |
| 67 | // }; |
| 68 | var flatten = function (obj, into, prefix) { |
| 69 | into = into || {}; |
| 70 | prefix = prefix || ''; |
| 71 | |
| 72 | _.each(obj, function(val, key) { |
| 73 | if(obj.hasOwnProperty(key)) { |
| 74 | if (val && typeof val === 'object' && !( |
| 75 | val instanceof Array || |
| 76 | val instanceof Date || |
| 77 | val instanceof RegExp || |
| 78 | val instanceof Backbone.Model || |
| 79 | val instanceof Backbone.Collection) |
| 80 | ) { |
| 81 | flatten(val, into, prefix + key + '.'); |
| 82 | } |
| 83 | else { |
| 84 | into[prefix + key] = val; |
| 85 | } |
| 86 | } |
| 87 | }); |
| 88 | |
| 89 | return into; |
| 90 | }; |
| 91 | |
| 92 | // Validation |
| 93 | // ---------- |
| 94 | |
| 95 | var Validation = (function(){ |
| 96 | |
| 97 | // Returns an object with undefined properties for all |
| 98 | // attributes on the model that has defined one or more |
| 99 | // validation rules. |
| 100 | var getValidatedAttrs = function(model) { |
| 101 | return _.reduce(_.keys(_.result(model, 'validation') || {}), function(memo, key) { |
| 102 | memo[key] = void 0; |
| 103 | return memo; |
| 104 | }, {}); |
| 105 | }; |
| 106 | |
| 107 | // Looks on the model for validations for a specified |
| 108 | // attribute. Returns an array of any validators defined, |
| 109 | // or an empty array if none is defined. |
| 110 | var getValidators = function(model, attr) { |
| 111 | var attrValidationSet = model.validation ? _.result(model, 'validation')[attr] || {} : {}; |
| 112 | |
| 113 | // If the validator is a function or a string, wrap it in a function validator |
| 114 | if (_.isFunction(attrValidationSet) || _.isString(attrValidationSet)) { |
| 115 | attrValidationSet = { |
| 116 | fn: attrValidationSet |
| 117 | }; |
| 118 | } |
| 119 | |
| 120 | // Stick the validator object into an array |
| 121 | if(!_.isArray(attrValidationSet)) { |
| 122 | attrValidationSet = [attrValidationSet]; |
| 123 | } |
| 124 | |
| 125 | // Reduces the array of validators into a new array with objects |
| 126 | // with a validation method to call, the value to validate against |
| 127 | // and the specified error message, if any |
| 128 | return _.reduce(attrValidationSet, function(memo, attrValidation) { |
| 129 | _.each(_.without(_.keys(attrValidation), 'msg'), function(validator) { |
| 130 | memo.push({ |
| 131 | fn: defaultValidators[validator], |
| 132 | val: attrValidation[validator], |
| 133 | msg: attrValidation.msg |
| 134 | }); |
| 135 | }); |
| 136 | return memo; |
| 137 | }, []); |
| 138 | }; |
| 139 | |
| 140 | // Validates an attribute against all validators defined |
| 141 | // for that attribute. If one or more errors are found, |
| 142 | // the first error message is returned. |
| 143 | // If the attribute is valid, an empty string is returned. |
| 144 | var validateAttr = function(model, attr, value, computed) { |
| 145 | // Reduces the array of validators to an error message by |
| 146 | // applying all the validators and returning the first error |
| 147 | // message, if any. |
| 148 | return _.reduce(getValidators(model, attr), function(memo, validator){ |
| 149 | // Pass the format functions plus the default |
| 150 | // validators as the context to the validator |
| 151 | var ctx = _.extend({}, formatFunctions, defaultValidators), |
| 152 | result = validator.fn.call(ctx, value, attr, validator.val, model, computed); |
| 153 | |
| 154 | if(result === false || memo === false) { |
| 155 | return false; |
| 156 | } |
| 157 | if (result && !memo) { |
| 158 | return _.result(validator, 'msg') || result; |
| 159 | } |
| 160 | return memo; |
| 161 | }, ''); |
| 162 | }; |
| 163 | |
| 164 | // Loops through the model's attributes and validates them all. |
| 165 | // Returns and object containing names of invalid attributes |
| 166 | // as well as error messages. |
| 167 | var validateModel = function(model, attrs) { |
| 168 | var error, |
| 169 | invalidAttrs = {}, |
| 170 | isValid = true, |
| 171 | computed = _.clone(attrs), |
| 172 | flattened = flatten(attrs); |
| 173 | |
| 174 | _.each(flattened, function(val, attr) { |
| 175 | error = validateAttr(model, attr, val, computed); |
| 176 | if (error) { |
| 177 | invalidAttrs[attr] = error; |
| 178 | isValid = false; |
| 179 | } |
| 180 | }); |
| 181 | |
| 182 | return { |
| 183 | invalidAttrs: invalidAttrs, |
| 184 | isValid: isValid |
| 185 | }; |
| 186 | }; |
| 187 | |
| 188 | // Contains the methods that are mixed in on the model when binding |
| 189 | var mixin = function(view, options) { |
| 190 | return { |
| 191 | |
| 192 | // Check whether or not a value, or a hash of values |
| 193 | // passes validation without updating the model |
| 194 | preValidate: function(attr, value) { |
| 195 | var self = this, |
| 196 | result = {}, |
| 197 | error; |
| 198 | |
| 199 | if(_.isObject(attr)){ |
| 200 | _.each(attr, function(value, key) { |
| 201 | error = self.preValidate(key, value); |
| 202 | if(error){ |
| 203 | result[key] = error; |
| 204 | } |
| 205 | }); |
| 206 | |
| 207 | return _.isEmpty(result) ? undefined : result; |
| 208 | } |
| 209 | else { |
| 210 | return validateAttr(this, attr, value, _.extend({}, this.attributes)); |
| 211 | } |
| 212 | }, |
| 213 | |
| 214 | // Check to see if an attribute, an array of attributes or the |
| 215 | // entire model is valid. Passing true will force a validation |
| 216 | // of the model. |
| 217 | isValid: function(option) { |
| 218 | var flattened = flatten(this.attributes); |
| 219 | |
| 220 | if(_.isString(option)){ |
| 221 | return !validateAttr(this, option, flattened[option], _.extend({}, this.attributes)); |
| 222 | } |
| 223 | if(_.isArray(option)){ |
| 224 | return _.reduce(option, function(memo, attr) { |
| 225 | return memo && !validateAttr(this, attr, flattened[attr], _.extend({}, this.attributes)); |
| 226 | }, true, this); |
| 227 | } |
| 228 | if(option === true) { |
| 229 | this.validate(); |
| 230 | } |
| 231 | return this.validation ? this._isValid : true; |
| 232 | }, |
| 233 | |
| 234 | // This is called by Backbone when it needs to perform validation. |
| 235 | // You can call it manually without any parameters to validate the |
| 236 | // entire model. |
| 237 | validate: function(attrs, setOptions){ |
| 238 | var model = this, |
| 239 | validateAll = !attrs, |
| 240 | opt = _.extend({}, options, setOptions), |
| 241 | validatedAttrs = getValidatedAttrs(model), |
| 242 | allAttrs = _.extend({}, validatedAttrs, model.attributes, attrs), |
| 243 | changedAttrs = flatten(attrs || allAttrs), |
| 244 | |
| 245 | result = validateModel(model, allAttrs); |
| 246 | |
| 247 | model._isValid = result.isValid; |
| 248 | |
| 249 | // After validation is performed, loop through all validated attributes |
| 250 | // and call the valid callbacks so the view is updated. |
| 251 | _.each(validatedAttrs, function(val, attr){ |
| 252 | var invalid = result.invalidAttrs.hasOwnProperty(attr); |
| 253 | if(!invalid){ |
| 254 | opt.valid(view, attr, opt.selector); |
| 255 | } |
| 256 | }); |
| 257 | |
| 258 | // After validation is performed, loop through all validated and changed attributes |
| 259 | // and call the invalid callback so the view is updated. |
| 260 | _.each(validatedAttrs, function(val, attr){ |
| 261 | var invalid = result.invalidAttrs.hasOwnProperty(attr), |
| 262 | changed = changedAttrs.hasOwnProperty(attr); |
| 263 | |
| 264 | if(invalid && (changed || validateAll)){ |
| 265 | opt.invalid(view, attr, result.invalidAttrs[attr], opt.selector); |
| 266 | } |
| 267 | }); |
| 268 | |
| 269 | // Trigger validated events. |
| 270 | // Need to defer this so the model is actually updated before |
| 271 | // the event is triggered. |
| 272 | _.defer(function() { |
| 273 | model.trigger('validated', model._isValid, model, result.invalidAttrs); |
| 274 | model.trigger('validated:' + (model._isValid ? 'valid' : 'invalid'), model, result.invalidAttrs); |
| 275 | }); |
| 276 | |
| 277 | // Return any error messages to Backbone, unless the forceUpdate flag is set. |
| 278 | // Then we do not return anything and fools Backbone to believe the validation was |
| 279 | // a success. That way Backbone will update the model regardless. |
| 280 | if (!opt.forceUpdate && _.intersection(_.keys(result.invalidAttrs), _.keys(changedAttrs)).length > 0) { |
| 281 | return result.invalidAttrs; |
| 282 | } |
| 283 | } |
| 284 | }; |
| 285 | }; |
| 286 | |
| 287 | // Helper to mix in validation on a model |
| 288 | var bindModel = function(view, model, options) { |
| 289 | _.extend(model, mixin(view, options)); |
| 290 | }; |
| 291 | |
| 292 | // Removes the methods added to a model |
| 293 | var unbindModel = function(model) { |
| 294 | delete model.validate; |
| 295 | delete model.preValidate; |
| 296 | delete model.isValid; |
| 297 | }; |
| 298 | |
| 299 | // Mix in validation on a model whenever a model is |
| 300 | // added to a collection |
| 301 | var collectionAdd = function(model) { |
| 302 | bindModel(this.view, model, this.options); |
| 303 | }; |
| 304 | |
| 305 | // Remove validation from a model whenever a model is |
| 306 | // removed from a collection |
| 307 | var collectionRemove = function(model) { |
| 308 | unbindModel(model); |
| 309 | }; |
| 310 | |
| 311 | // Returns the public methods on Backbone.Validation |
| 312 | return { |
| 313 | |
| 314 | // Current version of the library |
| 315 | version: '0.9.1', |
| 316 | |
| 317 | // Called to configure the default options |
| 318 | configure: function(options) { |
| 319 | _.extend(defaultOptions, options); |
| 320 | }, |
| 321 | |
| 322 | // Hooks up validation on a view with a model |
| 323 | // or collection |
| 324 | bind: function(view, options) { |
| 325 | options = _.extend({}, defaultOptions, defaultCallbacks, options); |
| 326 | |
| 327 | var model = options.model || view.model, |
| 328 | collection = options.collection || view.collection; |
| 329 | |
| 330 | if(typeof model === 'undefined' && typeof collection === 'undefined'){ |
| 331 | throw 'Before you execute the binding your view must have a model or a collection.\n' + |
| 332 | 'See http://thedersen.com/projects/backbone-validation/#using-form-model-validation for more information.'; |
| 333 | } |
| 334 | |
| 335 | if(model) { |
| 336 | bindModel(view, model, options); |
| 337 | } |
| 338 | else if(collection) { |
| 339 | collection.each(function(model){ |
| 340 | bindModel(view, model, options); |
| 341 | }); |
| 342 | collection.bind('add', collectionAdd, {view: view, options: options}); |
| 343 | collection.bind('remove', collectionRemove); |
| 344 | } |
| 345 | }, |
| 346 | |
| 347 | // Removes validation from a view with a model |
| 348 | // or collection |
| 349 | unbind: function(view, options) { |
| 350 | options = _.extend({}, options); |
| 351 | var model = options.model || view.model, |
| 352 | collection = options.collection || view.collection; |
| 353 | |
| 354 | if(model) { |
| 355 | unbindModel(model); |
| 356 | } |
| 357 | else if(collection) { |
| 358 | collection.each(function(model){ |
| 359 | unbindModel(model); |
| 360 | }); |
| 361 | collection.unbind('add', collectionAdd); |
| 362 | collection.unbind('remove', collectionRemove); |
| 363 | } |
| 364 | }, |
| 365 | |
| 366 | // Used to extend the Backbone.Model.prototype |
| 367 | // with validation |
| 368 | mixin: mixin(null, defaultOptions) |
| 369 | }; |
| 370 | }()); |
| 371 | |
| 372 | |
| 373 | // Callbacks |
| 374 | // --------- |
| 375 | |
| 376 | var defaultCallbacks = Validation.callbacks = { |
| 377 | |
| 378 | // Gets called when a previously invalid field in the |
| 379 | // view becomes valid. Removes any error message. |
| 380 | // Should be overridden with custom functionality. |
| 381 | valid: function(view, attr, selector) { |
| 382 | view.$('[' + selector + '~="' + attr + '"]') |
| 383 | .removeClass('invalid') |
| 384 | .removeAttr('data-error'); |
| 385 | }, |
| 386 | |
| 387 | // Gets called when a field in the view becomes invalid. |
| 388 | // Adds a error message. |
| 389 | // Should be overridden with custom functionality. |
| 390 | invalid: function(view, attr, error, selector) { |
| 391 | view.$('[' + selector + '~="' + attr + '"]') |
| 392 | .addClass('invalid') |
| 393 | .attr('data-error', error); |
| 394 | } |
| 395 | }; |
| 396 | |
| 397 | |
| 398 | // Patterns |
| 399 | // -------- |
| 400 | |
| 401 | var defaultPatterns = Validation.patterns = { |
| 402 | // Matches any digit(s) (i.e. 0-9) |
| 403 | digits: /^\d+$/, |
| 404 | |
| 405 | // Matches any number (e.g. 100.000) |
| 406 | number: /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/, |
| 407 | |
| 408 | // Matches a valid email address (e.g. mail@example.com) |
| 409 | 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, |
| 410 | |
| 411 | // Mathes any valid url (e.g. http://www.xample.com) |
| 412 | 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 |
| 413 | }; |
| 414 | |
| 415 | |
| 416 | // Error messages |
| 417 | // -------------- |
| 418 | |
| 419 | // Error message for the build in validators. |
| 420 | // {x} gets swapped out with arguments form the validator. |
| 421 | var defaultMessages = Validation.messages = { |
| 422 | required: '{0} is required', |
| 423 | acceptance: '{0} must be accepted', |
| 424 | min: '{0} must be greater than or equal to {1}', |
| 425 | max: '{0} must be less than or equal to {1}', |
| 426 | range: '{0} must be between {1} and {2}', |
| 427 | length: '{0} must be {1} characters', |
| 428 | minLength: '{0} must be at least {1} characters', |
| 429 | maxLength: '{0} must be at most {1} characters', |
| 430 | rangeLength: '{0} must be between {1} and {2} characters', |
| 431 | oneOf: '{0} must be one of: {1}', |
| 432 | equalTo: '{0} must be the same as {1}', |
| 433 | digits: '{0} must only contain digits', |
| 434 | number: '{0} must be a number', |
| 435 | email: '{0} must be a valid email', |
| 436 | url: '{0} must be a valid url', |
| 437 | inlinePattern: '{0} is invalid' |
| 438 | }; |
| 439 | |
| 440 | // Label formatters |
| 441 | // ---------------- |
| 442 | |
| 443 | // Label formatters are used to convert the attribute name |
| 444 | // to a more human friendly label when using the built in |
| 445 | // error messages. |
| 446 | // Configure which one to use with a call to |
| 447 | // |
| 448 | // Backbone.Validation.configure({ |
| 449 | // labelFormatter: 'label' |
| 450 | // }); |
| 451 | var defaultLabelFormatters = Validation.labelFormatters = { |
| 452 | |
| 453 | // Returns the attribute name with applying any formatting |
| 454 | none: function(attrName) { |
| 455 | return attrName; |
| 456 | }, |
| 457 | |
| 458 | // Converts attributeName or attribute_name to Attribute name |
| 459 | sentenceCase: function(attrName) { |
| 460 | return attrName.replace(/(?:^\w|[A-Z]|\b\w)/g, function(match, index) { |
| 461 | return index === 0 ? match.toUpperCase() : ' ' + match.toLowerCase(); |
| 462 | }).replace(/_/g, ' '); |
| 463 | }, |
| 464 | |
| 465 | // Looks for a label configured on the model and returns it |
| 466 | // |
| 467 | // var Model = Backbone.Model.extend({ |
| 468 | // validation: { |
| 469 | // someAttribute: { |
| 470 | // required: true |
| 471 | // } |
| 472 | // }, |
| 473 | // |
| 474 | // labels: { |
| 475 | // someAttribute: 'Custom label' |
| 476 | // } |
| 477 | // }); |
| 478 | label: function(attrName, model) { |
| 479 | return (model.labels && model.labels[attrName]) || defaultLabelFormatters.sentenceCase(attrName, model); |
| 480 | } |
| 481 | }; |
| 482 | |
| 483 | |
| 484 | // Built in validators |
| 485 | // ------------------- |
| 486 | |
| 487 | var defaultValidators = Validation.validators = (function(){ |
| 488 | // Use native trim when defined |
| 489 | var trim = String.prototype.trim ? |
| 490 | function(text) { |
| 491 | return text === null ? '' : String.prototype.trim.call(text); |
| 492 | } : |
| 493 | function(text) { |
| 494 | var trimLeft = /^\s+/, |
| 495 | trimRight = /\s+$/; |
| 496 | |
| 497 | return text === null ? '' : text.toString().replace(trimLeft, '').replace(trimRight, ''); |
| 498 | }; |
| 499 | |
| 500 | // Determines whether or not a value is a number |
| 501 | var isNumber = function(value){ |
| 502 | return _.isNumber(value) || (_.isString(value) && value.match(defaultPatterns.number)); |
| 503 | }; |
| 504 | |
| 505 | // Determines whether or not a value is empty |
| 506 | var hasValue = function(value) { |
| 507 | return !(_.isNull(value) || _.isUndefined(value) || (_.isString(value) && trim(value) === '') || (_.isArray(value) && _.isEmpty(value))); |
| 508 | }; |
| 509 | |
| 510 | return { |
| 511 | // Function validator |
| 512 | // Lets you implement a custom function used for validation |
| 513 | fn: function(value, attr, fn, model, computed) { |
| 514 | if(_.isString(fn)){ |
| 515 | fn = model[fn]; |
| 516 | } |
| 517 | return fn.call(model, value, attr, computed); |
| 518 | }, |
| 519 | |
| 520 | // Required validator |
| 521 | // Validates if the attribute is required or not |
| 522 | // This can be specified as either a boolean value or a function that returns a boolean value |
| 523 | required: function(value, attr, required, model, computed) { |
| 524 | var isRequired = _.isFunction(required) ? required.call(model, value, attr, computed) : required; |
| 525 | if(!isRequired && !hasValue(value)) { |
| 526 | return false; // overrides all other validators |
| 527 | } |
| 528 | if (isRequired && !hasValue(value)) { |
| 529 | return this.format(defaultMessages.required, this.formatLabel(attr, model)); |
| 530 | } |
| 531 | }, |
| 532 | |
| 533 | // Acceptance validator |
| 534 | // Validates that something has to be accepted, e.g. terms of use |
| 535 | // `true` or 'true' are valid |
| 536 | acceptance: function(value, attr, accept, model) { |
| 537 | if(value !== 'true' && (!_.isBoolean(value) || value === false)) { |
| 538 | return this.format(defaultMessages.acceptance, this.formatLabel(attr, model)); |
| 539 | } |
| 540 | }, |
| 541 | |
| 542 | // Min validator |
| 543 | // Validates that the value has to be a number and equal to or greater than |
| 544 | // the min value specified |
| 545 | min: function(value, attr, minValue, model) { |
| 546 | if (!isNumber(value) || value < minValue) { |
| 547 | return this.format(defaultMessages.min, this.formatLabel(attr, model), minValue); |
| 548 | } |
| 549 | }, |
| 550 | |
| 551 | // Max validator |
| 552 | // Validates that the value has to be a number and equal to or less than |
| 553 | // the max value specified |
| 554 | max: function(value, attr, maxValue, model) { |
| 555 | if (!isNumber(value) || value > maxValue) { |
| 556 | return this.format(defaultMessages.max, this.formatLabel(attr, model), maxValue); |
| 557 | } |
| 558 | }, |
| 559 | |
| 560 | // Range validator |
| 561 | // Validates that the value has to be a number and equal to or between |
| 562 | // the two numbers specified |
| 563 | range: function(value, attr, range, model) { |
| 564 | if(!isNumber(value) || value < range[0] || value > range[1]) { |
| 565 | return this.format(defaultMessages.range, this.formatLabel(attr, model), range[0], range[1]); |
| 566 | } |
| 567 | }, |
| 568 | |
| 569 | // Length validator |
| 570 | // Validates that the value has to be a string with length equal to |
| 571 | // the length value specified |
| 572 | length: function(value, attr, length, model) { |
| 573 | if (!_.isString(value) || value.length !== length) { |
| 574 | return this.format(defaultMessages.length, this.formatLabel(attr, model), length); |
| 575 | } |
| 576 | }, |
| 577 | |
| 578 | // Min length validator |
| 579 | // Validates that the value has to be a string with length equal to or greater than |
| 580 | // the min length value specified |
| 581 | minLength: function(value, attr, minLength, model) { |
| 582 | if (!_.isString(value) || value.length < minLength) { |
| 583 | return this.format(defaultMessages.minLength, this.formatLabel(attr, model), minLength); |
| 584 | } |
| 585 | }, |
| 586 | |
| 587 | // Max length validator |
| 588 | // Validates that the value has to be a string with length equal to or less than |
| 589 | // the max length value specified |
| 590 | maxLength: function(value, attr, maxLength, model) { |
| 591 | if (!_.isString(value) || value.length > maxLength) { |
| 592 | return this.format(defaultMessages.maxLength, this.formatLabel(attr, model), maxLength); |
| 593 | } |
| 594 | }, |
| 595 | |
| 596 | // Range length validator |
| 597 | // Validates that the value has to be a string and equal to or between |
| 598 | // the two numbers specified |
| 599 | rangeLength: function(value, attr, range, model) { |
| 600 | if (!_.isString(value) || value.length < range[0] || value.length > range[1]) { |
| 601 | return this.format(defaultMessages.rangeLength, this.formatLabel(attr, model), range[0], range[1]); |
| 602 | } |
| 603 | }, |
| 604 | |
| 605 | // One of validator |
| 606 | // Validates that the value has to be equal to one of the elements in |
| 607 | // the specified array. Case sensitive matching |
| 608 | oneOf: function(value, attr, values, model) { |
| 609 | if(!_.include(values, value)){ |
| 610 | return this.format(defaultMessages.oneOf, this.formatLabel(attr, model), values.join(', ')); |
| 611 | } |
| 612 | }, |
| 613 | |
| 614 | // Equal to validator |
| 615 | // Validates that the value has to be equal to the value of the attribute |
| 616 | // with the name specified |
| 617 | equalTo: function(value, attr, equalTo, model, computed) { |
| 618 | if(value !== computed[equalTo]) { |
| 619 | return this.format(defaultMessages.equalTo, this.formatLabel(attr, model), this.formatLabel(equalTo, model)); |
| 620 | } |
| 621 | }, |
| 622 | |
| 623 | // Pattern validator |
| 624 | // Validates that the value has to match the pattern specified. |
| 625 | // Can be a regular expression or the name of one of the built in patterns |
| 626 | pattern: function(value, attr, pattern, model) { |
| 627 | if (!hasValue(value) || !value.toString().match(defaultPatterns[pattern] || pattern)) { |
| 628 | return this.format(defaultMessages[pattern] || defaultMessages.inlinePattern, this.formatLabel(attr, model), pattern); |
| 629 | } |
| 630 | } |
| 631 | }; |
| 632 | }()); |
| 633 | |
| 634 | // Set the correct context for all validators |
| 635 | // when used from within a method validator |
| 636 | _.each(defaultValidators, function(validator, key){ |
| 637 | defaultValidators[key] = _.bind(defaultValidators[key], _.extend({}, formatFunctions, defaultValidators)); |
| 638 | }); |
| 639 | |
| 640 | return Validation; |
| 641 | }(_)); |
| 642 | return Backbone.Validation; |
| 643 | })); |