UNPKG

vuelidate-error-extractor

Version:
6 lines 7.27 kB
/*! * vuelidate-error-extractor v2.4.1 * (c) 2019 Dobromir Hristov * Released under the MIT License. */ !function(r,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(r.VuelidateErrorExtractorsingleErrorExtractorFoundation6={})}(this,function(r){"use strict";function t(r){return null!=r&&"object"==typeof r&&!1===Array.isArray(r)}function e(r,t,e){return"function"==typeof e.join?e.join(r):r[0]+t+r[1]}function i(r,t,e){return"function"==typeof e.split?e.split(r):r.split(t)}function a(r,t,e){return"function"!=typeof e.isValid||e.isValid(r,t)}function s(r){return t(r)||Array.isArray(r)||"function"==typeof r}function n(r,t,e){return p(r,t,{default:e})}function o(r){return"[vuelidate-error-extractor]: "+r}function u(r,t){if("string"!=typeof r)throw new TypeError(o("Expected a string in the first argument, got "+typeof r));if("object"!=typeof t)throw new TypeError(o("Expected an Object/Array in the second argument, got "+typeof t));var e=/{(.*?)}/g;return r.replace(e,function(r,e){return n(t,e,"")})}function l(r,t,e){return void 0===e&&(e={}),{validationKey:r,hasError:!this.preferredValidator[t],$params:this.preferredValidator.$params[t],$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 f(r,t,e){var i=n(r,t,!1);return i?u(i,e):t}function d(r,t){var e=t.replace(E,"");return n(r,e,e)}function c(r,t){var e=t.replace(E,""),i=n(r,e);return i?this.$t(i):this.$_VEE_i18nDefaultAttribute?this.$t(this.$_VEE_i18nDefaultAttribute+"."+e):e}function h(r,t,e){return this.$_VEE_hasI18n&&this.$_VEE_hasI18nAttributes?c.call(this,r,e):d(t,e)}function v(r,t,e,i,a,s,n,o,u,l){"boolean"!=typeof n&&(u=o,o=n,n=!1);var f="function"==typeof e?e.options:e;r&&r.render&&(f.render=r.render,f.staticRenderFns=r.staticRenderFns,f._compiled=!0,a&&(f.functional=!0)),i&&(f._scopeId=i);var d;if(s?(d=function(r){r=r||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext,r||"undefined"==typeof __VUE_SSR_CONTEXT__||(r=__VUE_SSR_CONTEXT__),t&&t.call(this,u(r)),r&&r._registeredComponents&&r._registeredComponents.add(s)},f._ssrRegister=d):t&&(d=n?function(){t.call(this,l(this.$root.$options.shadowRoot))}:function(r){t.call(this,o(r))}),d)if(f.functional){var c=f.render;f.render=function(r,t){return d.call(t),c(r,t)}}else{var h=f.beforeCreate;f.beforeCreate=h?[].concat(h,d):[d]}return e}var p=function(r,n,o){if(t(o)||(o={default:o}),!s(r))return void 0!==o.default?o.default:r;"number"==typeof n&&(n=String(n));var u=Array.isArray(n),l="string"==typeof n,f=o.separator||".",d=o.joinChar||("string"==typeof f?f:".");if(!l&&!u)return r;if(l&&n in r)return a(n,r,o)?r[n]:o.default;var c=u?n:i(n,f,o),h=c.length,v=0;do{var p=c[v];for("number"==typeof p&&(p=String(p));p&&"\\"===p.slice(-1);)p=e([p.slice(0,-1),c[++v]||""],d,o);if(p in r){if(!a(p,r,o))return o.default;r=r[p]}else{for(var E=!1,g=v+1;g<h;)if(p=e([p,c[g++]],d,o),E=p in r){if(!a(p,r,o))return o.default;r=r[p],v=g-1;break}if(!E)return o.default}}while(++v<h&&s(r));return v===h?r:o.default},E=/\$each\.\d\./g,g={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(r){return r.hasError&&r.$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 r=this;return this.activeErrors.map(function(t){return r.getErrorMessage(t.validationKey,t.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(r,t){return this.$_VEE_hasI18n?this.getI18nMessage(r,t):this.getPlainMessage(r,t)},getI18nMessage:function(r,t){var e=n(this.mergedMessages,r);return e?this.$t(e,t):this.$t(this.$vuelidateErrorExtractor.i18n+"."+r,t)},getPlainMessage:function(r,t){return f(this.mergedMessages,r,t)}}},_={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:g,computed:{preferredValidator:function(){return this.$options.propsData.hasOwnProperty("validator")?this.validator:this.name?n(this.formValidator,this.name,this.validator):this.validator},errors:function(){var r=this,t=this.preferredValidator.$params,e=this.$vuelidateErrorExtractor.validationKeys||{};return Object.keys(t).map(function(i){var a=t[i];if(e.hasOwnProperty(i)){var s=e[i].params.reduce(function(t,e){return t[e.ext]=r.validatorParams[e.vue]||a[e.vue],t},{});return l.call(r,e[i].validationKey,i,s)}var n=Object.assign({},a,r.validatorParams);return delete n.type,l.call(r,i,i,n)})},events:function(){var r=this;return{input:function(){return r.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}}},y={mixins:[_],computed:{attributes:function(){return{class:{"is-invalid-input":this.hasErrors}}}}},$=v,b=y,m=function(){var r=this,t=r.$createElement,e=r._self._c||t;return e("div",{staticClass:"form-group",class:{error:r.hasErrors}},[r._t("label",[r.label?e("label",{class:{"is-invalid-label":r.hasErrors}},[r._v(r._s(r.label)+" "+r._s(r.errors?"*":""))]):r._e()]),r._v(" "),r._t("default",null,{attributes:r.attributes,errorMessages:r.activeErrorMessages,errors:r.activeErrors,events:r.events,firstErrorMessage:r.firstErrorMessage,hasErrors:r.hasErrors,validator:r.preferredValidator}),r._v(" "),r._t("errors",[r.hasErrors?e("div",{staticClass:"form-error is-visible"},[r.showSingleError?e("div",{attrs:{"data-validation-attr":r.firstError.validationKey}},[r._v("\n "+r._s(r.firstErrorMessage)+"\n ")]):r._l(r.activeErrorMessages,function(t,i){return e("div",{key:r.activeErrors[i].validationKey,attrs:{"data-validation-attr":r.activeErrors[i].validationKey}},[r._v("\n "+r._s(t)+"\n ")])})],2):r._e()],{errors:r.activeErrors,errorMessages:r.activeErrorMessages,hasErrors:r.hasErrors,firstErrorMessage:r.firstErrorMessage})],2)},V=[];m._withStripped=!0;var M=$({render:m,staticRenderFns:V},void 0,b,void 0,!1,void 0,void 0,void 0);r.default=M,Object.defineProperty(r,"__esModule",{value:!0})});