UNPKG

vuelidate-error-extractor

Version:
6 lines 13.6 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.VuelidateErrorExtractor={})}(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 s(r,t,e){return"function"==typeof e.split?e.split(r):r.split(t)}function i(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 o(r,t,e){return _(r,t,{default:e})}function n(r){return"[vuelidate-error-extractor]: "+r}function u(r,t){if("string"!=typeof r)throw new TypeError(n("Expected a string in the first argument, got "+typeof r));if("object"!=typeof t)throw new TypeError(n("Expected an Object/Array in the second argument, got "+typeof t));var e=/{(.*?)}/g;return r.replace(e,function(r,e){return o(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 d(r,t){return Object.entries(r).filter(function(r){var t=r[0];r[1];return!t.startsWith("$")||"$each"===t}).reduce(function(e,s){var i=s[0],a=s[1];if("object"==typeof a){var o="$each"!==i&&isNaN(parseInt(i))?t?t+"."+i:i:t;return e.concat(d(a,o))}var n=Object.assign({},r.$params[i]);return delete n.type,e.push({fieldName:t,validationKey:i,hasError:!a,params:n,$dirty:r.$dirty,$error:r.$error,$invalid:r.$invalid}),e},[])}function c(r,t,e){var s=o(r,t,!1);return s?u(s,e):t}function f(r,t){var e=t.replace(m,"");return o(r,e,e)}function v(r,t){var e=t.replace(m,""),s=o(r,e);return s?this.$t(s):this.$_VEE_i18nDefaultAttribute?this.$t(this.$_VEE_i18nDefaultAttribute+"."+e):e}function h(r,t,e){return this.$_VEE_hasI18n&&this.$_VEE_hasI18nAttributes?v.call(this,r,e):f(t,e)}function E(r,t,e,s,i,a,o,n,u,l){"boolean"!=typeof o&&(u=n,n=o,o=!1);var d="function"==typeof e?e.options:e;r&&r.render&&(d.render=r.render,d.staticRenderFns=r.staticRenderFns,d._compiled=!0,i&&(d.functional=!0)),s&&(d._scopeId=s);var c;if(a?(c=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=c):t&&(c=o?function(){t.call(this,l(this.$root.$options.shadowRoot))}:function(r){t.call(this,n(r))}),c)if(d.functional){var f=d.render;d.render=function(r,t){return c.call(t),f(r,t)}}else{var v=d.beforeCreate;d.beforeCreate=v?[].concat(v,c):[c]}return e}function p(r,t){void 0===t&&(t={});var e={i18n:t.i18n||!1,i18nAttributes:t.i18nAttributes,messages:t.messages||{},validationKeys:t.validationKeys||{},attributes:t.attributes||{},name:t.name||"formGroup"};r.prototype.$vuelidateErrorExtractor=e,void 0!==t.template&&r.component(e.name,t.template)}var _=function(r,o,n){if(t(n)||(n={default:n}),!a(r))return void 0!==n.default?n.default:r;"number"==typeof o&&(o=String(o));var u=Array.isArray(o),l="string"==typeof o,d=n.separator||".",c=n.joinChar||("string"==typeof d?d:".");if(!l&&!u)return r;if(l&&o in r)return i(o,r,n)?r[o]:n.default;var f=u?o:s(o,d,n),v=f.length,h=0;do{var E=f[h];for("number"==typeof E&&(E=String(E));E&&"\\"===E.slice(-1);)E=e([E.slice(0,-1),f[++h]||""],c,n);if(E in r){if(!i(E,r,n))return n.default;r=r[E]}else{for(var p=!1,_=h+1;_<v;)if(E=e([E,f[_++]],c,n),p=E in r){if(!i(E,r,n))return n.default;r=r[E],h=_-1;break}if(!p)return n.default}}while(++h<v&&a(r));return h===v?r:n.default},m=/\$each\.\d\./g,b={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=o(this.mergedMessages,r);return e?this.$t(e,t):this.$t(this.$vuelidateErrorExtractor.i18n+"."+r,t)},getPlainMessage:function(r,t){return c(this.mergedMessages,r,t)}}},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:b,computed:{preferredValidator:function(){return this.$options.propsData.hasOwnProperty("validator")?this.validator:this.name?o(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(s){var i=t[s];if(e.hasOwnProperty(s)){var a=e[s].params.reduce(function(t,e){return t[e.ext]=r.validatorParams[e.vue]||i[e.vue],t},{});return l.call(r,e[s].validationKey,s,a)}var o=Object.assign({},i,r.validatorParams);return delete o.type,l.call(r,s,s,o)})},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:[g],computed:{attributes:function(){return{class:{"is-invalid-input":this.hasErrors}}}}},$=E,M=y,x=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,s){return e("div",{key:r.activeErrors[s].validationKey,attrs:{"data-validation-attr":r.activeErrors[s].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=[];x._withStripped=!0;var A=$({render:x,staticRenderFns:V},void 0,M,void 0,!1,void 0,void 0,void 0),j={mixins:[g],computed:{attributes:function(){return{class:{"form-control":!0},name:this.name||void 0}}}},S=j,O=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)},w=[];O._withStripped=!0;var C=$({render:O,staticRenderFns:w},void 0,S,void 0,!1,void 0,void 0,void 0),K={name:"Bootstrap4",mixins:[g],computed:{attributes:function(){return{class:{"form-control":!0,"is-invalid":this.hasErrors,"is-valid":this.isValid},name:this.name||void 0}}}},k=K,R=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)},I=[];R._withStripped=!0;var F=$({render:R,staticRenderFns:I},void 0,k,void 0,!1,void 0,void 0,void 0),P={foundation6:A,bootstrap3:C,bootstrap4:F},N={props:{attributes:{type:Object,default:function(){return{}}}},extends:b,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 r=this;return d(this.preferredValidator).map(function(t){return Object.assign({},t,{params:Object.assign({},t.params,{attribute:r.getResolvedAttribute(t.fieldName)})})})},hasErrors:function(){return!!this.activeErrors.length}},methods:{getResolvedAttribute:function(r){return h.call(this,this.mergedAttributes,this.mergedAttributes,r)}}},T={name:"baseMultiErrorExtractor",extends:N},D=T,W=function(){var r=this,t=r.$createElement,e=r._self._c||t;return e("div",r._l(r.activeErrorMessages,function(t,s){return e("div",{key:s},[r._t("default",[e("div",[r._v(r._s(t))])],{errorMessage:t,error:r.activeErrors[s]})],2)}),0)},q=[];W._withStripped=!0;var B=$({render:W,staticRenderFns:q},void 0,D,void 0,!1,void 0,void 0,void 0),U={inheritAttrs:!1,components:{baseMultiErrorExtractor:B}},X=U,G=function(){var r=this,t=r.$createElement,e=r._self._c||t;return e("base-multi-error-extractor",r._b({staticClass:"has-error",scopedSlots:r._u([{key:"default",fn:function(t){var s=t.errorMessage;return[e("label",{staticClass:"help-block"},[r._v(r._s(s))])]}}])},"base-multi-error-extractor",r.$attrs,!1))},L=[];G._withStripped=!0;var z=$({render:G,staticRenderFns:L},void 0,X,void 0,!1,void 0,void 0,void 0),H={inheritAttrs:!1,components:{baseMultiErrorExtractor:B}},J=H,Q=function(){var r=this,t=r.$createElement,e=r._self._c||t;return e("base-multi-error-extractor",r._b({staticClass:"was-validated",scopedSlots:r._u([{key:"default",fn:function(t){var s=t.errorMessage;return[e("label",{staticClass:"invalid-feedback d-block"},[r._v(r._s(s))])]}}])},"base-multi-error-extractor",r.$attrs,!1))},Y=[];Q._withStripped=!0;var Z=$({render:Q,staticRenderFns:Y},void 0,J,void 0,!1,void 0,void 0,void 0),rr={inheritAttrs:!1,components:{baseMultiErrorExtractor:B}},tr=rr,er=function(){var r=this,t=r.$createElement,e=r._self._c||t;return e("base-multi-error-extractor",r._b({staticStyle:{"margin-top":"1rem"},scopedSlots:r._u([{key:"default",fn:function(t){var s=t.errorMessage;return[e("label",{staticClass:"form-error is-visible"},[r._v(r._s(s))])]}}])},"base-multi-error-extractor",r.$attrs,!1))},sr=[];er._withStripped=!0;var ir=$({render:er,staticRenderFns:sr},void 0,tr,void 0,!1,void 0,void 0,void 0),ar={baseMultiErrorExtractor:B,bootstrap3:z,bootstrap4:Z,foundation6:ir},or={name:"FormWrapper",props:{validator:{type:Object,required:!0},messages:{type:Object,default:function(){return{}}}},render:function(r){return r("div",this.$slots.default)},provide:function(){return{formValidator:this.validator,formMessages:this.messages}}},nr={singleErrorExtractor:P,multiErrorExtractor:ar,FormWrapper:or},ur={minLength:{validationKey:"min.string",params:[{vue:"min",ext:"min"}]},sameAs:{validationKey:"same",params:[{vue:"eq",ext:"other"}]}},lr={laravel:ur};r.default=p,r.singleErrorExtractorMixin=g,r.multiErrorExtractorMixin=N,r.configs=lr,r.templates=nr,r.version="2.4.1",Object.defineProperty(r,"__esModule",{value:!0})});