Brought in bootstrap-rtl and validate.js packages via bower for Matrix's patient...
[openemr.git] / public / assets / validate.js-0-10-0 / validate.js
blob0c79d60cab5b9a7d182068e354881cd3a71f0cab
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: null,
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       var attr;
151       errors = v.pruneEmptyErrors(errors, options);
152       errors = v.expandMultipleErrors(errors, options);
153       errors = v.convertErrorMessages(errors, options);
155       switch (options.format || "grouped") {
156         case "detailed":
157           // Do nothing more to the errors
158           break;
160         case "flat":
161           errors = v.flattenErrorsToArray(errors);
162           break;
164         case "grouped":
165           errors = v.groupErrorsByAttribute(errors);
166           for (attr in errors) {
167             errors[attr] = v.flattenErrorsToArray(errors[attr]);
168           }
169           break;
171         default:
172           throw new Error(v.format("Unknown format %{format}", options));
173       }
175       return v.isEmpty(errors) ? undefined : errors;
176     },
178     // Runs the validations with support for promises.
179     // This function will return a promise that is settled when all the
180     // validation promises have been completed.
181     // It can be called even if no validations returned a promise.
182     async: function(attributes, constraints, options) {
183       options = v.extend({}, v.async.options, options);
185       var WrapErrors = options.wrapErrors || function(errors) {
186         return errors;
187       };
189       // Removes unknown attributes
190       if (options.cleanAttributes !== false) {
191         attributes = v.cleanAttributes(attributes, constraints);
192       }
194       var results = v.runValidations(attributes, constraints, options);
196       return new v.Promise(function(resolve, reject) {
197         v.waitForResults(results).then(function() {
198           var errors = v.processValidationResults(results, options);
199           if (errors) {
200             reject(new WrapErrors(errors, options, attributes, constraints));
201           } else {
202             resolve(attributes);
203           }
204         }, function(err) {
205           reject(err);
206         });
207       });
208     },
210     single: function(value, constraints, options) {
211       options = v.extend({}, v.single.options, options, {
212         format: "flat",
213         fullMessages: false
214       });
215       return v({single: value}, {single: constraints}, options);
216     },
218     // Returns a promise that is resolved when all promises in the results array
219     // are settled. The promise returned from this function is always resolved,
220     // never rejected.
221     // This function modifies the input argument, it replaces the promises
222     // with the value returned from the promise.
223     waitForResults: function(results) {
224       // Create a sequence of all the results starting with a resolved promise.
225       return results.reduce(function(memo, result) {
226         // If this result isn't a promise skip it in the sequence.
227         if (!v.isPromise(result.error)) {
228           return memo;
229         }
231         return memo.then(function() {
232           return result.error.then(
233             function(error) {
234               result.error = error || null;
235             },
236             function(error) {
237               if (error instanceof Error) {
238                 throw error;
239               }
240               v.error("Rejecting promises with the result is deprecated. Please use the resolve callback instead.");
241               result.error = error;
242             }
243           );
244         });
245       }, new v.Promise(function(r) { r(); })); // A resolved promise
246     },
248     // If the given argument is a call: function the and: function return the value
249     // otherwise just return the value. Additional arguments will be passed as
250     // arguments to the function.
251     // Example:
252     // ```
253     // result('foo') // 'foo'
254     // result(Math.max, 1, 2) // 2
255     // ```
256     result: function(value) {
257       var args = [].slice.call(arguments, 1);
258       if (typeof value === 'function') {
259         value = value.apply(null, args);
260       }
261       return value;
262     },
264     // Checks if the value is a number. This function does not consider NaN a
265     // number like many other `isNumber` functions do.
266     isNumber: function(value) {
267       return typeof value === 'number' && !isNaN(value);
268     },
270     // Returns false if the object is not a function
271     isFunction: function(value) {
272       return typeof value === 'function';
273     },
275     // A simple check to verify that the value is an integer. Uses `isNumber`
276     // and a simple modulo check.
277     isInteger: function(value) {
278       return v.isNumber(value) && value % 1 === 0;
279     },
281     // Checks if the value is a boolean
282     isBoolean: function(value) {
283       return typeof value === 'boolean';
284     },
286     // Uses the `Object` function to check if the given argument is an object.
287     isObject: function(obj) {
288       return obj === Object(obj);
289     },
291     // Simply checks if the object is an instance of a date
292     isDate: function(obj) {
293       return obj instanceof Date;
294     },
296     // Returns false if the object is `null` of `undefined`
297     isDefined: function(obj) {
298       return obj !== null && obj !== undefined;
299     },
301     // Checks if the given argument is a promise. Anything with a `then`
302     // function is considered a promise.
303     isPromise: function(p) {
304       return !!p && v.isFunction(p.then);
305     },
307     isJqueryElement: function(o) {
308       return o && v.isString(o.jquery);
309     },
311     isDomElement: function(o) {
312       if (!o) {
313         return false;
314       }
316       if (!o.querySelectorAll || !o.querySelector) {
317         return false;
318       }
320       if (v.isObject(document) && o === document) {
321         return true;
322       }
324       // http://stackoverflow.com/a/384380/699304
325       /* istanbul ignore else */
326       if (typeof HTMLElement === "object") {
327         return o instanceof HTMLElement;
328       } else {
329         return o &&
330           typeof o === "object" &&
331           o !== null &&
332           o.nodeType === 1 &&
333           typeof o.nodeName === "string";
334       }
335     },
337     isEmpty: function(value) {
338       var attr;
340       // Null and undefined are empty
341       if (!v.isDefined(value)) {
342         return true;
343       }
345       // functions are non empty
346       if (v.isFunction(value)) {
347         return false;
348       }
350       // Whitespace only strings are empty
351       if (v.isString(value)) {
352         return v.EMPTY_STRING_REGEXP.test(value);
353       }
355       // For arrays we use the length property
356       if (v.isArray(value)) {
357         return value.length === 0;
358       }
360       // Dates have no attributes but aren't empty
361       if (v.isDate(value)) {
362         return false;
363       }
365       // If we find at least one property we consider it non empty
366       if (v.isObject(value)) {
367         for (attr in value) {
368           return false;
369         }
370         return true;
371       }
373       return false;
374     },
376     // Formats the specified strings with the given values like so:
377     // ```
378     // format("Foo: %{foo}", {foo: "bar"}) // "Foo bar"
379     // ```
380     // If you want to write %{...} without having it replaced simply
381     // prefix it with % like this `Foo: %%{foo}` and it will be returned
382     // as `"Foo: %{foo}"`
383     format: v.extend(function(str, vals) {
384       if (!v.isString(str)) {
385         return str;
386       }
387       return str.replace(v.format.FORMAT_REGEXP, function(m0, m1, m2) {
388         if (m1 === '%') {
389           return "%{" + m2 + "}";
390         } else {
391           return String(vals[m2]);
392         }
393       });
394     }, {
395       // Finds %{key} style patterns in the given string
396       FORMAT_REGEXP: /(%?)%\{([^\}]+)\}/g
397     }),
399     // "Prettifies" the given string.
400     // Prettifying means replacing [.\_-] with spaces as well as splitting
401     // camel case words.
402     prettify: function(str) {
403       if (v.isNumber(str)) {
404         // If there are more than 2 decimals round it to two
405         if ((str * 100) % 1 === 0) {
406           return "" + str;
407         } else {
408           return parseFloat(Math.round(str * 100) / 100).toFixed(2);
409         }
410       }
412       if (v.isArray(str)) {
413         return str.map(function(s) { return v.prettify(s); }).join(", ");
414       }
416       if (v.isObject(str)) {
417         return str.toString();
418       }
420       // Ensure the string is actually a string
421       str = "" + str;
423       return str
424         // Splits keys separated by periods
425         .replace(/([^\s])\.([^\s])/g, '$1 $2')
426         // Removes backslashes
427         .replace(/\\+/g, '')
428         // Replaces - and - with space
429         .replace(/[_-]/g, ' ')
430         // Splits camel cased words
431         .replace(/([a-z])([A-Z])/g, function(m0, m1, m2) {
432           return "" + m1 + " " + m2.toLowerCase();
433         })
434         .toLowerCase();
435     },
437     stringifyValue: function(value) {
438       return v.prettify(value);
439     },
441     isString: function(value) {
442       return typeof value === 'string';
443     },
445     isArray: function(value) {
446       return {}.toString.call(value) === '[object Array]';
447     },
449     // Checks if the object is a hash, which is equivalent to an object that
450     // is neither an array nor a function.
451     isHash: function(value) {
452       return v.isObject(value) && !v.isArray(value) && !v.isFunction(value);
453     },
455     contains: function(obj, value) {
456       if (!v.isDefined(obj)) {
457         return false;
458       }
459       if (v.isArray(obj)) {
460         return obj.indexOf(value) !== -1;
461       }
462       return value in obj;
463     },
465     unique: function(array) {
466       if (!v.isArray(array)) {
467         return array;
468       }
469       return array.filter(function(el, index, array) {
470         return array.indexOf(el) == index;
471       });
472     },
474     forEachKeyInKeypath: function(object, keypath, callback) {
475       if (!v.isString(keypath)) {
476         return undefined;
477       }
479       var key = ""
480         , i
481         , escape = false;
483       for (i = 0; i < keypath.length; ++i) {
484         switch (keypath[i]) {
485           case '.':
486             if (escape) {
487               escape = false;
488               key += '.';
489             } else {
490               object = callback(object, key, false);
491               key = "";
492             }
493             break;
495           case '\\':
496             if (escape) {
497               escape = false;
498               key += '\\';
499             } else {
500               escape = true;
501             }
502             break;
504           default:
505             escape = false;
506             key += keypath[i];
507             break;
508         }
509       }
511       return callback(object, key, true);
512     },
514     getDeepObjectValue: function(obj, keypath) {
515       if (!v.isObject(obj)) {
516         return undefined;
517       }
519       return v.forEachKeyInKeypath(obj, keypath, function(obj, key) {
520         if (v.isObject(obj)) {
521           return obj[key];
522         }
523       });
524     },
526     // This returns an object with all the values of the form.
527     // It uses the input name as key and the value as value
528     // So for example this:
529     // <input type="text" name="email" value="foo@bar.com" />
530     // would return:
531     // {email: "foo@bar.com"}
532     collectFormValues: function(form, options) {
533       var values = {}
534         , i
535         , input
536         , inputs
537         , value;
539       if (v.isJqueryElement(form)) {
540         form = form[0];
541       }
543       if (!form) {
544         return values;
545       }
547       options = options || {};
549       inputs = form.querySelectorAll("input[name], textarea[name]");
550       for (i = 0; i < inputs.length; ++i) {
551         input = inputs.item(i);
553         if (v.isDefined(input.getAttribute("data-ignored"))) {
554           continue;
555         }
557         value = v.sanitizeFormValue(input.value, options);
558         if (input.type === "number") {
559           value = value ? +value : null;
560         } else if (input.type === "checkbox") {
561           if (input.attributes.value) {
562             if (!input.checked) {
563               value = values[input.name] || null;
564             }
565           } else {
566             value = input.checked;
567           }
568         } else if (input.type === "radio") {
569           if (!input.checked) {
570             value = values[input.name] || null;
571           }
572         }
573         values[input.name] = value;
574       }
576       inputs = form.querySelectorAll("select[name]");
577       for (i = 0; i < inputs.length; ++i) {
578         input = inputs.item(i);
579         value = v.sanitizeFormValue(input.options[input.selectedIndex].value, options);
580         values[input.name] = value;
581       }
583       return values;
584     },
586     sanitizeFormValue: function(value, options) {
587       if (options.trim && v.isString(value)) {
588         value = value.trim();
589       }
591       if (options.nullify !== false && value === "") {
592         return null;
593       }
594       return value;
595     },
597     capitalize: function(str) {
598       if (!v.isString(str)) {
599         return str;
600       }
601       return str[0].toUpperCase() + str.slice(1);
602     },
604     // Remove all errors who's error attribute is empty (null or undefined)
605     pruneEmptyErrors: function(errors) {
606       return errors.filter(function(error) {
607         return !v.isEmpty(error.error);
608       });
609     },
611     // In
612     // [{error: ["err1", "err2"], ...}]
613     // Out
614     // [{error: "err1", ...}, {error: "err2", ...}]
615     //
616     // All attributes in an error with multiple messages are duplicated
617     // when expanding the errors.
618     expandMultipleErrors: function(errors) {
619       var ret = [];
620       errors.forEach(function(error) {
621         // Removes errors without a message
622         if (v.isArray(error.error)) {
623           error.error.forEach(function(msg) {
624             ret.push(v.extend({}, error, {error: msg}));
625           });
626         } else {
627           ret.push(error);
628         }
629       });
630       return ret;
631     },
633     // Converts the error mesages by prepending the attribute name unless the
634     // message is prefixed by ^
635     convertErrorMessages: function(errors, options) {
636       options = options || {};
638       var ret = [];
639       errors.forEach(function(errorInfo) {
640         var error = v.result(errorInfo.error,
641             errorInfo.value,
642             errorInfo.attribute,
643             errorInfo.options,
644             errorInfo.attributes,
645             errorInfo.globalOptions);
647         if (!v.isString(error)) {
648           ret.push(errorInfo);
649           return;
650         }
652         if (error[0] === '^') {
653           error = error.slice(1);
654         } else if (options.fullMessages !== false) {
655           error = v.capitalize(v.prettify(errorInfo.attribute)) + " " + error;
656         }
657         error = error.replace(/\\\^/g, "^");
658         error = v.format(error, {value: v.stringifyValue(errorInfo.value)});
659         ret.push(v.extend({}, errorInfo, {error: error}));
660       });
661       return ret;
662     },
664     // In:
665     // [{attribute: "<attributeName>", ...}]
666     // Out:
667     // {"<attributeName>": [{attribute: "<attributeName>", ...}]}
668     groupErrorsByAttribute: function(errors) {
669       var ret = {};
670       errors.forEach(function(error) {
671         var list = ret[error.attribute];
672         if (list) {
673           list.push(error);
674         } else {
675           ret[error.attribute] = [error];
676         }
677       });
678       return ret;
679     },
681     // In:
682     // [{error: "<message 1>", ...}, {error: "<message 2>", ...}]
683     // Out:
684     // ["<message 1>", "<message 2>"]
685     flattenErrorsToArray: function(errors) {
686       return errors.map(function(error) { return error.error; });
687     },
689     cleanAttributes: function(attributes, whitelist) {
690       function whitelistCreator(obj, key, last) {
691         if (v.isObject(obj[key])) {
692           return obj[key];
693         }
694         return (obj[key] = last ? true : {});
695       }
697       function buildObjectWhitelist(whitelist) {
698         var ow = {}
699           , lastObject
700           , attr;
701         for (attr in whitelist) {
702           if (!whitelist[attr]) {
703             continue;
704           }
705           v.forEachKeyInKeypath(ow, attr, whitelistCreator);
706         }
707         return ow;
708       }
710       function cleanRecursive(attributes, whitelist) {
711         if (!v.isObject(attributes)) {
712           return attributes;
713         }
715         var ret = v.extend({}, attributes)
716           , w
717           , attribute;
719         for (attribute in attributes) {
720           w = whitelist[attribute];
722           if (v.isObject(w)) {
723             ret[attribute] = cleanRecursive(ret[attribute], w);
724           } else if (!w) {
725             delete ret[attribute];
726           }
727         }
728         return ret;
729       }
731       if (!v.isObject(whitelist) || !v.isObject(attributes)) {
732         return {};
733       }
735       whitelist = buildObjectWhitelist(whitelist);
736       return cleanRecursive(attributes, whitelist);
737     },
739     exposeModule: function(validate, root, exports, module, define) {
740       if (exports) {
741         if (module && module.exports) {
742           exports = module.exports = validate;
743         }
744         exports.validate = validate;
745       } else {
746         root.validate = validate;
747         if (validate.isFunction(define) && define.amd) {
748           define([], function () { return validate; });
749         }
750       }
751     },
753     warn: function(msg) {
754       if (typeof console !== "undefined" && console.warn) {
755         console.warn("[validate.js] " + msg);
756       }
757     },
759     error: function(msg) {
760       if (typeof console !== "undefined" && console.error) {
761         console.error("[validate.js] " + msg);
762       }
763     }
764   });
766   validate.validators = {
767     // Presence validates that the value isn't empty
768     presence: function(value, options) {
769       options = v.extend({}, this.options, options);
770       if (v.isEmpty(value)) {
771         return options.message || this.message || "can't be blank";
772       }
773     },
774     length: function(value, options, attribute) {
775       // Empty values are allowed
776       if (v.isEmpty(value)) {
777         return;
778       }
780       options = v.extend({}, this.options, options);
782       var is = options.is
783         , maximum = options.maximum
784         , minimum = options.minimum
785         , tokenizer = options.tokenizer || function(val) { return val; }
786         , err
787         , errors = [];
789       value = tokenizer(value);
790       var length = value.length;
791       if(!v.isNumber(length)) {
792         v.error(v.format("Attribute %{attr} has a non numeric value for `length`", {attr: attribute}));
793         return options.message || this.notValid || "has an incorrect length";
794       }
796       // Is checks
797       if (v.isNumber(is) && length !== is) {
798         err = options.wrongLength ||
799           this.wrongLength ||
800           "is the wrong length (should be %{count} characters)";
801         errors.push(v.format(err, {count: is}));
802       }
804       if (v.isNumber(minimum) && length < minimum) {
805         err = options.tooShort ||
806           this.tooShort ||
807           "is too short (minimum is %{count} characters)";
808         errors.push(v.format(err, {count: minimum}));
809       }
811       if (v.isNumber(maximum) && length > maximum) {
812         err = options.tooLong ||
813           this.tooLong ||
814           "is too long (maximum is %{count} characters)";
815         errors.push(v.format(err, {count: maximum}));
816       }
818       if (errors.length > 0) {
819         return options.message || errors;
820       }
821     },
822     numericality: function(value, options) {
823       // Empty values are fine
824       if (v.isEmpty(value)) {
825         return;
826       }
828       options = v.extend({}, this.options, options);
830       var errors = []
831         , name
832         , count
833         , checks = {
834             greaterThan:          function(v, c) { return v > c; },
835             greaterThanOrEqualTo: function(v, c) { return v >= c; },
836             equalTo:              function(v, c) { return v === c; },
837             lessThan:             function(v, c) { return v < c; },
838             lessThanOrEqualTo:    function(v, c) { return v <= c; },
839             divisibleBy:          function(v, c) { return v % c === 0; }
840           };
842       // Strict will check that it is a valid looking number
843       if (v.isString(value) && options.strict) {
844         var pattern = "^(0|[1-9]\\d*)";
845         if (!options.onlyInteger) {
846           pattern += "(\\.\\d+)?";
847         }
848         pattern += "$";
850         if (!(new RegExp(pattern).test(value))) {
851           return options.message || options.notValid || this.notValid || "must be a valid number";
852         }
853       }
855       // Coerce the value to a number unless we're being strict.
856       if (options.noStrings !== true && v.isString(value)) {
857         value = +value;
858       }
860       // If it's not a number we shouldn't continue since it will compare it.
861       if (!v.isNumber(value)) {
862         return options.message || options.notValid || this.notValid || "is not a number";
863       }
865       // Same logic as above, sort of. Don't bother with comparisons if this
866       // doesn't pass.
867       if (options.onlyInteger && !v.isInteger(value)) {
868         return options.message || options.notInteger || this.notInteger  || "must be an integer";
869       }
871       for (name in checks) {
872         count = options[name];
873         if (v.isNumber(count) && !checks[name](value, count)) {
874           // This picks the default message if specified
875           // For example the greaterThan check uses the message from
876           // this.notGreaterThan so we capitalize the name and prepend "not"
877           var key = "not" + v.capitalize(name);
878           var msg = options[key] || this[key] || "must be %{type} %{count}";
880           errors.push(v.format(msg, {
881             count: count,
882             type: v.prettify(name)
883           }));
884         }
885       }
887       if (options.odd && value % 2 !== 1) {
888         errors.push(options.notOdd || this.notOdd || "must be odd");
889       }
890       if (options.even && value % 2 !== 0) {
891         errors.push(options.notEven || this.notEven || "must be even");
892       }
894       if (errors.length) {
895         return options.message || errors;
896       }
897     },
898     datetime: v.extend(function(value, options) {
899       if (!v.isFunction(this.parse) || !v.isFunction(this.format)) {
900         throw new Error("Both the parse and format functions needs to be set to use the datetime/date validator");
901       }
903       // Empty values are fine
904       if (v.isEmpty(value)) {
905         return;
906       }
908       options = v.extend({}, this.options, options);
910       var err
911         , errors = []
912         , earliest = options.earliest ? this.parse(options.earliest, options) : NaN
913         , latest = options.latest ? this.parse(options.latest, options) : NaN;
915       value = this.parse(value, options);
917       // 86400000 is the number of seconds in a day, this is used to remove
918       // the time from the date
919       if (isNaN(value) || options.dateOnly && value % 86400000 !== 0) {
920         err = options.notValid ||
921           options.message ||
922           this.notValid ||
923           "must be a valid date";
924         return v.format(err, {value: arguments[0]});
925       }
927       if (!isNaN(earliest) && value < earliest) {
928         err = options.tooEarly ||
929           options.message ||
930           this.tooEarly ||
931           "must be no earlier than %{date}";
932         err = v.format(err, {
933           value: this.format(value, options),
934           date: this.format(earliest, options)
935         });
936         errors.push(err);
937       }
939       if (!isNaN(latest) && value > latest) {
940         err = options.tooLate ||
941           options.message ||
942           this.tooLate ||
943           "must be no later than %{date}";
944         err = v.format(err, {
945           date: this.format(latest, options),
946           value: this.format(value, options)
947         });
948         errors.push(err);
949       }
951       if (errors.length) {
952         return v.unique(errors);
953       }
954     }, {
955       parse: null,
956       format: null
957     }),
958     date: function(value, options) {
959       options = v.extend({}, options, {dateOnly: true});
960       return v.validators.datetime.call(v.validators.datetime, value, options);
961     },
962     format: function(value, options) {
963       if (v.isString(options) || (options instanceof RegExp)) {
964         options = {pattern: options};
965       }
967       options = v.extend({}, this.options, options);
969       var message = options.message || this.message || "is invalid"
970         , pattern = options.pattern
971         , match;
973       // Empty values are allowed
974       if (v.isEmpty(value)) {
975         return;
976       }
977       if (!v.isString(value)) {
978         return message;
979       }
981       if (v.isString(pattern)) {
982         pattern = new RegExp(options.pattern, options.flags);
983       }
984       match = pattern.exec(value);
985       if (!match || match[0].length != value.length) {
986         return message;
987       }
988     },
989     inclusion: function(value, options) {
990       // Empty values are fine
991       if (v.isEmpty(value)) {
992         return;
993       }
994       if (v.isArray(options)) {
995         options = {within: options};
996       }
997       options = v.extend({}, this.options, options);
998       if (v.contains(options.within, value)) {
999         return;
1000       }
1001       var message = options.message ||
1002         this.message ||
1003         "^%{value} is not included in the list";
1004       return v.format(message, {value: value});
1005     },
1006     exclusion: function(value, options) {
1007       // Empty values are fine
1008       if (v.isEmpty(value)) {
1009         return;
1010       }
1011       if (v.isArray(options)) {
1012         options = {within: options};
1013       }
1014       options = v.extend({}, this.options, options);
1015       if (!v.contains(options.within, value)) {
1016         return;
1017       }
1018       var message = options.message || this.message || "^%{value} is restricted";
1019       return v.format(message, {value: value});
1020     },
1021     email: v.extend(function(value, options) {
1022       options = v.extend({}, this.options, options);
1023       var message = options.message || this.message || "is not a valid email";
1024       // Empty values are fine
1025       if (v.isEmpty(value)) {
1026         return;
1027       }
1028       if (!v.isString(value)) {
1029         return message;
1030       }
1031       if (!this.PATTERN.exec(value)) {
1032         return message;
1033       }
1034     }, {
1035       PATTERN: /^[a-z0-9\u007F-\uffff!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9\u007F-\uffff!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/i
1036     }),
1037     equality: function(value, options, attribute, attributes) {
1038       if (v.isEmpty(value)) {
1039         return;
1040       }
1042       if (v.isString(options)) {
1043         options = {attribute: options};
1044       }
1045       options = v.extend({}, this.options, options);
1046       var message = options.message ||
1047         this.message ||
1048         "is not equal to %{attribute}";
1050       if (v.isEmpty(options.attribute) || !v.isString(options.attribute)) {
1051         throw new Error("The attribute must be a non empty string");
1052       }
1054       var otherValue = v.getDeepObjectValue(attributes, options.attribute)
1055         , comparator = options.comparator || function(v1, v2) {
1056           return v1 === v2;
1057         };
1059       if (!comparator(value, otherValue, options, attribute, attributes)) {
1060         return v.format(message, {attribute: v.prettify(options.attribute)});
1061       }
1062     },
1064     // A URL validator that is used to validate URLs with the ability to
1065     // restrict schemes and some domains.
1066     url: function(value, options) {
1067       if (v.isEmpty(value)) {
1068         return;
1069       }
1071       options = v.extend({}, this.options, options);
1073       var message = options.message || this.message || "is not a valid url"
1074         , schemes = options.schemes || this.schemes || ['http', 'https']
1075         , allowLocal = options.allowLocal || this.allowLocal || false;
1077       if (!v.isString(value)) {
1078         return message;
1079       }
1081       // https://gist.github.com/dperini/729294
1082       var regex =
1083         "^" +
1084           // schemes
1085           "(?:(?:" + schemes.join("|") + "):\\/\\/)" +
1086           // credentials
1087           "(?:\\S+(?::\\S*)?@)?";
1089       regex += "(?:";
1091       var tld = "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))";
1093       // This ia a special case for the localhost hostname
1094       if (allowLocal) {
1095         tld += "?";
1096       } else {
1097         // private & local addresses
1098         regex +=
1099           "(?!10(?:\\.\\d{1,3}){3})" +
1100           "(?!127(?:\\.\\d{1,3}){3})" +
1101           "(?!169\\.254(?:\\.\\d{1,3}){2})" +
1102           "(?!192\\.168(?:\\.\\d{1,3}){2})" +
1103           "(?!172" +
1104           "\\.(?:1[6-9]|2\\d|3[0-1])" +
1105           "(?:\\.\\d{1,3})" +
1106           "{2})";
1107       }
1109       var hostname =
1110           "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
1111           "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
1112           tld + ")";
1114       // reserved addresses
1115       regex +=
1116           "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
1117           "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
1118           "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
1119         "|" +
1120           hostname +
1121           // port number
1122           "(?::\\d{2,5})?" +
1123           // path
1124           "(?:\\/[^\\s]*)?" +
1125         "$";
1127       var PATTERN = new RegExp(regex, 'i');
1128       if (!PATTERN.exec(value)) {
1129         return message;
1130       }
1131     }
1132   };
1134   validate.exposeModule(validate, this, exports, module, define);
1135 }).call(this,
1136         typeof exports !== 'undefined' ? /* istanbul ignore next */ exports : null,
1137         typeof module !== 'undefined' ? /* istanbul ignore next */ module : null,
1138         typeof define !== 'undefined' ? /* istanbul ignore next */ define : null);