vuelidate-error-extractor
Version:
Helps showing vuelidate error messages from translation files.
6 lines • 4.88 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.VuelidateErrorExtractorMultiErrorExtractorMixin={})}(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 p(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 f(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(f(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 c(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 h(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 l(t,r,e){return this.$_VEE_hasI18n&&this.$_VEE_hasI18nAttributes?h.call(this,t,e):d(r,e)}var p=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),f="string"==typeof a,c=o.separator||".",d=o.joinChar||("string"==typeof c?c:".");if(!f&&!u)return t;if(f&&a in t)return n(a,t,o)?t[a]:o.default;var h=u?a:i(a,c,o),l=h.length,p=0;do{var E=h[p];for("number"==typeof E&&(E=String(E));E&&"\\"===E.slice(-1);)E=e([E.slice(0,-1),h[++p]||""],d,o);if(E in t){if(!n(E,t,o))return o.default;t=t[E]}else{for(var g=!1,v=p+1;v<l;)if(E=e([E,h[v++]],d,o),g=E in t){if(!n(E,t,o))return o.default;t=t[E],p=v-1;break}if(!g)return o.default}}while(++p<l&&s(t));return p===l?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 c(this.mergedMessages,t,r)}}},v={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 f(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 l.call(this,this.mergedAttributes,this.mergedAttributes,t)}}};t.default=v,Object.defineProperty(t,"__esModule",{value:!0})});