vuelidate-error-extractor
Version:
Helps showing vuelidate error messages from translation files.
6 lines • 7.02 kB
JavaScript
/*!
* 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.VuelidateErrorExtractorsingleErrorExtractorBootstrap4={})}(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 n(r,t,e){return"function"!=typeof e.isValid||e.isValid(r,t)}function a(r){return t(r)||Array.isArray(r)||"function"==typeof r}function s(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 s(t,e,"")})}function f(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 d(r,t,e){var i=s(r,t,!1);return i?u(i,e):t}function l(r,t){var e=t.replace(E,"");return s(r,e,e)}function c(r,t){var e=t.replace(E,""),i=s(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):l(t,e)}function v(r,t,e,i,n,a,s,o,u,f){"boolean"!=typeof s&&(u=o,o=s,s=!1);var d="function"==typeof e?e.options:e;r&&r.render&&(d.render=r.render,d.staticRenderFns=r.staticRenderFns,d._compiled=!0,n&&(d.functional=!0)),i&&(d._scopeId=i);var l;if(a?(l=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(a)},d._ssrRegister=l):t&&(l=s?function(){t.call(this,f(this.$root.$options.shadowRoot))}:function(r){t.call(this,o(r))}),l)if(d.functional){var c=d.render;d.render=function(r,t){return l.call(t),c(r,t)}}else{var h=d.beforeCreate;d.beforeCreate=h?[].concat(h,l):[l]}return e}var p=function(r,s,o){if(t(o)||(o={default:o}),!a(r))return void 0!==o.default?o.default:r;"number"==typeof s&&(s=String(s));var u=Array.isArray(s),f="string"==typeof s,d=o.separator||".",l=o.joinChar||("string"==typeof d?d:".");if(!f&&!u)return r;if(f&&s in r)return n(s,r,o)?r[s]:o.default;var c=u?s:i(s,d,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]||""],l,o);if(p in r){if(!n(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++]],l,o),E=p in r){if(!n(p,r,o))return o.default;r=r[p],v=g-1;break}if(!E)return o.default}}while(++v<h&&a(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=s(this.mergedMessages,r);return e?this.$t(e,t):this.$t(this.$vuelidateErrorExtractor.i18n+"."+r,t)},getPlainMessage:function(r,t){return d(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?s(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 n=t[i];if(e.hasOwnProperty(i)){var a=e[i].params.reduce(function(t,e){return t[e.ext]=r.validatorParams[e.vue]||n[e.vue],t},{});return f.call(r,e[i].validationKey,i,a)}var s=Object.assign({},n,r.validatorParams);return delete s.type,f.call(r,i,i,s)})},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={name:"Bootstrap4",mixins:[_],computed:{attributes:function(){return{class:{"form-control":!0,"is-invalid":this.hasErrors,"is-valid":this.isValid},name:this.name||void 0}}}},$=v,b=y,m=function(){var r=this,t=r.$createElement,e=r._self._c||t;return e("div",[r._t("label",[e("label",[r._v(r._s(r.label))])]),r._v(" "),r._t("default",null,{attributes:r.attributes,errors:r.activeErrors,events:r.events,firstErrorMessage:r.firstErrorMessage,hasErrors:r.hasErrors,validator:r.preferredValidator}),r._v(" "),r._t("errors",[r.hasErrors?e("div",{class:{"invalid-feedback":r.hasErrors,"valid-feedback":!r.hasErrors}},[r.showSingleError?[r._v(r._s(r.firstErrorMessage))]:r._l(r.activeErrorMessages,function(t){return e("div",{key:t},[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})});