UNPKG

vuelidate-error-extractor

Version:
6 lines 7.32 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.VuelidateErrorExtractorsingleErrorExtractorBootstrap3={})}(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 n(r){return t(r)||Array.isArray(r)||"function"==typeof r}function s(r,t,e){return v(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 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=s(r,t,!1);return i?u(i,e):t}function d(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):d(t,e)}function p(r,t,e,i,a,n,s,o,u,l){"boolean"!=typeof s&&(u=o,o=s,s=!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(n?(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(n)},f._ssrRegister=d):t&&(d=s?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 v=function(r,s,o){if(t(o)||(o={default:o}),!n(r))return void 0!==o.default?o.default:r;"number"==typeof s&&(s=String(s));var u=Array.isArray(s),l="string"==typeof s,f=o.separator||".",d=o.joinChar||("string"==typeof f?f:".");if(!l&&!u)return r;if(l&&s in r)return a(s,r,o)?r[s]:o.default;var c=u?s:i(s,f,o),h=c.length,p=0;do{var v=c[p];for("number"==typeof v&&(v=String(v));v&&"\\"===v.slice(-1);)v=e([v.slice(0,-1),c[++p]||""],d,o);if(v in r){if(!a(v,r,o))return o.default;r=r[v]}else{for(var E=!1,g=p+1;g<h;)if(v=e([v,c[g++]],d,o),E=v in r){if(!a(v,r,o))return o.default;r=r[v],p=g-1;break}if(!E)return o.default}}while(++p<h&&n(r));return p===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 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?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 a=t[i];if(e.hasOwnProperty(i)){var n=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,n)}var s=Object.assign({},a,r.validatorParams);return delete s.type,l.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={mixins:[_],computed:{attributes:function(){return{class:{"form-control":!0},name:this.name||void 0}}}},$=p,b=y,m=function(){var r=this,t=r.$createElement,e=r._self._c||t;return e("div",{staticClass:"form-group",class:{"has-error":r.hasErrors,"has-success":r.isValid}},[r._t("label",[r.label?e("label",{staticClass:"control-label"},[r._v("\n "+r._s(r.label)+" "+r._s(r.errors?"*":"")+"\n ")]):r._e()]),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",{staticClass:"help-block"},[r.showSingleError?e("span",{attrs:{"data-validation-attr":r.firstError.validationKey}},[r._v("\n "+r._s(r.firstErrorMessage)+"\n ")]):r._e(),r._v(" "),r.showSingleError?r._e():r._l(r.activeErrors,function(t){return e("span",{key:t.validationKey,attrs:{"data-validation-attr":t.validationKey}},[r._v("\n "+r._s(r.getErrorMessage(t.validationKey,t.params))+"\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})});