js_stack 0.0.1

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