UNPKG

vuelidate-error-extractor

Version:
6 lines 5.23 kB
/*! * vuelidate-error-extractor v2.4.1 * (c) 2019 Dobromir Hristov * Released under the MIT License. */ !function(t,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r(t.VuelidateErrorExtractorSingleErrorExtractorMixin={})}(this,function(t){"use strict";function r(t){return null!=t&&"object"==typeof t&&!1===Array.isArray(t)}function e(t,r,e){return"function"==typeof e.join?e.join(t):t[0]+r+t[1]}function i(t,r,e){return"function"==typeof e.split?e.split(t):t.split(r)}function n(t,r,e){return"function"!=typeof e.isValid||e.isValid(t,r)}function a(t){return r(t)||Array.isArray(t)||"function"==typeof t}function s(t,r,e){return p(t,r,{default:e})}function o(t){return"[vuelidate-error-extractor]: "+t}function u(t,r){if("string"!=typeof t)throw new TypeError(o("Expected a string in the first argument, got "+typeof t));if("object"!=typeof r)throw new TypeError(o("Expected an Object/Array in the second argument, got "+typeof r));var e=/{(.*?)}/g;return t.replace(e,function(t,e){return s(r,e,"")})}function f(t,r,e){return void 0===e&&(e={}),{validationKey:t,hasError:!this.preferredValidator[r],$params:this.preferredValidator.$params[r],$dirty:this.preferredValidator.$dirty,$error:this.preferredValidator.$error,$invalid:this.preferredValidator.$invalid,params:Object.assign({},{attribute:this.resolvedAttribute,label:this.label},e,this.validatorParams)}}function l(t,r,e){var i=s(t,r,!1);return i?u(i,e):r}function d(t,r){var e=r.replace(E,"");return s(t,e,e)}function c(t,r){var e=r.replace(E,""),i=s(t,e);return i?this.$t(i):this.$_VEE_i18nDefaultAttribute?this.$t(this.$_VEE_i18nDefaultAttribute+"."+e):e}function h(t,r,e){return this.$_VEE_hasI18n&&this.$_VEE_hasI18nAttributes?c.call(this,t,e):d(r,e)}var p=function(t,s,o){if(r(o)||(o={default:o}),!a(t))return void 0!==o.default?o.default:t;"number"==typeof s&&(s=String(s));var u=Array.isArray(s),f="string"==typeof s,l=o.separator||".",d=o.joinChar||("string"==typeof l?l:".");if(!f&&!u)return t;if(f&&s in t)return n(s,t,o)?t[s]:o.default;var c=u?s:i(s,l,o),h=c.length,p=0;do{var E=c[p];for("number"==typeof E&&(E=String(E));E&&"\\"===E.slice(-1);)E=e([E.slice(0,-1),c[++p]||""],d,o);if(E in t){if(!n(E,t,o))return o.default;t=t[E]}else{for(var v=!1,g=p+1;g<h;)if(E=e([E,c[g++]],d,o),v=E in t){if(!n(E,t,o))return o.default;t=t[E],p=g-1;break}if(!v)return o.default}}while(++p<h&&a(t));return p===h?t:o.default},E=/\$each\.\d\./g,v={inject:{formValidator:{default:!1},formMessages:{default:function(){return{}}}},props:{validator:{type:Object,default:function(){return{$dirty:!1,$error:!1,$invalid:!0,$pending:!1,$params:[]}}},messages:{type:Object,default:function(){return{}}}},computed:{activeErrors:function(){return this.errors.filter(function(t){return t.hasError&&t.$dirty})},mergedMessages:function(){return Object.assign({},this.$vuelidateErrorExtractor.messages,this.formMessages,this.messages)},firstError:function(){return this.activeErrors.length?this.activeErrors[0]:""},firstErrorMessage:function(){return this.activeErrors.length?this.activeErrorMessages[0]:""},hasErrors:function(){return this.preferredValidator.$error},activeErrorMessages:function(){var t=this;return this.activeErrors.map(function(r){return t.getErrorMessage(r.validationKey,r.params)})},$_VEE_hasI18n:function(){return!!this.$vuelidateErrorExtractor.i18n},$_VEE_hasI18nAttributes:function(){return!!this.$vuelidateErrorExtractor.i18nAttributes},$_VEE_i18nDefaultAttribute:function(){return this.$_VEE_hasI18nAttributes?this.$vuelidateErrorExtractor.i18nAttributes.__default:""}},methods:{getErrorMessage:function(t,r){return this.$_VEE_hasI18n?this.getI18nMessage(t,r):this.getPlainMessage(t,r)},getI18nMessage:function(t,r){var e=s(this.mergedMessages,t);return e?this.$t(e,r):this.$t(this.$vuelidateErrorExtractor.i18n+"."+t,r)},getPlainMessage:function(t,r){return l(this.mergedMessages,t,r)}}},g={props:{label:{type:String,default:""},attribute:{type:String,default:""},name:{type:String,default:""},validatorParams:{type:Object,default:function(){return{}}},showSingleError:{type:Boolean,default:!1}},extends:v,computed:{preferredValidator:function(){return this.$options.propsData.hasOwnProperty("validator")?this.validator:this.name?s(this.formValidator,this.name,this.validator):this.validator},errors:function(){var t=this,r=this.preferredValidator.$params,e=this.$vuelidateErrorExtractor.validationKeys||{};return Object.keys(r).map(function(i){var n=r[i];if(e.hasOwnProperty(i)){var a=e[i].params.reduce(function(r,e){return r[e.ext]=t.validatorParams[e.vue]||n[e.vue],r},{});return f.call(t,e[i].validationKey,i,a)}var s=Object.assign({},n,t.validatorParams);return delete s.type,f.call(t,i,i,s)})},events:function(){var t=this;return{input:function(){return t.preferredValidator.$touch()}}},isValid:function(){return this.preferredValidator.$dirty?!this.hasErrors:null},resolvedAttribute:function(){return this.attribute?this.$_VEE_hasI18n?this.$t(this.attribute):this.attribute:this.name?h.call(this,this.$vuelidateErrorExtractor.i18nAttributes,this.$vuelidateErrorExtractor.attributes,this.name):this.label}}};t.default=g,Object.defineProperty(t,"__esModule",{value:!0})});