UNPKG

19.8 kBJavaScriptView Raw
1/*! validatorjs - v3.14.2 - - 2018-10-25 */!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,b.Validator=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){function d(a,b){this.onResolvedAll=b,this.onFailedOne=a,this.resolvers={},this.resolversCount=0,this.passed=[],this.failed=[],this.firing=!1}d.prototype={add:function(a){var b=this.resolversCount;return this.resolvers[b]=a,this.resolversCount++,b},resolve:function(a){var b=this.resolvers[a];b.passes===!0?this.passed.push(b):b.passes===!1&&(this.failed.push(b),this.onFailedOne(b)),this.fire()},isAllResolved:function(){return this.passed.length+this.failed.length===this.resolversCount},fire:function(){this.firing&&this.isAllResolved()&&this.onResolvedAll(0===this.failed.length)},enableFiring:function(){this.firing=!0}},b.exports=d},{}],2:[function(a,b,c){function d(a){return a.replace(/[_\[]/g," ").replace(/]/g,"")}var e={between:function(a,b){var c=b.getParameters();return this._replacePlaceholders(b,a,{min:c[0],max:c[1]})},required_if:function(a,b){var c=b.getParameters();return this._replacePlaceholders(b,a,{other:this._getAttributeName(c[0]),value:c[1]})},required_unless:function(a,b){var c=b.getParameters();return this._replacePlaceholders(b,a,{other:this._getAttributeName(c[0]),value:c[1]})},required_with:function(a,b){var c=b.getParameters();return this._replacePlaceholders(b,a,{field:this._getAttributeName(c[0])})},required_with_all:function(a,b){var c=b.getParameters(),d=this._getAttributeName.bind(this);return this._replacePlaceholders(b,a,{fields:c.map(d).join(", ")})},required_without:function(a,b){var c=b.getParameters();return this._replacePlaceholders(b,a,{field:this._getAttributeName(c[0])})},required_without_all:function(a,b){var c=b.getParameters(),d=this._getAttributeName.bind(this);return this._replacePlaceholders(b,a,{fields:c.map(d).join(", ")})},after:function(a,b){var c=b.getParameters();return this._replacePlaceholders(b,a,{after:this._getAttributeName(c[0])})},before:function(a,b){var c=b.getParameters();return this._replacePlaceholders(b,a,{before:this._getAttributeName(c[0])})},after_or_equal:function(a,b){var c=b.getParameters();return this._replacePlaceholders(b,a,{after_or_equal:this._getAttributeName(c[0])})},before_or_equal:function(a,b){var c=b.getParameters();return this._replacePlaceholders(b,a,{before_or_equal:this._getAttributeName(c[0])})},same:function(a,b){var c=b.getParameters();return this._replacePlaceholders(b,a,{same:this._getAttributeName(c[0])})}};b.exports={replacements:e,formatter:d}},{}],3:[function(a,b,c){var d=function(){this.errors={}};d.prototype={constructor:d,add:function(a,b){this.has(a)||(this.errors[a]=[]),this.errors[a].indexOf(b)===-1&&this.errors[a].push(b)},get:function(a){return this.has(a)?this.errors[a]:[]},first:function(a){return!!this.has(a)&&this.errors[a][0]},all:function(){return this.errors},has:function(a){return!!this.errors.hasOwnProperty(a)}},b.exports=d},{}],4:[function(a,b,c){var d=a("./messages");a("./lang/en");var e=a,f={messages:{},_set:function(a,b){this.messages[a]=b},_setRuleMessage:function(a,b,c){this._load(a),void 0===c&&(c=this.messages[a].def),this.messages[a][b]=c},_load:function(a){if(!this.messages[a])try{var b=e("./lang/"+a);this._set(a,b)}catch(c){}},_get:function(a){return this._load(a),this.messages[a]},_make:function(a){return this._load(a),new d(a,this.messages[a])}};b.exports=f},{"./lang/en":5,"./messages":6}],5:[function(a,b,c){b.exports={accepted:"The :attribute must be accepted.",after:"The :attribute must be after :after.",after_or_equal:"The :attribute must be equal or after :after_or_equal.",alpha:"The :attribute field must contain only alphabetic characters.",alpha_dash:"The :attribute field may only contain alpha-numeric characters, as well as dashes and underscores.",alpha_num:"The :attribute field must be alphanumeric.",before:"The :attribute must be before :before.",before_or_equal:"The :attribute must be equal or before :before_or_equal.",between:"The :attribute field must be between :min and :max.",confirmed:"The :attribute confirmation does not match.",email:"The :attribute format is invalid.",date:"The :attribute is not a valid date format.",def:"The :attribute attribute has errors.",digits:"The :attribute must be :digits digits.",different:"The :attribute and :different must be different.","in":"The selected :attribute is invalid.",integer:"The :attribute must be an integer.",hex:"The :attribute field should have hexadecimal format",min:{numeric:"The :attribute must be at least :min.",string:"The :attribute must be at least :min characters."},max:{numeric:"The :attribute may not be greater than :max.",string:"The :attribute may not be greater than :max characters."},not_in:"The selected :attribute is invalid.",numeric:"The :attribute must be a number.",present:"The :attribute field must be present (but can be empty).",required:"The :attribute field is required.",required_if:"The :attribute field is required when :other is :value.",required_unless:"The :attribute field is required when :other is not :value.",required_with:"The :attribute field is required when :field is not empty.",required_with_all:"The :attribute field is required when :fields are not empty.",required_without:"The :attribute field is required when :field is empty.",required_without_all:"The :attribute field is required when :fields are empty.",same:"The :attribute and :same fields must match.",size:{numeric:"The :attribute must be :size.",string:"The :attribute must be :size characters."},string:"The :attribute must be a string.",url:"The :attribute format is invalid.",regex:"The :attribute format is invalid.",attributes:{}}},{}],6:[function(a,b,c){var d=a("./attributes"),e=function(a,b){this.lang=a,this.messages=b,this.customMessages={},this.attributeNames={}};e.prototype={constructor:e,_setCustom:function(a){this.customMessages=a||{}},_setAttributeNames:function(a){this.attributeNames=a},_setAttributeFormatter:function(a){this.attributeFormatter=a},_getAttributeName:function(a){var b=a;return this.attributeNames.hasOwnProperty(a)?this.attributeNames[a]:(this.messages.attributes.hasOwnProperty(a)&&(b=this.messages.attributes[a]),this.attributeFormatter&&(b=this.attributeFormatter(b)),b)},all:function(){return this.messages},render:function(a){if(a.customMessage)return a.customMessage;var b,c=this._getTemplate(a);return b=d.replacements[a.name]?d.replacements[a.name].apply(this,[c,a]):this._replacePlaceholders(a,c,{})},_getTemplate:function(a){for(var b,c=this.messages,d=c.def,e=this.customMessages,f=[a.name+"."+a.attribute,a.name],g=0;g<f.length;g++){if(b=f[g],e.hasOwnProperty(b)){d=e[b];break}if(c.hasOwnProperty(b)){d=c[b];break}}return"object"==typeof d&&(d=d[a._getValueType()]),d},_replacePlaceholders:function(a,b,c){var d,e;if(c.attribute=this._getAttributeName(a.attribute),c[a.name]=c[a.name]||a.getParameters().join(","),"string"==typeof b&&"object"==typeof c){d=b;for(e in c)d=d.replace(new RegExp(":"+e,"g"),c[e])}return d}},b.exports=e},{"./attributes":2}],7:[function(a,b,c){function d(a){return a%4===0&&a%100!==0||a%400===0}function e(a){var b=!0;if("string"==typeof a){var c=a.indexOf(".");c>0&&c<=6&&(a=a.replace(/\./g,"-"))}var e=new Date(a),f=e.getFullYear(),g=e.getMonth(),h=e.getDate(),i=[31,d(f)?29:28,31,30,31,30,31,31,30,31,30,31];return!(f<1e3)&&(!isNaN(g)&&(!(g+1>12)&&(!isNaN(h)&&(!(h>i[g])&&b))))}function f(a,b,c){this.name=a,this.fn=b,this.passes=null,this._customMessage=void 0,this.async=c}var g,h={required:function(a){var b;return void 0!==a&&null!==a&&(b=String(a).replace(/\s/g,""),b.length>0)},required_if:function(a,b,c){return b=this.getParameters(),this.validator._objectPath(this.validator.input,b[0])!==b[1]||this.validator.getRule("required").validate(a)},required_unless:function(a,b,c){return b=this.getParameters(),this.validator._objectPath(this.validator.input,b[0])===b[1]||this.validator.getRule("required").validate(a)},required_with:function(a,b,c){return!this.validator._objectPath(this.validator.input,b)||this.validator.getRule("required").validate(a)},required_with_all:function(a,b,c){b=this.getParameters();for(var d=0;d<b.length;d++)if(!this.validator._objectPath(this.validator.input,b[d]))return!0;return this.validator.getRule("required").validate(a)},required_without:function(a,b,c){return!!this.validator._objectPath(this.validator.input,b)||this.validator.getRule("required").validate(a)},required_without_all:function(a,b,c){b=this.getParameters();for(var d=0;d<b.length;d++)if(this.validator._objectPath(this.validator.input,b[d]))return!0;return this.validator.getRule("required").validate(a)},"boolean":function(a){return a===!0||a===!1||0===a||1===a||"0"===a||"1"===a||"true"===a||"false"===a},size:function(a,b,c){if(a){b=parseFloat(b);var d=this.getSize();return d===b}return!0},string:function(a,b,c){return"string"==typeof a},sometimes:function(a){return!0},min:function(a,b,c){var d=this.getSize();return d>=b},max:function(a,b,c){var d=this.getSize();return d<=b},between:function(a,b,c){b=this.getParameters();var d=this.getSize(),e=parseFloat(b[0],10),f=parseFloat(b[1],10);return d>=e&&d<=f},email:function(a){var b=/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;return b.test(a)},numeric:function(a){var b;return b=Number(a),"number"==typeof b&&!isNaN(b)&&"boolean"!=typeof a},array:function(a){return a instanceof Array},url:function(a){return/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,63}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)/i.test(a)},alpha:function(a){return/^[a-zA-Z]+$/.test(a)},alpha_dash:function(a){return/^[a-zA-Z0-9_\-]+$/.test(a)},alpha_num:function(a){return/^[a-zA-Z0-9]+$/.test(a)},same:function(a,b){var c=this.validator._flattenObject(this.validator.input)[b],d=a;return c===d},different:function(a,b){var c=this.validator._flattenObject(this.validator.input)[b],d=a;return c!==d},"in":function(a,b){var c,d;if(a&&(c=this.getParameters()),a&&!(a instanceof Array)){var e=a;for(d=0;d<c.length;d++)if("string"==typeof c[d]&&(e=String(a)),e===c[d])return!0;return!1}if(a&&a instanceof Array)for(d=0;d<a.length;d++)if(c.indexOf(a[d])<0)return!1;return!0},not_in:function(a,b){for(var c=this.getParameters(),d=c.length,e=!0,f=0;f<d;f++){var g=a;if("string"==typeof c[f]&&(g=String(a)),g===c[f]){e=!1;break}}return e},accepted:function(a){return"on"===a||"yes"===a||1===a||"1"===a||a===!0},confirmed:function(a,b,c){var d=c+"_confirmation";return this.validator.input[d]===a},integer:function(a){return String(parseInt(a,10))===String(a)},digits:function(a,b){var c=this.validator.getRule("numeric");return!(!c.validate(a)||String(a).length!==parseInt(b))},regex:function(a,b){var c=/[g|i|m]{1,3}$/,d=b.match(c);return d=d?d[0]:"",b=b.replace(c,"").slice(1,-1),b=new RegExp(b,d),!!b.test(a)},date:function(a,b){return e(a)},present:function(a){return"undefined"!=typeof a},after:function(a,b){var c=this.validator.input[b],d=a;return!!e(c)&&(!!e(d)&&new Date(c).getTime()<new Date(d).getTime())},after_or_equal:function(a,b){var c=this.validator.input[b],d=a;return!!e(c)&&(!!e(d)&&new Date(c).getTime()<=new Date(d).getTime())},before:function(a,b){var c=this.validator.input[b],d=a;return!!e(c)&&(!!e(d)&&new Date(c).getTime()>new Date(d).getTime())},before_or_equal:function(a,b){var c=this.validator.input[b],d=a;return!!e(c)&&(!!e(d)&&new Date(c).getTime()>=new Date(d).getTime())},hex:function(a){return/^[0-9a-f]+$/i.test(a)}},i=function(){throw new Error("Validator `"+this.name+"` is not defined!")};f.prototype={validate:function(a,b,c,d){var e=this;if(this._setValidatingData(c,a,b),"function"==typeof d){this.callback=d;var f=function(a,b){e.response(a,b)};return this.async?this._apply(a,b,c,f):f(this._apply(a,b,c))}return this._apply(a,b,c)},_apply:function(a,b,c,d){var e=this.isMissed()?i:this.fn;return e.apply(this,[a,b,c,d])},_setValidatingData:function(a,b,c){this.attribute=a,this.inputValue=b,this.ruleValue=c},getParameters:function(){var a=[];return"string"==typeof this.ruleValue&&(a=this.ruleValue.split(",")),"number"==typeof this.ruleValue&&a.push(this.ruleValue),this.ruleValue instanceof Array&&(a=this.ruleValue),a},getSize:function(){var a=this.inputValue;return a instanceof Array?a.length:"number"==typeof a?a:this.validator._hasNumericRule(this.attribute)?parseFloat(a,10):a.length},_getValueType:function(){return"number"==typeof this.inputValue||this.validator._hasNumericRule(this.attribute)?"numeric":"string"},response:function(a,b){this.passes=void 0===a||a===!0,this._customMessage=b,this.callback(this.passes,b)},setValidator:function(a){this.validator=a},isMissed:function(){return"function"!=typeof this.fn},get customMessage(){return this.isMissed()?g:this._customMessage}};var j={asyncRules:[],implicitRules:["required","required_if","required_unless","required_with","required_with_all","required_without","required_without_all","accepted","present"],make:function(a,b){var c=this.isAsync(a),d=new f(a,h[a],c);return d.setValidator(b),d},isAsync:function(a){for(var b=0,c=this.asyncRules.length;b<c;b++)if(this.asyncRules[b]===a)return!0;return!1},isImplicit:function(a){return this.implicitRules.indexOf(a)>-1},register:function(a,b){h[a]=b},registerImplicit:function(a,b){this.register(a,b),this.implicitRules.push(a)},registerAsync:function(a,b){this.register(a,b),this.asyncRules.push(a)},registerAsyncImplicit:function(a,b){this.registerImplicit(a,b),this.asyncRules.push(a)},registerMissedRuleValidator:function(a,b){i=a,g=b}};b.exports=j},{}],8:[function(a,b,c){var d=a("./rules"),e=a("./lang"),f=a("./errors"),g=a("./attributes"),h=a("./async"),i=function(a,b,c){var d=i.getDefaultLang();this.input=a||{},this.messages=e._make(d),this.messages._setCustom(c),this.setAttributeFormatter(i.prototype.attributeFormatter),this.errors=new f,this.errorCount=0,this.hasAsync=!1,this.rules=this._parseRules(b)};i.prototype={constructor:i,lang:"en",numericRules:["integer","numeric"],attributeFormatter:g.formatter,check:function(){for(var a in this.rules){var b=this.rules[a],c=this._objectPath(this.input,a);if(!this._hasRule(a,["sometimes"])||this._suppliedWithData(a))for(var d,e,f,g=0,h=b.length;g<h&&(e=b[g],d=this.getRule(e.name),!this._isValidatable(d,c)||(f=d.validate(c,e.value,a),f||this._addFailure(d),!this._shouldStopValidating(a,f)));g++);}return 0===this.errorCount},checkAsync:function(a,b){var c=this;a=a||function(){},b=b||function(){};var d=function(a,b){c._addFailure(a,b)},e=function(c){c?a():b()},f=new h(d,e),g=function(a,b,c,d){return function(){var e=f.add(d);d.validate(a,b.value,c,function(){f.resolve(e)})}};for(var i in this.rules){var j=this.rules[i],k=this._objectPath(this.input,i);if(!this._hasRule(i,["sometimes"])||this._suppliedWithData(i))for(var l,m,n=0,o=j.length;n<o;n++)m=j[n],l=this.getRule(m.name),this._isValidatable(l,k)&&g(k,m,i,l)()}f.enableFiring(),f.fire()},_addFailure:function(a){var b=this.messages.render(a);this.errors.add(a.attribute,b),this.errorCount++},_flattenObject:function(a){function b(a,d){if(d||0!==Object.getOwnPropertyNames(a).length)if(Object(a)!==a||Array.isArray(a))c[d]=a;else{var e=!0;for(var f in a)e=!1,b(a[f],d?d+"."+f:f);e&&(c[d]={})}}var c={};return a&&b(a),c},_objectPath:function(a,b){if(Object.prototype.hasOwnProperty.call(a,b))return a[b];var c=b.replace(/\[(\w+)\]/g,".$1").replace(/^\./,"").split("."),d={};for(var e in a)Object.prototype.hasOwnProperty.call(a,e)&&(d[e]=a[e]);for(var f=0,g=c.length;f<g;f++){if(!Object.hasOwnProperty.call(d,c[f]))return;d=d[c[f]]}return d},_parseRules:function(a){var b={};a=this._flattenObject(a);for(var c in a){var d=a[c];this._parseRulesCheck(c,d,b)}return b},_parseRulesCheck:function(a,b,c,d){a.indexOf("*")>-1?this._parsedRulesRecurse(a,b,c,d):this._parseRulesDefault(a,b,c,d)},_parsedRulesRecurse:function(a,b,c,d){var e=a.substr(0,a.indexOf("*")-1),f=this._objectPath(this.input,e);if(f)for(var g=0;g<f.length;g++){var h=d?d.slice():[];h.push(g),this._parseRulesCheck(a.replace("*",g),b,c,h)}},_parseRulesDefault:function(a,b,c,e){var f=[];b instanceof Array&&(b=this._prepareRulesArray(b)),"string"==typeof b&&(b=b.split("|"));for(var g,h=0,i=b.length;h<i;h++)g="string"==typeof b[h]?this._extractRuleAndRuleValue(b[h]):b[h],g.value&&(g.value=this._replaceWildCards(g.value,e),this._replaceWildCardsMessages(e)),d.isAsync(g.name)&&(this.hasAsync=!0),f.push(g);c[a]=f},_replaceWildCards:function(a,b){if(!b)return a;var c=a;return b.forEach(function(a){return Array.isArray(c)&&(c=c[0]),pos=c.indexOf("*"),pos===-1?c:void(c=c.substr(0,pos)+a+c.substr(pos+1))}),Array.isArray(a)&&(a[0]=c,c=a),c},_replaceWildCardsMessages:function(a){var b=this.messages.customMessages,c=this;Object.keys(b).forEach(function(d){if(a){var e=c._replaceWildCards(d,a);b[e]=b[d]}}),this.messages._setCustom(b)},_prepareRulesArray:function(a){for(var b=[],c=0,d=a.length;c<d;c++)if("object"==typeof a[c])for(var e in a[c])b.push({name:e,value:a[c][e]});else b.push(a[c]);return b},_suppliedWithData:function(a){return this.input.hasOwnProperty(a)},_extractRuleAndRuleValue:function(a){var b,c={};return c.name=a,a.indexOf(":")>=0&&(b=a.split(":"),c.name=b[0],c.value=b.slice(1).join(":")),c},_hasRule:function(a,b){for(var c=this.rules[a]||[],d=0,e=c.length;d<e;d++)if(b.indexOf(c[d].name)>-1)return!0;return!1},_hasNumericRule:function(a){return this._hasRule(a,this.numericRules)},_isValidatable:function(a,b){return!!d.isImplicit(a.name)||this.getRule("required").validate(b)},_shouldStopValidating:function(a,b){var c=this.stopOnAttributes;return"undefined"!=typeof c&&c!==!1&&b!==!0&&(!(c instanceof Array)||c.indexOf(a)>-1)},setAttributeNames:function(a){this.messages._setAttributeNames(a)},setAttributeFormatter:function(a){this.messages._setAttributeFormatter(a)},getRule:function(a){return d.make(a,this)},stopOnError:function(a){this.stopOnAttributes=a},passes:function(a){var b=this._checkAsync("passes",a);return b?this.checkAsync(a):this.check()},fails:function(a){var b=this._checkAsync("fails",a);return b?this.checkAsync(function(){},a):!this.check()},_checkAsync:function(a,b){var c="function"==typeof b;if(this.hasAsync&&!c)throw a+" expects a callback when async rules are being tested.";return this.hasAsync||c}},i.setMessages=function(a,b){return e._set(a,b),this},i.getMessages=function(a){return e._get(a)},i.useLang=function(a){this.prototype.lang=a},i.getDefaultLang=function(){return this.prototype.lang},i.setAttributeFormatter=function(a){this.prototype.attributeFormatter=a},i.stopOnError=function(a){this.prototype.stopOnAttributes=a},i.register=function(a,b,c){var f=i.getDefaultLang();d.register(a,b),e._setRuleMessage(f,a,c)},i.registerImplicit=function(a,b,c){var f=i.getDefaultLang();d.registerImplicit(a,b),e._setRuleMessage(f,a,c)},i.registerAsync=function(a,b,c){var f=i.getDefaultLang();d.registerAsync(a,b),e._setRuleMessage(f,a,c)},i.registerAsyncImplicit=function(a,b,c){var f=i.getDefaultLang();d.registerAsyncImplicit(a,b),e._setRuleMessage(f,a,c)},i.registerMissedRuleValidator=function(a,b){d.registerMissedRuleValidator(a,b)},b.exports=i},{"./async":1,"./attributes":2,"./errors":3,"./lang":4,"./rules":7}]},{},[8])(8)});
\No newline at end of file