vuelidate-error-extractor
Version:
Helps showing vuelidate error messages from translation files.
6 lines • 6.52 kB
JavaScript
/*!
* 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.VuelidateErrorExtractormultiErrorExtractorBootstrap3={})}(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 s(t){return r(t)||Array.isArray(t)||"function"==typeof t}function a(t,r,e){return v(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 a(r,e,"")})}function c(t,r){return Object.entries(t).filter(function(t){var r=t[0];t[1];return!r.startsWith("$")||"$each"===r}).reduce(function(e,i){var n=i[0],s=i[1];if("object"==typeof s){var a="$each"!==n&&isNaN(parseInt(n))?r?r+"."+n:n:r;return e.concat(c(s,a))}var o=Object.assign({},t.$params[n]);return delete o.type,e.push({fieldName:r,validationKey:n,hasError:!s,params:o,$dirty:t.$dirty,$error:t.$error,$invalid:t.$invalid}),e},[])}function f(t,r,e){var i=a(t,r,!1);return i?u(i,e):r}function d(t,r){var e=r.replace(E,"");return a(t,e,e)}function l(t,r){var e=r.replace(E,""),i=a(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?l.call(this,t,e):d(r,e)}function p(t,r,e,i,n,s,a,o,u,c){"boolean"!=typeof a&&(u=o,o=a,a=!1);var f="function"==typeof e?e.options:e;t&&t.render&&(f.render=t.render,f.staticRenderFns=t.staticRenderFns,f._compiled=!0,n&&(f.functional=!0)),i&&(f._scopeId=i);var d;if(s?(d=function(t){t=t||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext,t||"undefined"==typeof __VUE_SSR_CONTEXT__||(t=__VUE_SSR_CONTEXT__),r&&r.call(this,u(t)),t&&t._registeredComponents&&t._registeredComponents.add(s)},f._ssrRegister=d):r&&(d=a?function(){r.call(this,c(this.$root.$options.shadowRoot))}:function(t){r.call(this,o(t))}),d)if(f.functional){var l=f.render;f.render=function(t,r){return d.call(r),l(t,r)}}else{var h=f.beforeCreate;f.beforeCreate=h?[].concat(h,d):[d]}return e}var v=function(t,a,o){if(r(o)||(o={default:o}),!s(t))return void 0!==o.default?o.default:t;"number"==typeof a&&(a=String(a));var u=Array.isArray(a),c="string"==typeof a,f=o.separator||".",d=o.joinChar||("string"==typeof f?f:".");if(!c&&!u)return t;if(c&&a in t)return n(a,t,o)?t[a]:o.default;var l=u?a:i(a,f,o),h=l.length,p=0;do{var v=l[p];for("number"==typeof v&&(v=String(v));v&&"\\"===v.slice(-1);)v=e([v.slice(0,-1),l[++p]||""],d,o);if(v in t){if(!n(v,t,o))return o.default;t=t[v]}else{for(var E=!1,g=p+1;g<h;)if(v=e([v,l[g++]],d,o),E=v in t){if(!n(v,t,o))return o.default;t=t[v],p=g-1;break}if(!E)return o.default}}while(++p<h&&s(t));return p===h?t: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(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=a(this.mergedMessages,t);return e?this.$t(e,r):this.$t(this.$vuelidateErrorExtractor.i18n+"."+t,r)},getPlainMessage:function(t,r){return f(this.mergedMessages,t,r)}}},_={props:{attributes:{type:Object,default:function(){return{}}}},extends:g,computed:{preferredValidator:function(){return this.$options.propsData.hasOwnProperty("validator")?this.validator:this.formValidator},mergedAttributes:function(){return this.$_VEE_hasI18n&&this.$_VEE_hasI18nAttributes?Object.assign({},this.$vuelidateErrorExtractor.i18nAttributes,this.attributes):Object.assign({},this.$vuelidateErrorExtractor.attributes,this.attributes)},errors:function(){var t=this;return c(this.preferredValidator).map(function(r){return Object.assign({},r,{params:Object.assign({},r.params,{attribute:t.getResolvedAttribute(r.fieldName)})})})},hasErrors:function(){return!!this.activeErrors.length}},methods:{getResolvedAttribute:function(t){return h.call(this,this.mergedAttributes,this.mergedAttributes,t)}}},$={name:"baseMultiErrorExtractor",extends:_},b=p,m=$,y=function(){var t=this,r=t.$createElement,e=t._self._c||r;return e("div",t._l(t.activeErrorMessages,function(r,i){return e("div",{key:i},[t._t("default",[e("div",[t._v(t._s(r))])],{errorMessage:r,error:t.activeErrors[i]})],2)}),0)},x=[];y._withStripped=!0;var A=b({render:y,staticRenderFns:x},void 0,m,void 0,!1,void 0,void 0,void 0),V={inheritAttrs:!1,components:{baseMultiErrorExtractor:A}},j=V,M=function(){var t=this,r=t.$createElement,e=t._self._c||r;return e("base-multi-error-extractor",t._b({staticClass:"has-error",scopedSlots:t._u([{key:"default",fn:function(r){var i=r.errorMessage;return[e("label",{staticClass:"help-block"},[t._v(t._s(i))])]}}])},"base-multi-error-extractor",t.$attrs,!1))},O=[];M._withStripped=!0;var I=b({render:M,staticRenderFns:O},void 0,j,void 0,!1,void 0,void 0,void 0);t.default=I,Object.defineProperty(t,"__esModule",{value:!0})});