This repository has been archived by the owner on Feb 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
validatejs.min.js
1 lines (1 loc) · 12.7 KB
/
validatejs.min.js
1
var _=system.library.Underscore.require(),instance={};function require(){return instance.validate}(function(t,e,n){"use strict";var r=function(t,e,n){n=i.extend({},i.options,n);var o=i.runValidations(t,e,n);return r.processValidationResults(o,n)},i=r;return i.extend=function(t){return _.each([].slice.call(arguments,1),function(e){for(var n in e)t[n]=e[n]}),t},i.extend(r,{version:{major:0,minor:12,patch:0,metadata:null,toString:function(){var t=i.format("%{major}.%{minor}.%{patch}",i.version);return i.isEmpty(i.version.metadata)||(t+="+"+i.version.metadata),t}},Promise:"undefined"!=typeof Promise?Promise:null,EMPTY_STRING_REGEXP:/^\s*$/,runValidations:function(t,e,n){var r,o,s,a,u,f,l,c=[];(i.isDomElement(t)||i.isJqueryElement(t))&&(t=i.collectFormValues(t));for(r in e){s=i.getDeepObjectValue(t,r),a=i.result(e[r],s,t,r,n,e);for(o in a){if(!(u=i.validators[o]))throw l=i.format("Unknown validator %{name}",{name:o}),new Error(l);f=a[o],(f=i.result(f,s,t,r,n,e))&&c.push({attribute:r,value:s,validator:o,globalOptions:n,attributes:t,options:f,error:u.call(u,s,f,r,t,n)})}}return c},processValidationResults:function(t,e){t=i.pruneEmptyErrors(t,e),t=i.expandMultipleErrors(t,e),t=i.convertErrorMessages(t,e);var n=e.format||"grouped";if("function"!=typeof i.formatters[n])throw new Error(i.format("Unknown format %{format}",e));return t=i.formatters[n](t),i.isEmpty(t)?void 0:t},async:function(t,e,n){var r=(n=i.extend({},i.async.options,n)).wrapErrors||function(t){return t};!1!==n.cleanAttributes&&(t=i.cleanAttributes(t,e));var o=i.runValidations(t,e,n);return new i.Promise(function(s,a){i.waitForResults(o).then(function(){var u=i.processValidationResults(o,n);u?a(new r(u,n,t,e)):s(t)},function(t){a(t)})})},single:function(t,e,n){return n=i.extend({},i.single.options,n,{format:"flat",fullMessages:!1}),i({single:t},{single:e},n)},waitForResults:function(t){return t.reduce(function(t,e){return i.isPromise(e.error)?t.then(function(){return e.error.then(function(t){e.error=t||null})}):t},new i.Promise(function(t){t()}))},result:function(t){var e=[].slice.call(arguments,1);return"function"==typeof t&&(t=t.apply(null,e)),t},isNumber:function(t){return"number"==typeof t&&!isNaN(t)},isFunction:function(t){return"function"==typeof t},isInteger:function(t){return i.isNumber(t)&&t%1==0},isBoolean:function(t){return"boolean"==typeof t},isObject:function(t){return t===Object(t)},isDate:function(t){return t instanceof Date},isDefined:function(t){return null!==t&&void 0!==t},isPromise:function(t){return!!t&&i.isFunction(t.then)},isJqueryElement:function(t){return t&&i.isString(t.jquery)},isDomElement:function(t){return!!t&&(!(!t.querySelectorAll||!t.querySelector)&&(!(!i.isObject(document)||t!==document)||("object"==typeof HTMLElement?t instanceof HTMLElement:t&&"object"==typeof t&&null!==t&&1===t.nodeType&&"string"==typeof t.nodeName)))},isEmpty:function(t){var e;if(!i.isDefined(t))return!0;if(i.isFunction(t))return!1;if(i.isString(t))return i.EMPTY_STRING_REGEXP.test(t);if(i.isArray(t))return 0===t.length;if(i.isDate(t))return!1;if(i.isObject(t)){for(e in t)return!1;return!0}return!1},format:i.extend(function(t,e){return i.isString(t)?t.replace(i.format.FORMAT_REGEXP,function(t,n,r){return"%"===n?"%{"+r+"}":String(e[r])}):t},{FORMAT_REGEXP:/(%?)%\{([^\}]+)\}/g}),prettify:function(t){return i.isNumber(t)?100*t%1==0?""+t:parseFloat(Math.round(100*t)/100).toFixed(2):i.isArray(t)?t.map(function(t){return i.prettify(t)}).join(", "):i.isObject(t)?t.toString():(t=""+t).replace(/([^\s])\.([^\s])/g,"$1 $2").replace(/\\+/g,"").replace(/[_-]/g," ").replace(/([a-z])([A-Z])/g,function(t,e,n){return e+" "+n.toLowerCase()}).toLowerCase()},stringifyValue:function(t,e){return(e&&e.prettify||i.prettify)(t)},isString:function(t){return"string"==typeof t},isArray:function(t){return"[object Array]"==={}.toString.call(t)},isHash:function(t){return i.isObject(t)&&!i.isArray(t)&&!i.isFunction(t)},contains:function(t,e){return!!i.isDefined(t)&&(i.isArray(t)?-1!==t.indexOf(e):e in t)},unique:function(t){return i.isArray(t)?t.filter(function(t,e,n){return n.indexOf(t)==e}):t},forEachKeyInKeypath:function(t,e,n){if(i.isString(e)){var r,o="",s=!1;for(r=0;r<e.length;++r)switch(e[r]){case".":s?(s=!1,o+="."):(t=n(t,o,!1),o="");break;case"\\":s?(s=!1,o+="\\"):s=!0;break;default:s=!1,o+=e[r]}return n(t,o,!0)}},getDeepObjectValue:function(t,e){if(i.isObject(t))return i.forEachKeyInKeypath(t,e,function(t,e){if(i.isObject(t))return t[e]})},collectFormValues:function(t,e){var n,r,o,s,a,u,f={};if(i.isJqueryElement(t)&&(t=t[0]),!t)return f;for(e=e||{},s=t.querySelectorAll("input[name], textarea[name]"),n=0;n<s.length;++n)o=s.item(n),i.isDefined(o.getAttribute("data-ignored"))||(name=o.name.replace(/\./g,"\\\\."),u=i.sanitizeFormValue(o.value,e),"number"===o.type?u=u?+u:null:"checkbox"===o.type?o.attributes.value?o.checked||(u=f[name]||null):u=o.checked:"radio"===o.type&&(o.checked||(u=f[name]||null)),f[name]=u);for(s=t.querySelectorAll("select[name]"),n=0;n<s.length;++n)if(o=s.item(n),!i.isDefined(o.getAttribute("data-ignored"))){if(o.multiple){u=[];for(r in o.options)(a=o.options[r])&&a.selected&&u.push(i.sanitizeFormValue(a.value,e))}else{var l=void 0!==o.options[o.selectedIndex]?o.options[o.selectedIndex].value:"";u=i.sanitizeFormValue(l,e)}f[o.name]=u}return f},sanitizeFormValue:function(t,e){return e.trim&&i.isString(t)&&(t=t.trim()),!1!==e.nullify&&""===t?null:t},capitalize:function(t){return i.isString(t)?t[0].toUpperCase()+t.slice(1):t},pruneEmptyErrors:function(t){return _.filter(t,function(t){return!i.isEmpty(t.error)})},expandMultipleErrors:function(t){var e=[];return _.each(t,function(t){i.isArray(t.error)?_.each(t.error,function(n){e.push(i.extend({},t,{error:n}))}):e.push(t)}),e},convertErrorMessages:function(t,e){var n=[],r=(e=e||{}).prettify||i.prettify;return _.each(t,function(t){var o=i.result(t.error,t.value,t.attribute,t.options,t.attributes,t.globalOptions);i.isString(o)?("^"===o[0]?o=o.slice(1):!1!==e.fullMessages&&(o=i.capitalize(r(t.attribute))+" "+o),o=o.replace(/\\\^/g,"^"),o=i.format(o,{value:i.stringifyValue(t.value,e)}),n.push(i.extend({},t,{error:o}))):n.push(t)}),n},groupErrorsByAttribute:function(t){var e={};return _.each(t,function(t){var n=e[t.attribute];n?n.push(t):e[t.attribute]=[t]}),e},flattenErrorsToArray:function(t){var e=_.map(t,function(t){return t.error});return e=_.filter(e,function(t,e,n){return _.indexOf(n,t)===e})},cleanAttributes:function(t,e){function n(t,e,n){return i.isObject(t[e])?t[e]:t[e]=!!n||{}}return i.isObject(e)&&i.isObject(t)?function t(e,n){if(!i.isObject(e))return e;var r,o,s=i.extend({},e);for(o in e)r=n[o],i.isObject(r)?s[o]=t(s[o],r):r||delete s[o];return s}(t,e=function(t){var e,r={};for(e in t)t[e]&&i.forEachKeyInKeypath(r,e,n);return r}(e)):{}},exposeModule:function(t,e,n,r,i){n?(r&&r.exports&&(n=r.exports=t),n.validate=t):(e.validate=t,t.isFunction(i)&&i.amd&&i([],function(){return t}))},warn:function(t){"undefined"!=typeof console&&console.warn&&console.warn("[validate.js] "+t)},error:function(t){"undefined"!=typeof console&&console.error&&console.error("[validate.js] "+t)}}),r.validators={presence:function(t,e){if(!1!==(e=i.extend({},this.options,e)).allowEmpty?!i.isDefined(t):i.isEmpty(t))return e.message||this.message||"can't be blank"},length:function(t,e,n){if(i.isDefined(t)){var r,o=(e=i.extend({},this.options,e)).is,s=e.maximum,a=e.minimum,u=[],f=(t=(e.tokenizer||function(t){return t})(t)).length;return i.isNumber(f)?(i.isNumber(o)&&f!==o&&(r=e.wrongLength||this.wrongLength||"is the wrong length (should be %{count} characters)",u.push(i.format(r,{count:o}))),i.isNumber(a)&&f<a&&(r=e.tooShort||this.tooShort||"is too short (minimum is %{count} characters)",u.push(i.format(r,{count:a}))),i.isNumber(s)&&f>s&&(r=e.tooLong||this.tooLong||"is too long (maximum is %{count} characters)",u.push(i.format(r,{count:s}))),u.length>0?e.message||u:void 0):(i.error(i.format("Attribute %{attr} has a non numeric value for `length`",{attr:n})),e.message||this.notValid||"has an incorrect length")}},numericality:function(t,e,n,r,o){if(i.isDefined(t)){var s,a,u=[],f={greaterThan:function(t,e){return t>e},greaterThanOrEqualTo:function(t,e){return t>=e},equalTo:function(t,e){return t===e},lessThan:function(t,e){return t<e},lessThanOrEqualTo:function(t,e){return t<=e},divisibleBy:function(t,e){return t%e==0}},l=(e=i.extend({},this.options,e)).prettify||o&&o.prettify||i.prettify;if(i.isString(t)&&e.strict){var c="^-?(0|[1-9]\\d*)";if(e.onlyInteger||(c+="(\\.\\d+)?"),c+="$",!new RegExp(c).test(t))return e.message||e.notValid||this.notValid||this.message||"must be a valid number"}if(!0!==e.noStrings&&i.isString(t)&&!i.isEmpty(t)&&(t=+t),!i.isNumber(t))return e.message||e.notValid||this.notValid||this.message||"is not a number";if(e.onlyInteger&&!i.isInteger(t))return e.message||e.notInteger||this.notInteger||this.message||"must be an integer";for(s in f)if(a=e[s],i.isNumber(a)&&!f[s](t,a)){var m="not"+i.capitalize(s),d=e[m]||this[m]||this.message||"must be %{type} %{count}";u.push(i.format(d,{count:a,type:l(s)}))}return e.odd&&t%2!=1&&u.push(e.notOdd||this.notOdd||this.message||"must be odd"),e.even&&t%2!=0&&u.push(e.notEven||this.notEven||this.message||"must be even"),u.length?e.message||u:void 0}},datetime:i.extend(function(t,e){if(!i.isFunction(this.parse)||!i.isFunction(this.format))throw new Error("Both the parse and format functions needs to be set to use the datetime/date validator");if(i.isDefined(t)){var n,r=[],o=(e=i.extend({},this.options,e)).earliest?this.parse(e.earliest,e):NaN,s=e.latest?this.parse(e.latest,e):NaN;return t=this.parse(t,e),isNaN(t)||e.dateOnly&&t%864e5!=0?(n=e.notValid||e.message||this.notValid||"must be a valid date",i.format(n,{value:arguments[0]})):(!isNaN(o)&&t<o&&(n=e.tooEarly||e.message||this.tooEarly||"must be no earlier than %{date}",n=i.format(n,{value:this.format(t,e),date:this.format(o,e)}),r.push(n)),!isNaN(s)&&t>s&&(n=e.tooLate||e.message||this.tooLate||"must be no later than %{date}",n=i.format(n,{date:this.format(s,e),value:this.format(t,e)}),r.push(n)),r.length?i.unique(r):void 0)}},{parse:null,format:null}),date:function(t,e){return e=i.extend({},e,{dateOnly:!0}),i.validators.datetime.call(i.validators.datetime,t,e)},format:function(t,e){(i.isString(e)||e instanceof RegExp)&&(e={pattern:e});var n,r=(e=i.extend({},this.options,e)).message||this.message||"is invalid",o=e.pattern;if(i.isDefined(t))return i.isString(t)?(i.isString(o)&&(o=new RegExp(e.pattern,e.flags)),(n=o.exec(t))&&n[0].length==t.length?void 0:r):r},inclusion:function(t,e){if(i.isDefined(t)&&(i.isArray(e)&&(e={within:e}),e=i.extend({},this.options,e),!i.contains(e.within,t))){var n=e.message||this.message||"^%{value} is not included in the list";return i.format(n,{value:t})}},exclusion:function(t,e){if(i.isDefined(t)&&(i.isArray(e)&&(e={within:e}),e=i.extend({},this.options,e),i.contains(e.within,t))){var n=e.message||this.message||"^%{value} is restricted";return i.format(n,{value:t})}},email:i.extend(function(t,e){var n=(e=i.extend({},this.options,e)).message||this.message||"is not a valid email";if(i.isDefined(t))return i.isString(t)&&this.PATTERN.exec(t)?void 0:n},{PATTERN:/^[a-z0-9\u007F-\uffff!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9\u007F-\uffff!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/i}),equality:function(t,e,n,r,o){if(i.isDefined(t)){i.isString(e)&&(e={attribute:e});var s=(e=i.extend({},this.options,e)).message||this.message||"is not equal to %{attribute}";if(i.isEmpty(e.attribute)||!i.isString(e.attribute))throw new Error("The attribute must be a non empty string");var a=i.getDeepObjectValue(r,e.attribute),u=e.comparator||function(t,e){return t===e},f=e.prettify||o&&o.prettify||i.prettify;return u(t,a,e,n,r)?void 0:i.format(s,{attribute:f(e.attribute)})}},url:function(t,e){if(i.isDefined(t)){var n=(e=i.extend({},this.options,e)).message||this.message||"is not a valid url",r=e.schemes||this.schemes||["http","https"],o=e.allowLocal||this.allowLocal||!1;if(!i.isString(t))return n;var s="^(?:(?:"+r.join("|")+")://)(?:\\S+(?::\\S*)?@)?(?:",a="(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))";return o?a+="?":s+="(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})",s+="(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*"+a+")(?::\\d{2,5})?(?:[/?#]\\S*)?$",new RegExp(s,"i").exec(t)?void 0:n}}},r.formatters={detailed:function(t){return t},flat:i.flattenErrorsToArray,grouped:function(t){var e;t=i.groupErrorsByAttribute(t);for(e in t)t[e]=i.flattenErrorsToArray(t[e]);return t},constraint:function(t){var e;t=i.groupErrorsByAttribute(t);for(e in t)t[e]=t[e].map(function(t){return t.validator}).sort();return t}},r.exposeModule(r,this,t,e,n),r}).call(instance);