Revert "migrated the validate.js package to a standard asset"
[openemr.git] / library / js / vendors / validate / validate.js
blobf0823b8bfe1483813b088c137dfe71ef0ecd51b0
1 /*!
2  * validate.js 0.10.0
3  *
4  * (c) 2013-2016 Nicklas Ansman, 2013 Wrapp
5  * Validate.js may be freely distributed under the MIT license.
6  * For all details and documentation:
7  * http://validatejs.org/
8  */
10 (function(exports, module, define) {
11   "use strict";
13   // The main function that calls the validators specified by the constraints.
14   // The options are the following:
15   //   - format (string) - An option that controls how the returned value is formatted
16   //     * flat - Returns a flat array of just the error messages
17   //     * grouped - Returns the messages grouped by attribute (default)
18   //     * detailed - Returns an array of the raw validation data
19   //   - fullMessages (boolean) - If `true` (default) the attribute name is prepended to the error.
20   //
21   // Please note that the options are also passed to each validator.
22   var validate = function(attributes, constraints, options) {
23     options = v.extend({}, v.options, options);
25     var results = v.runValidations(attributes, constraints, options)
26       , attr
27       , validator;
29     for (attr in results) {
30       for (validator in results[attr]) {
31         if (v.isPromise(results[attr][validator])) {
32           throw new Error("Use validate.async if you want support for promises");
33         }
34       }
35     }
36     return validate.processValidationResults(results, options);
37   };
39   var v = validate;
41   // Copies over attributes from one or more sources to a single destination.
42   // Very much similar to underscore's extend.
43   // The first argument is the target object and the remaining arguments will be
44   // used as sources.
45   v.extend = function(obj) {
46     [].slice.call(arguments, 1).forEach(function(source) {
47       for (var attr in source) {
48         obj[attr] = source[attr];
49       }
50     });
51     return obj;
52   };
54   v.extend(validate, {
55     // This is the version of the library as a semver.
56     // The toString function will allow it to be coerced into a string
57     version: {
58       major: 0,
59       minor: 10,
60       patch: 0,
61       metadata: "development",
62       toString: function() {
63         var version = v.format("%{major}.%{minor}.%{patch}", v.version);
64         if (!v.isEmpty(v.version.metadata)) {
65           version += "+" + v.version.metadata;
66         }
67         return version;
68       }
69     },
71     // Below is the dependencies that are used in validate.js
73     // The constructor of the Promise implementation.
74     // If you are using Q.js, RSVP or any other A+ compatible implementation
75     // override this attribute to be the constructor of that promise.
76     // Since jQuery promises aren't A+ compatible they won't work.
77     Promise: typeof Promise !== "undefined" ? Promise : /* istanbul ignore next */ null,
79     EMPTY_STRING_REGEXP: /^\s*$/,
81     // Runs the validators specified by the constraints object.
82     // Will return an array of the format:
83     //     [{attribute: "<attribute name>", error: "<validation result>"}, ...]
84     runValidations: function(attributes, constraints, options) {
85       var results = []
86         , attr
87         , validatorName
88         , value
89         , validators
90         , validator
91         , validatorOptions
92         , error;
94       if (v.isDomElement(attributes) || v.isJqueryElement(attributes)) {
95         attributes = v.collectFormValues(attributes);
96       }
98       // Loops through each constraints, finds the correct validator and run it.
99       for (attr in constraints) {
100         value = v.getDeepObjectValue(attributes, attr);
101         // This allows the constraints for an attribute to be a function.
102         // The function will be called with the value, attribute name, the complete dict of
103         // attributes as well as the options and constraints passed in.
104         // This is useful when you want to have different
105         // validations depending on the attribute value.
106         validators = v.result(constraints[attr], value, attributes, attr, options, constraints);
108         for (validatorName in validators) {
109           validator = v.validators[validatorName];
111           if (!validator) {
112             error = v.format("Unknown validator %{name}", {name: validatorName});
113             throw new Error(error);
114           }
116           validatorOptions = validators[validatorName];
117           // This allows the options to be a function. The function will be
118           // called with the value, attribute name, the complete dict of
119           // attributes as well as the options and constraints passed in.
120           // This is useful when you want to have different
121           // validations depending on the attribute value.
122           validatorOptions = v.result(validatorOptions, value, attributes, attr, options, constraints);
123           if (!validatorOptions) {
124             continue;
125           }
126           results.push({
127             attribute: attr,
128             value: value,
129             validator: validatorName,
130             globalOptions: options,
131             attributes: attributes,
132             options: validatorOptions,
133             error: validator.call(validator,
134                 value,
135                 validatorOptions,
136                 attr,
137                 attributes,
138                 options)
139           });
140         }
141       }
143       return results;
144     },
146     // Takes the output from runValidations and converts it to the correct
147     // output format.
148     processValidationResults: function(errors, options) {
149       errors = v.pruneEmptyErrors(errors, options);
150       errors = v.expandMultipleErrors(errors, options);
151       errors = v.convertErrorMessages(errors, options);
153       var format = options.format || "grouped";
155       if(typeof v.formatters[format] === 'function') {
156         errors = v.formatters[format](errors);
157       }
158       else {
159         throw new Error(v.format("Unknown format %{format}", options));
160       }
162       return v.isEmpty(errors) ? undefined : errors;
163     },
165     // Runs the validations with support for promises.
166     // This function will return a promise that is settled when all the
167     // validation promises have been completed.
168     // It can be called even if no validations returned a promise.
169     async: function(attributes, constraints, options) {
170       options = v.extend({}, v.async.options, options);
172       var WrapErrors = options.wrapErrors || function(errors) {
173         return errors;
174       };
176       // Removes unknown attributes
177       if (options.cleanAttributes !== false) {
178         attributes = v.cleanAttributes(attributes, constraints);
179       }
181       var results = v.runValidations(attributes, constraints, options);
183       return new v.Promise(function(resolve, reject) {
184         v.waitForResults(results).then(function() {
185           var errors = v.processValidationResults(results, options);
186           if (errors) {
187             reject(new WrapErrors(errors, options, attributes, constraints));
188           } else {
189             resolve(attributes);
190           }
191         }, function(err) {
192           reject(err);
193         });
194       });
195     },
197     single: function(value, constraints, options) {
198       options = v.extend({}, v.single.options, options, {
199         format: "flat",
200         fullMessages: false
201       });
202       return v({single: value}, {single: constraints}, options);
203     },
205     // Returns a promise that is resolved when all promises in the results array
206     // are settled. The promise returned from this function is always resolved,
207     // never rejected.
208     // This function modifies the input argument, it replaces the promises
209     // with the value returned from the promise.
210     waitForResults: function(results) {
211       // Create a sequence of all the results starting with a resolved promise.
212       return results.reduce(function(memo, result) {
213         // If this result isn't a promise skip it in the sequence.
214         if (!v.isPromise(result.error)) {
215           return memo;
216         }
218         return memo.then(function() {
219           return result.error.then(function(error) {
220             result.error = error || null;
221           });
222         });
223       }, new v.Promise(function(r) { r(); })); // A resolved promise
224     },
226     // If the given argument is a call: function the and: function return the value
227     // otherwise just return the value. Additional arguments will be passed as
228     // arguments to the function.
229     // Example:
230     // ```
231     // result('foo') // 'foo'
232     // result(Math.max, 1, 2) // 2
233     // ```
234     result: function(value) {
235       var args = [].slice.call(arguments, 1);
236       if (typeof value === 'function') {
237         value = value.apply(null, args);
238       }
239       return value;
240     },
242     // Checks if the value is a number. This function does not consider NaN a
243     // number like many other `isNumber` functions do.
244     isNumber: function(value) {
245       return typeof value === 'number' && !isNaN(value);
246     },
248     // Returns false if the object is not a function
249     isFunction: function(value) {
250       return typeof value === 'function';
251     },
253     // A simple check to verify that the value is an integer. Uses `isNumber`
254     // and a simple modulo check.
255     isInteger: function(value) {
256       return v.isNumber(value) && value % 1 === 0;
257     },
259     // Checks if the value is a boolean
260     isBoolean: function(value) {
261       return typeof value === 'boolean';
262     },
264     // Uses the `Object` function to check if the given argument is an object.
265     isObject: function(obj) {
266       return obj === Object(obj);
267     },
269     // Simply checks if the object is an instance of a date
270     isDate: function(obj) {
271       return obj instanceof Date;
272     },
274     // Returns false if the object is `null` of `undefined`
275     isDefined: function(obj) {
276       return obj !== null && obj !== undefined;
277     },
279     // Checks if the given argument is a promise. Anything with a `then`
280     // function is considered a promise.
281     isPromise: function(p) {
282       return !!p && v.isFunction(p.then);
283     },
285     isJqueryElement: function(o) {
286       return o && v.isString(o.jquery);
287     },
289     isDomElement: function(o) {
290       if (!o) {
291         return false;
292       }
294       if (!o.querySelectorAll || !o.querySelector) {
295         return false;
296       }
298       if (v.isObject(document) && o === document) {
299         return true;
300       }
302       // http://stackoverflow.com/a/384380/699304
303       /* istanbul ignore else */
304       if (typeof HTMLElement === "object") {
305         return o instanceof HTMLElement;
306       } else {
307         return o &&
308           typeof o === "object" &&
309           o !== null &&
310           o.nodeType === 1 &&
311           typeof o.nodeName === "string";
312       }
313     },
315     isEmpty: function(value) {
316       var attr;
318       // Null and undefined are empty
319       if (!v.isDefined(value)) {
320         return true;
321       }
323       // functions are non empty
324       if (v.isFunction(value)) {
325         return false;
326       }
328       // Whitespace only strings are empty
329       if (v.isString(value)) {
330         return v.EMPTY_STRING_REGEXP.test(value);
331       }
333       // For arrays we use the length property
334       if (v.isArray(value)) {
335         return value.length === 0;
336       }
338       // Dates have no attributes but aren't empty
339       if (v.isDate(value)) {
340         return false;
341       }
343       // If we find at least one property we consider it non empty
344       if (v.isObject(value)) {
345         for (attr in value) {
346           return false;
347         }
348         return true;
349       }
351       return false;
352     },
354     // Formats the specified strings with the given values like so:
355     // ```
356     // format("Foo: %{foo}", {foo: "bar"}) // "Foo bar"
357     // ```
358     // If you want to write %{...} without having it replaced simply
359     // prefix it with % like this `Foo: %%{foo}` and it will be returned
360     // as `"Foo: %{foo}"`
361     format: v.extend(function(str, vals) {
362       if (!v.isString(str)) {
363         return str;
364       }
365       return str.replace(v.format.FORMAT_REGEXP, function(m0, m1, m2) {
366         if (m1 === '%') {
367           return "%{" + m2 + "}";
368         } else {
369           return String(vals[m2]);
370         }
371       });
372     }, {
373       // Finds %{key} style patterns in the given string
374       FORMAT_REGEXP: /(%?)%\{([^\}]+)\}/g
375     }),
377     // "Prettifies" the given string.
378     // Prettifying means replacing [.\_-] with spaces as well as splitting
379     // camel case words.
380     prettify: function(str) {
381       if (v.isNumber(str)) {
382         // If there are more than 2 decimals round it to two
383         if ((str * 100) % 1 === 0) {
384           return "" + str;
385         } else {
386           return parseFloat(Math.round(str * 100) / 100).toFixed(2);
387         }
388       }
390       if (v.isArray(str)) {
391         return str.map(function(s) { return v.prettify(s); }).join(", ");
392       }
394       if (v.isObject(str)) {
395         return str.toString();
396       }
398       // Ensure the string is actually a string
399       str = "" + str;
401       return str
402         // Splits keys separated by periods
403         .replace(/([^\s])\.([^\s])/g, '$1 $2')
404         // Removes backslashes
405         .replace(/\\+/g, '')
406         // Replaces - and - with space
407         .replace(/[_-]/g, ' ')
408         // Splits camel cased words
409         .replace(/([a-z])([A-Z])/g, function(m0, m1, m2) {
410           return "" + m1 + " " + m2.toLowerCase();
411         })
412         .toLowerCase();
413     },
415     stringifyValue: function(value) {
416       return v.prettify(value);
417     },
419     isString: function(value) {
420       return typeof value === 'string';
421     },
423     isArray: function(value) {
424       return {}.toString.call(value) === '[object Array]';
425     },
427     // Checks if the object is a hash, which is equivalent to an object that
428     // is neither an array nor a function.
429     isHash: function(value) {
430       return v.isObject(value) && !v.isArray(value) && !v.isFunction(value);
431     },
433     contains: function(obj, value) {
434       if (!v.isDefined(obj)) {
435         return false;
436       }
437       if (v.isArray(obj)) {
438         return obj.indexOf(value) !== -1;
439       }
440       return value in obj;
441     },
443     unique: function(array) {
444       if (!v.isArray(array)) {
445         return array;
446       }
447       return array.filter(function(el, index, array) {
448         return array.indexOf(el) == index;
449       });
450     },
452     forEachKeyInKeypath: function(object, keypath, callback) {
453       if (!v.isString(keypath)) {
454         return undefined;
455       }
457       var key = ""
458         , i
459         , escape = false;
461       for (i = 0; i < keypath.length; ++i) {
462         switch (keypath[i]) {
463           case '.':
464             if (escape) {
465               escape = false;
466               key += '.';
467             } else {
468               object = callback(object, key, false);
469               key = "";
470             }
471             break;
473           case '\\':
474             if (escape) {
475               escape = false;
476               key += '\\';
477             } else {
478               escape = true;
479             }
480             break;
482           default:
483             escape = false;
484             key += keypath[i];
485             break;
486         }
487       }
489       return callback(object, key, true);
490     },
492     getDeepObjectValue: function(obj, keypath) {
493       if (!v.isObject(obj)) {
494         return undefined;
495       }
497       return v.forEachKeyInKeypath(obj, keypath, function(obj, key) {
498         if (v.isObject(obj)) {
499           return obj[key];
500         }
501       });
502     },
504     // This returns an object with all the values of the form.
505     // It uses the input name as key and the value as value
506     // So for example this:
507     // <input type="text" name="email" value="foo@bar.com" />
508     // would return:
509     // {email: "foo@bar.com"}
510     collectFormValues: function(form, options) {
511       var values = {}
512         , i
513         , j
514         , input
515         , inputs
516         , option
517         , value;
519       if (v.isJqueryElement(form)) {
520         form = form[0];
521       }
523       if (!form) {
524         return values;
525       }
527       options = options || {};
529       inputs = form.querySelectorAll("input[name], textarea[name]");
530       for (i = 0; i < inputs.length; ++i) {
531         input = inputs.item(i);
533         if (v.isDefined(input.getAttribute("data-ignored"))) {
534           continue;
535         }
537         value = v.sanitizeFormValue(input.value, options);
538         if (input.type === "number") {
539           value = value ? +value : null;
540         } else if (input.type === "checkbox") {
541           if (input.attributes.value) {
542             if (!input.checked) {
543               value = values[input.name] || null;
544             }
545           } else {
546             value = input.checked;
547           }
548         } else if (input.type === "radio") {
549           if (!input.checked) {
550             value = values[input.name] || null;
551           }
552         }
553         values[input.name] = value;
554       }
556       inputs = form.querySelectorAll("select[name]");
557       for (i = 0; i < inputs.length; ++i) {
558         input = inputs.item(i);
559         if (input.multiple) {
560           value = [];
562           for (j in input.options) {
563             option = input.options[j];
564             if (option.selected) {
565               value.push(v.sanitizeFormValue(option.value, options));
566             }
567           }
568         } else {
569           value = v.sanitizeFormValue(input.options[input.selectedIndex].value, options);
570         }
571         values[input.name] = value;
572       }
574       return values;
575     },
577     sanitizeFormValue: function(value, options) {
578       if (options.trim && v.isString(value)) {
579         value = value.trim();
580       }
582       if (options.nullify !== false && value === "") {
583         return null;
584       }
585       return value;
586     },
588     capitalize: function(str) {
589       if (!v.isString(str)) {
590         return str;
591       }
592       return str[0].toUpperCase() + str.slice(1);
593     },
595     // Remove all errors who's error attribute is empty (null or undefined)
596     pruneEmptyErrors: function(errors) {
597       return errors.filter(function(error) {
598         return !v.isEmpty(error.error);
599       });
600     },
602     // In
603     // [{error: ["err1", "err2"], ...}]
604     // Out
605     // [{error: "err1", ...}, {error: "err2", ...}]
606     //
607     // All attributes in an error with multiple messages are duplicated
608     // when expanding the errors.
609     expandMultipleErrors: function(errors) {
610       var ret = [];
611       errors.forEach(function(error) {
612         // Removes errors without a message
613         if (v.isArray(error.error)) {
614           error.error.forEach(function(msg) {
615             ret.push(v.extend({}, error, {error: msg}));
616           });
617         } else {
618           ret.push(error);
619         }
620       });
621       return ret;
622     },
624     // Converts the error mesages by prepending the attribute name unless the
625     // message is prefixed by ^
626     convertErrorMessages: function(errors, options) {
627       options = options || {};
629       var ret = [];
630       errors.forEach(function(errorInfo) {
631         var error = v.result(errorInfo.error,
632             errorInfo.value,
633             errorInfo.attribute,
634             errorInfo.options,
635             errorInfo.attributes,
636             errorInfo.globalOptions);
638         if (!v.isString(error)) {
639           ret.push(errorInfo);
640           return;
641         }
643         if (error[0] === '^') {
644           error = error.slice(1);
645         } else if (options.fullMessages !== false) {
646           error = v.capitalize(v.prettify(errorInfo.attribute)) + " " + error;
647         }
648         error = error.replace(/\\\^/g, "^");
649         error = v.format(error, {value: v.stringifyValue(errorInfo.value)});
650         ret.push(v.extend({}, errorInfo, {error: error}));
651       });
652       return ret;
653     },
655     // In:
656     // [{attribute: "<attributeName>", ...}]
657     // Out:
658     // {"<attributeName>": [{attribute: "<attributeName>", ...}]}
659     groupErrorsByAttribute: function(errors) {
660       var ret = {};
661       errors.forEach(function(error) {
662         var list = ret[error.attribute];
663         if (list) {
664           list.push(error);
665         } else {
666           ret[error.attribute] = [error];
667         }
668       });
669       return ret;
670     },
672     // In:
673     // [{error: "<message 1>", ...}, {error: "<message 2>", ...}]
674     // Out:
675     // ["<message 1>", "<message 2>"]
676     flattenErrorsToArray: function(errors) {
677       return errors.map(function(error) { return error.error; });
678     },
680     cleanAttributes: function(attributes, whitelist) {
681       function whitelistCreator(obj, key, last) {
682         if (v.isObject(obj[key])) {
683           return obj[key];
684         }
685         return (obj[key] = last ? true : {});
686       }
688       function buildObjectWhitelist(whitelist) {
689         var ow = {}
690           , lastObject
691           , attr;
692         for (attr in whitelist) {
693           if (!whitelist[attr]) {
694             continue;
695           }
696           v.forEachKeyInKeypath(ow, attr, whitelistCreator);
697         }
698         return ow;
699       }
701       function cleanRecursive(attributes, whitelist) {
702         if (!v.isObject(attributes)) {
703           return attributes;
704         }
706         var ret = v.extend({}, attributes)
707           , w
708           , attribute;
710         for (attribute in attributes) {
711           w = whitelist[attribute];
713           if (v.isObject(w)) {
714             ret[attribute] = cleanRecursive(ret[attribute], w);
715           } else if (!w) {
716             delete ret[attribute];
717           }
718         }
719         return ret;
720       }
722       if (!v.isObject(whitelist) || !v.isObject(attributes)) {
723         return {};
724       }
726       whitelist = buildObjectWhitelist(whitelist);
727       return cleanRecursive(attributes, whitelist);
728     },
730     exposeModule: function(validate, root, exports, module, define) {
731       if (exports) {
732         if (module && module.exports) {
733           exports = module.exports = validate;
734         }
735         exports.validate = validate;
736       } else {
737         root.validate = validate;
738         if (validate.isFunction(define) && define.amd) {
739           define([], function () { return validate; });
740         }
741       }
742     },
744     warn: function(msg) {
745       if (typeof console !== "undefined" && console.warn) {
746         console.warn("[validate.js] " + msg);
747       }
748     },
750     error: function(msg) {
751       if (typeof console !== "undefined" && console.error) {
752         console.error("[validate.js] " + msg);
753       }
754     }
755   });
757   validate.validators = {
758     // Presence validates that the value isn't empty
759     presence: function(value, options) {
760       options = v.extend({}, this.options, options);
761       if (v.isEmpty(value)) {
762         return options.message || this.message || "can't be blank";
763       }
764     },
765     length: function(value, options, attribute) {
766       // Empty values are allowed
767       if (v.isEmpty(value)) {
768         return;
769       }
771       options = v.extend({}, this.options, options);
773       var is = options.is
774         , maximum = options.maximum
775         , minimum = options.minimum
776         , tokenizer = options.tokenizer || function(val) { return val; }
777         , err
778         , errors = [];
780       value = tokenizer(value);
781       var length = value.length;
782       if(!v.isNumber(length)) {
783         v.error(v.format("Attribute %{attr} has a non numeric value for `length`", {attr: attribute}));
784         return options.message || this.notValid || "has an incorrect length";
785       }
787       // Is checks
788       if (v.isNumber(is) && length !== is) {
789         err = options.wrongLength ||
790           this.wrongLength ||
791           "is the wrong length (should be %{count} characters)";
792         errors.push(v.format(err, {count: is}));
793       }
795       if (v.isNumber(minimum) && length < minimum) {
796         err = options.tooShort ||
797           this.tooShort ||
798           "is too short (minimum is %{count} characters)";
799         errors.push(v.format(err, {count: minimum}));
800       }
802       if (v.isNumber(maximum) && length > maximum) {
803         err = options.tooLong ||
804           this.tooLong ||
805           "is too long (maximum is %{count} characters)";
806         errors.push(v.format(err, {count: maximum}));
807       }
809       if (errors.length > 0) {
810         return options.message || errors;
811       }
812     },
813     numericality: function(value, options) {
814       // Empty values are fine
815       if (v.isEmpty(value)) {
816         return;
817       }
819       options = v.extend({}, this.options, options);
821       var errors = []
822         , name
823         , count
824         , checks = {
825             greaterThan:          function(v, c) { return v > c; },
826             greaterThanOrEqualTo: function(v, c) { return v >= c; },
827             equalTo:              function(v, c) { return v === c; },
828             lessThan:             function(v, c) { return v < c; },
829             lessThanOrEqualTo:    function(v, c) { return v <= c; },
830             divisibleBy:          function(v, c) { return v % c === 0; }
831           };
833       // Strict will check that it is a valid looking number
834       if (v.isString(value) && options.strict) {
835         var pattern = "^(0|[1-9]\\d*)";
836         if (!options.onlyInteger) {
837           pattern += "(\\.\\d+)?";
838         }
839         pattern += "$";
841         if (!(new RegExp(pattern).test(value))) {
842           return options.message || options.notValid || this.notValid || "must be a valid number";
843         }
844       }
846       // Coerce the value to a number unless we're being strict.
847       if (options.noStrings !== true && v.isString(value)) {
848         value = +value;
849       }
851       // If it's not a number we shouldn't continue since it will compare it.
852       if (!v.isNumber(value)) {
853         return options.message || options.notValid || this.notValid || "is not a number";
854       }
856       // Same logic as above, sort of. Don't bother with comparisons if this
857       // doesn't pass.
858       if (options.onlyInteger && !v.isInteger(value)) {
859         return options.message || options.notInteger || this.notInteger  || "must be an integer";
860       }
862       for (name in checks) {
863         count = options[name];
864         if (v.isNumber(count) && !checks[name](value, count)) {
865           // This picks the default message if specified
866           // For example the greaterThan check uses the message from
867           // this.notGreaterThan so we capitalize the name and prepend "not"
868           var key = "not" + v.capitalize(name);
869           var msg = options[key] || this[key] || "must be %{type} %{count}";
871           errors.push(v.format(msg, {
872             count: count,
873             type: v.prettify(name)
874           }));
875         }
876       }
878       if (options.odd && value % 2 !== 1) {
879         errors.push(options.notOdd || this.notOdd || "must be odd");
880       }
881       if (options.even && value % 2 !== 0) {
882         errors.push(options.notEven || this.notEven || "must be even");
883       }
885       if (errors.length) {
886         return options.message || errors;
887       }
888     },
889     datetime: v.extend(function(value, options) {
890       if (!v.isFunction(this.parse) || !v.isFunction(this.format)) {
891         throw new Error("Both the parse and format functions needs to be set to use the datetime/date validator");
892       }
894       // Empty values are fine
895       if (v.isEmpty(value)) {
896         return;
897       }
899       options = v.extend({}, this.options, options);
901       var err
902         , errors = []
903         , earliest = options.earliest ? this.parse(options.earliest, options) : NaN
904         , latest = options.latest ? this.parse(options.latest, options) : NaN;
906       value = this.parse(value, options);
908       // 86400000 is the number of seconds in a day, this is used to remove
909       // the time from the date
910       if (isNaN(value) || options.dateOnly && value % 86400000 !== 0) {
911         err = options.notValid ||
912           options.message ||
913           this.notValid ||
914           "must be a valid date";
915         return v.format(err, {value: arguments[0]});
916       }
918       if (!isNaN(earliest) && value < earliest) {
919         err = options.tooEarly ||
920           options.message ||
921           this.tooEarly ||
922           "must be no earlier than %{date}";
923         err = v.format(err, {
924           value: this.format(value, options),
925           date: this.format(earliest, options)
926         });
927         errors.push(err);
928       }
930       if (!isNaN(latest) && value > latest) {
931         err = options.tooLate ||
932           options.message ||
933           this.tooLate ||
934           "must be no later than %{date}";
935         err = v.format(err, {
936           date: this.format(latest, options),
937           value: this.format(value, options)
938         });
939         errors.push(err);
940       }
942       if (errors.length) {
943         return v.unique(errors);
944       }
945     }, {
946       parse: null,
947       format: null
948     }),
949     date: function(value, options) {
950       options = v.extend({}, options, {dateOnly: true});
951       return v.validators.datetime.call(v.validators.datetime, value, options);
952     },
953     format: function(value, options) {
954       if (v.isString(options) || (options instanceof RegExp)) {
955         options = {pattern: options};
956       }
958       options = v.extend({}, this.options, options);
960       var message = options.message || this.message || "is invalid"
961         , pattern = options.pattern
962         , match;
964       // Empty values are allowed
965       if (v.isEmpty(value)) {
966         return;
967       }
968       if (!v.isString(value)) {
969         return message;
970       }
972       if (v.isString(pattern)) {
973         pattern = new RegExp(options.pattern, options.flags);
974       }
975       match = pattern.exec(value);
976       if (!match || match[0].length != value.length) {
977         return message;
978       }
979     },
980     inclusion: function(value, options) {
981       // Empty values are fine
982       if (v.isEmpty(value)) {
983         return;
984       }
985       if (v.isArray(options)) {
986         options = {within: options};
987       }
988       options = v.extend({}, this.options, options);
989       if (v.contains(options.within, value)) {
990         return;
991       }
992       var message = options.message ||
993         this.message ||
994         "^%{value} is not included in the list";
995       return v.format(message, {value: value});
996     },
997     exclusion: function(value, options) {
998       // Empty values are fine
999       if (v.isEmpty(value)) {
1000         return;
1001       }
1002       if (v.isArray(options)) {
1003         options = {within: options};
1004       }
1005       options = v.extend({}, this.options, options);
1006       if (!v.contains(options.within, value)) {
1007         return;
1008       }
1009       var message = options.message || this.message || "^%{value} is restricted";
1010       return v.format(message, {value: value});
1011     },
1012     email: v.extend(function(value, options) {
1013       options = v.extend({}, this.options, options);
1014       var message = options.message || this.message || "is not a valid email";
1015       // Empty values are fine
1016       if (v.isEmpty(value)) {
1017         return;
1018       }
1019       if (!v.isString(value)) {
1020         return message;
1021       }
1022       if (!this.PATTERN.exec(value)) {
1023         return message;
1024       }
1025     }, {
1026       PATTERN: /^[a-z0-9\u007F-\uffff!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9\u007F-\uffff!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/i
1027     }),
1028     equality: function(value, options, attribute, attributes) {
1029       if (v.isEmpty(value)) {
1030         return;
1031       }
1033       if (v.isString(options)) {
1034         options = {attribute: options};
1035       }
1036       options = v.extend({}, this.options, options);
1037       var message = options.message ||
1038         this.message ||
1039         "is not equal to %{attribute}";
1041       if (v.isEmpty(options.attribute) || !v.isString(options.attribute)) {
1042         throw new Error("The attribute must be a non empty string");
1043       }
1045       var otherValue = v.getDeepObjectValue(attributes, options.attribute)
1046         , comparator = options.comparator || function(v1, v2) {
1047           return v1 === v2;
1048         };
1050       if (!comparator(value, otherValue, options, attribute, attributes)) {
1051         return v.format(message, {attribute: v.prettify(options.attribute)});
1052       }
1053     },
1055     // A URL validator that is used to validate URLs with the ability to
1056     // restrict schemes and some domains.
1057     url: function(value, options) {
1058       if (v.isEmpty(value)) {
1059         return;
1060       }
1062       options = v.extend({}, this.options, options);
1064       var message = options.message || this.message || "is not a valid url"
1065         , schemes = options.schemes || this.schemes || ['http', 'https']
1066         , allowLocal = options.allowLocal || this.allowLocal || false;
1068       if (!v.isString(value)) {
1069         return message;
1070       }
1072       // https://gist.github.com/dperini/729294
1073       var regex =
1074         "^" +
1075           // schemes
1076           "(?:(?:" + schemes.join("|") + "):\\/\\/)" +
1077           // credentials
1078           "(?:\\S+(?::\\S*)?@)?";
1080       regex += "(?:";
1082       var tld = "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))";
1084       // This ia a special case for the localhost hostname
1085       if (allowLocal) {
1086         tld += "?";
1087       } else {
1088         // private & local addresses
1089         regex +=
1090           "(?!10(?:\\.\\d{1,3}){3})" +
1091           "(?!127(?:\\.\\d{1,3}){3})" +
1092           "(?!169\\.254(?:\\.\\d{1,3}){2})" +
1093           "(?!192\\.168(?:\\.\\d{1,3}){2})" +
1094           "(?!172" +
1095           "\\.(?:1[6-9]|2\\d|3[0-1])" +
1096           "(?:\\.\\d{1,3})" +
1097           "{2})";
1098       }
1100       var hostname =
1101           "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
1102           "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
1103           tld + ")";
1105       // reserved addresses
1106       regex +=
1107           "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
1108           "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
1109           "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
1110         "|" +
1111           hostname +
1112           // port number
1113           "(?::\\d{2,5})?" +
1114           // path
1115           "(?:\\/[^\\s]*)?" +
1116         "$";
1118       var PATTERN = new RegExp(regex, 'i');
1119       if (!PATTERN.exec(value)) {
1120         return message;
1121       }
1122     }
1123   };
1125   validate.formatters = {
1126     detailed: function(errors) {return errors;},
1127     flat: v.flattenErrorsToArray,
1128     grouped: function(errors) {
1129       var attr;
1131       errors = v.groupErrorsByAttribute(errors);
1132       for (attr in errors) {
1133         errors[attr] = v.flattenErrorsToArray(errors[attr]);
1134       }
1135       return errors;
1136     }
1137   };
1139   validate.exposeModule(validate, this, exports, module, define);
1140 }).call(this,
1141         typeof exports !== 'undefined' ? /* istanbul ignore next */ exports : null,
1142         typeof module !== 'undefined' ? /* istanbul ignore next */ module : null,
1143         typeof define !== 'undefined' ? /* istanbul ignore next */ define : null);