From 5f0fc7f56bcf79f7bc1c22281d6e7dbab3794107 Mon Sep 17 00:00:00 2001 From: 1on Date: Wed, 28 Oct 2020 16:22:18 +0300 Subject: [PATCH] 0.3.1 --- dist/formulario.esm.js | 2 +- dist/formulario.min.js | 2 +- dist/formulario.umd.js | 2 +- package.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dist/formulario.esm.js b/dist/formulario.esm.js index cbf81d9..126ac90 100644 --- a/dist/formulario.esm.js +++ b/dist/formulario.esm.js @@ -13,4 +13,4 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ -function y(e,t){var r={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(r[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o=0;n--)(o=e[n])&&(a=(s<3?o(a):s>3?o(t,r,a):o(t,r))||a);return s>3&&a&&Object.defineProperty(t,r,a),a}class b{constructor(e){this.registry=new Map,this.ctx=e}add(e,t){this.registry.set(e,t)}remove(e){this.registry.delete(e);const t=this.ctx.proxy,r=e,i=(t[r],y(t,["symbol"==typeof r?r:r+""]));this.ctx.proxy=i}has(e){return this.registry.has(e)}hasNested(e){for(const t of this.registry.keys())if(t===e||t.includes(e+"."))return!0;return!1}get(e){return this.registry.get(e)}getNested(e){const t=new Map;for(const r of this.registry.keys())(r===e||r.includes(e+"."))&&t.set(r,this.registry.get(r));return t}forEach(e){this.registry.forEach((t,r)=>{e(t,r)})}keys(){return Array.from(this.registry.keys())}register(e,t){if(this.registry.has(e))return;this.registry.set(e,t);const r=u(t.$options.propsData||{},"value");!r&&this.ctx.hasInitialValue&&void 0!==p(this.ctx.initialValues,e)?t.context.model=p(this.ctx.initialValues,e):r&&!c(t.proxy,p(this.ctx.initialValues,e))&&this.ctx.setFieldValue(e,t.proxy)}reduce(e,t){return this.registry.forEach((r,i)=>{t=e(t,r,i)}),t}}class E{constructor(e=[]){this.observers=[],this.observers=e}add(e){this.observers.some(t=>t.callback===e.callback)||this.observers.push(e)}remove(e){this.observers=this.observers.filter(t=>t.callback!==e)}filter(e){return new E(this.observers.filter(e))}some(e){return this.observers.some(e)}observe(e){this.observers.forEach(t=>{"form"===t.type?t.callback(e):t.field&&!Array.isArray(e)&&u(e,t.field)&&t.callback(e[t.field])})}}let x=class extends r{constructor(){super(...arguments),this.path="",this.proxy={},this.registry=new b(this),this.errorObserverRegistry=new E,this.localFormErrors=[],this.localFieldErrors={}}get initialValues(){return this.hasModel&&"object"==typeof this.formularioValue?Object.assign({},this.formularioValue):{}}get mergedFormErrors(){return[...this.formErrors,...this.localFormErrors]}get mergedFieldErrors(){return d(this.errors||{},this.localFieldErrors)}get hasModel(){return u(this.$options.propsData||{},"formularioValue")}get hasInitialValue(){return this.formularioValue&&"object"==typeof this.formularioValue}onFormularioValueChanged(e){this.hasModel&&e&&"object"==typeof e&&this.setValues(e)}onMergedFormErrorsChanged(e){this.errorObserverRegistry.filter(e=>"form"===e.type).observe(e)}onMergedFieldErrorsChanged(e){this.errorObserverRegistry.filter(e=>"input"===e.type).observe(e)}created(){this.initProxy()}getFormValues(){return this.proxy}onFormSubmit(){return this.hasValidationErrors().then(e=>e?void 0:function e(t){if("object"!=typeof t)return t;const r=Array.isArray(t)?[]:{};for(const i in t)u(t,i)&&(r[i]=h(t[i])?t[i]:e(t[i]));return r}(this.proxy)).then(e=>{void 0!==e?this.$emit("submit",e):this.$emit("error")})}onFormularioFieldValidation(e){this.$emit("validation",e)}addErrorObserver(e){this.errorObserverRegistry.add(e),"form"===e.type?e.callback(this.mergedFormErrors):e.field&&u(this.mergedFieldErrors,e.field)&&e.callback(this.mergedFieldErrors[e.field])}removeErrorObserver(e){this.errorObserverRegistry.remove(e)}register(e,t){this.registry.register(e,t)}deregister(e){this.registry.remove(e)}initProxy(){this.hasInitialValue&&(this.proxy=this.initialValues)}setValues(e){const t=Array.from(new Set([...Object.keys(e),...Object.keys(this.proxy)]));let r=!1;t.forEach(t=>{this.registry.hasNested(t)&&this.registry.getNested(t).forEach((t,i)=>{const o=this.registry.get(i),s=p(this.proxy,i),a=p(e,i);c(a,s)||(this.setFieldValue(i,a,!1),r=!0),c(a,o.proxy)||(o.context.model=a)})}),this.initProxy(),r&&this.$emit("input",Object.assign({},this.proxy))}setFieldValue(e,t,r=!0){if(void 0===t){const t=this.proxy,r=e,i=(t[r],y(t,["symbol"==typeof r?r:r+""]));this.proxy=i}else!function(e,t,r){const i=t.split(".");let o=e;for(let e=0;e(e.push(t.runValidation()&&t.hasValidationErrors()),e),[])).then(e=>e.some(e=>e))}resetValidation(){this.localFormErrors=[],this.localFieldErrors={},this.registry.forEach(e=>{e.resetValidation()})}};function $(e,t,r,i,o,s,a,n,l,u){"boolean"!=typeof a&&(l=n,n=a,a=!1);const d="function"==typeof r?r.options:r;let h;if(e&&e.render&&(d.render=e.render,d.staticRenderFns=e.staticRenderFns,d._compiled=!0,o&&(d.functional=!0)),i&&(d._scopeId=i),s?(h=function(e){(e=e||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext)||"undefined"==typeof __VUE_SSR_CONTEXT__||(e=__VUE_SSR_CONTEXT__),t&&t.call(this,l(e)),e&&e._registeredComponents&&e._registeredComponents.add(s)},d._ssrRegister=h):t&&(h=a?function(e){t.call(this,u(e,this.$root.$options.shadowRoot))}:function(e){t.call(this,n(e))}),h)if(d.functional){const e=d.render;d.render=function(t,r){return h.call(r),e(t,r)}}else{const e=d.beforeCreate;d.beforeCreate=e?[].concat(e,h):[h]}return r}g([i("input",{default:()=>({})})],x.prototype,"formularioValue",void 0),g([o({default:()=>({})})],x.prototype,"errors",void 0),g([o({default:()=>[]})],x.prototype,"formErrors",void 0),g([s()],x.prototype,"path",void 0),g([a("formularioValue",{deep:!0})],x.prototype,"onFormularioValueChanged",null),g([a("mergedFormErrors")],x.prototype,"onMergedFormErrorsChanged",null),g([a("mergedFieldErrors",{immediate:!0})],x.prototype,"onMergedFieldErrorsChanged",null),g([s()],x.prototype,"getFormValues",null),g([s()],x.prototype,"onFormularioFieldValidation",null),g([s()],x.prototype,"addErrorObserver",null),g([s()],x.prototype,"removeErrorObserver",null),g([s("formularioRegister")],x.prototype,"register",null),g([s("formularioDeregister")],x.prototype,"deregister",null),g([s("formularioSetter")],x.prototype,"setFieldValue",null),x=g([n({name:"FormularioForm"})],x);const N=x;var F=function(){var e=this,t=e.$createElement;return(e._self._c||t)("form",{on:{submit:function(t){return t.preventDefault(),e.onFormSubmit(t)}}},[e._t("default",null,{errors:e.mergedFormErrors})],2)};F._withStripped=!0;const V=$({render:F,staticRenderFns:[]},void 0,N,void 0,!1,void 0,!1,void 0,void 0,void 0);let O=class extends r{get groupPath(){return this.isArrayItem?`${this.path}[${this.name}]`:""===this.path?this.name:`${this.path}.${this.name}`}};g([l({default:""})],O.prototype,"path",void 0),g([o({required:!0})],O.prototype,"name",void 0),g([o({default:!1})],O.prototype,"isArrayItem",void 0),g([s("path")],O.prototype,"groupPath",null),O=g([n({name:"FormularioGrouping"})],O);const P=O;var R=function(){var e=this.$createElement;return(this._self._c||e)("div",[this._t("default")],2)};R._withStripped=!0;const j=$({render:R,staticRenderFns:[]},void 0,P,void 0,!1,void 0,!1,void 0,void 0,void 0);function w(e,t,r,i){return o=>Promise.resolve(e(o,...r)).then(e=>e?null:{rule:t,args:r,context:o,message:i(o,...r)})}function A(e){return/^[\^]/.test(e.charAt(0))?[f(e.substr(1)),e.charAt(0)]:[f(e),null]}function _(e,t,r){return"function"==typeof e?[e,null,null]:Array.isArray(e)&&e.length?function(e,t,r){const i=e.slice(),o=i.shift();if("function"==typeof o)return[o,null,null];if("string"!=typeof o)throw new Error("[Formulario]: For array constraint first element must be rule name or Validator function");const[s,a]=A(o);if(u(t,s))return[w(t[s],s,i,r[s]||r.default),s,a];throw new Error("[Formulario] Can't create validator for constraint: "+JSON.stringify(e))}(e,t,r):"string"==typeof e?function(e,t,r){const i=e.split(":"),[o,s]=A(i.shift()||"");if(u(t,o))return[w(t[o],o,i.length?i.join(":").split(","):[],r[o]||r.default),o,s];throw new Error("[Formulario] Can't create validator for constraint: "+e)}(e,t,r):[()=>Promise.resolve(null),null,null]}function D(e){const t=[];if(e.length){let r=e.shift();t.push(r),e.forEach(e=>{e.bail||e.bail!==r.bail?(r=Object.assign({},e),t.push(r)):r.validators.push(...e.validators)})}return t}function M(e,t){return new Promise(r=>{const i=(e,o=[])=>{if(e.length){const s=e.shift();(function(e,t){return Promise.all(e.validators.map(e=>e(t))).then(e=>e.filter(e=>null!==e))})(s,t).then(t=>0!==t.length&&s.bail||!e.length?r(o.concat(t)):i(e,o.concat(t)))}else r([])};i(function(e){const t=([e,,t])=>({validators:[e],bail:"^"===t}),r=[],i=e.findIndex(([,e])=>e&&"bail"===e.toLowerCase());return i>=0?(r.push(...D(e.splice(0,i+1).slice(0,-1).map(t))),r.push(...e.map(([e])=>({validators:[e],bail:!0})))):r.push(...D(e.map(t))),r}(e))})}const k={DEMAND:"demand",LIVE:"live",SUBMIT:"submit"};let C=class extends r{constructor(){super(...arguments),this.proxy=this.getInitialValue(),this.localErrors=[],this.violations=[],this.validationRun=Promise.resolve()}get fullQualifiedName(){return""!==this.path?`${this.path}.${this.name}`:this.name}get model(){const e=this.hasModel?"value":"proxy";return void 0===this[e]?"":this[e]}set model(e){c(e,this.proxy)||(this.proxy=e),this.$emit("input",e),"function"==typeof this.formularioSetter&&this.formularioSetter(this.context.name,e)}get context(){return Object.defineProperty({name:this.fullQualifiedName,runValidation:this.runValidation.bind(this),violations:this.violations,errors:this.localErrors,allErrors:[...this.localErrors,...this.violations.map(e=>e.message)]},"model",{get:()=>this.model,set:e=>{this.model=e}})}get normalizedValidationRules(){const e={};return Object.keys(this.validationRules).forEach(t=>{e[f(t)]=this.validationRules[t]}),e}get normalizedValidationMessages(){const e={};return Object.keys(this.validationMessages).forEach(t=>{e[f(t)]=this.validationMessages[t]}),e}get hasModel(){return u(this.$options.propsData||{},"value")}onProxyChanged(e,t){this.hasModel||c(e,t)||(this.context.model=e),this.validationBehavior===k.LIVE?this.runValidation():this.violations=[]}onValueChanged(e,t){this.hasModel&&!c(e,t)&&(this.context.model=e)}created(){this.initProxy(),"function"==typeof this.formularioRegister&&this.formularioRegister(this.fullQualifiedName,this),"function"!=typeof this.addErrorObserver||this.errorsDisabled||this.addErrorObserver({callback:this.setErrors,type:"input",field:this.fullQualifiedName}),this.validationBehavior===k.LIVE&&this.runValidation()}beforeDestroy(){this.errorsDisabled||"function"!=typeof this.removeErrorObserver||this.removeErrorObserver(this.setErrors),"function"==typeof this.formularioDeregister&&this.formularioDeregister(this.fullQualifiedName)}getInitialValue(){return u(this.$options.propsData||{},"value")?this.value:""}initProxy(){c(this.context.model,this.proxy)||(this.context.model=this.proxy)}runValidation(){return this.validationRun=this.validate().then(e=>{const t=!c(e,this.violations);if(this.violations=e,t){const e={name:this.context.name,violations:this.violations};this.$emit("validation",e),"function"==typeof this.onFormularioFieldValidation&&this.onFormularioFieldValidation(e)}return this.violations}),this.validationRun}validate(){return M(function e(t,r,i){return"string"==typeof t?e(t.split("|").filter(e=>e.length),r,i):Array.isArray(t)?t.map(e=>_(e,r,i)):[]}(this.validation,this.$formulario.getRules(this.normalizedValidationRules),this.$formulario.getMessages(this,this.normalizedValidationMessages)),{value:this.context.model,name:this.context.name,formValues:this.getFormValues()})}hasValidationErrors(){return new Promise(e=>{this.$nextTick(()=>{this.validationRun.then(()=>e(this.violations.length>0))})})}setErrors(e){var t;this.localErrors=(t=e)?"string"==typeof t?[t]:Array.isArray(t)?t:"object"==typeof t?Object.values(t):[]:[]}resetValidation(){this.localErrors=[],this.violations=[]}};g([l({default:void 0})],C.prototype,"formularioSetter",void 0),g([l({default:()=>()=>{}})],C.prototype,"onFormularioFieldValidation",void 0),g([l({default:void 0})],C.prototype,"formularioRegister",void 0),g([l({default:void 0})],C.prototype,"formularioDeregister",void 0),g([l({default:()=>()=>({})})],C.prototype,"getFormValues",void 0),g([l({default:void 0})],C.prototype,"addErrorObserver",void 0),g([l({default:void 0})],C.prototype,"removeErrorObserver",void 0),g([l({default:""})],C.prototype,"path",void 0),g([i("input",{default:""})],C.prototype,"value",void 0),g([o({required:!0,validator:e=>"string"==typeof e&&e.length>0})],C.prototype,"name",void 0),g([o({default:""})],C.prototype,"validation",void 0),g([o({default:()=>({})})],C.prototype,"validationRules",void 0),g([o({default:()=>({})})],C.prototype,"validationMessages",void 0),g([o({default:k.DEMAND,validator:e=>Object.values(k).includes(e)})],C.prototype,"validationBehavior",void 0),g([o({default:!1})],C.prototype,"errorsDisabled",void 0),g([a("proxy")],C.prototype,"onProxyChanged",null),g([a("value")],C.prototype,"onValueChanged",null),C=g([n({name:"FormularioInput",inheritAttrs:!1})],C);const S=C;var I=function(){var e=this.$createElement;return(this._self._c||e)("div",{staticClass:"formulario-input"},[this._t("default",null,{context:this.context})],2)};I._withStripped=!0;const T=$({render:I,staticRenderFns:[]},void 0,S,void 0,!1,void 0,!1,void 0,void 0,void 0);var z=new class{constructor(){this.validationRules={},this.validationMessages={},this.validationRules=v,this.validationMessages=m}install(e,t){e.prototype.$formulario=this,e.component("FormularioForm",V),e.component("FormularioGrouping",j),e.component("FormularioInput",T),this.extend(t||{})}extend(e){if("object"==typeof e)return this.validationRules=d(this.validationRules,e.validationRules||{}),this.validationMessages=d(this.validationMessages,e.validationMessages||{}),this;throw new Error(`[Formulario]: Formulario.extend() should be passed an object (was ${typeof e})`)}getRules(e={}){return d(this.validationRules,e)}getMessages(e,t){const r=d(this.validationMessages||{},t),i={};for(const t in r)i[t]=(i,...o)=>"string"==typeof r[t]?r[t]:r[t](e,i,...o);return i}};export default z; +function y(e,t){var r={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(r[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o=0;n--)(o=e[n])&&(a=(s<3?o(a):s>3?o(t,r,a):o(t,r))||a);return s>3&&a&&Object.defineProperty(t,r,a),a}class b{constructor(e){this.registry=new Map,this.ctx=e}add(e,t){this.registry.set(e,t)}remove(e){this.registry.delete(e);const t=this.ctx.proxy,r=e,i=(t[r],y(t,["symbol"==typeof r?r:r+""]));this.ctx.proxy=i}has(e){return this.registry.has(e)}hasNested(e){for(const t of this.registry.keys())if(t===e||t.includes(e+"."))return!0;return!1}get(e){return this.registry.get(e)}getNested(e){const t=new Map;for(const r of this.registry.keys())(r===e||r.includes(e+"."))&&t.set(r,this.registry.get(r));return t}forEach(e){this.registry.forEach((t,r)=>{e(t,r)})}keys(){return Array.from(this.registry.keys())}register(e,t){if(this.registry.has(e))return;this.registry.set(e,t);const r=u(t.$options.propsData||{},"value");!r&&this.ctx.hasInitialValue&&void 0!==p(this.ctx.initialValues,e)?t.context.model=p(this.ctx.initialValues,e):r&&!c(t.proxy,p(this.ctx.initialValues,e))&&this.ctx.setFieldValue(e,t.proxy)}reduce(e,t){return this.registry.forEach((r,i)=>{t=e(t,r,i)}),t}}class E{constructor(e=[]){this.observers=[],this.observers=e}add(e){this.observers.some(t=>t.callback===e.callback)||this.observers.push(e)}remove(e){this.observers=this.observers.filter(t=>t.callback!==e)}filter(e){return new E(this.observers.filter(e))}some(e){return this.observers.some(e)}observe(e){this.observers.forEach(t=>{"form"===t.type?t.callback(e):t.field&&!Array.isArray(e)&&u(e,t.field)&&t.callback(e[t.field])})}}let x=class extends r{constructor(){super(...arguments),this.path="",this.proxy={},this.registry=new b(this),this.errorObserverRegistry=new E,this.localFormErrors=[],this.localFieldErrors={}}get initialValues(){return this.hasModel&&"object"==typeof this.formularioValue?Object.assign({},this.formularioValue):{}}get mergedFormErrors(){return[...this.formErrors,...this.localFormErrors]}get mergedFieldErrors(){return d(this.errors||{},this.localFieldErrors)}get hasModel(){return u(this.$options.propsData||{},"formularioValue")}get hasInitialValue(){return this.formularioValue&&"object"==typeof this.formularioValue}onFormularioValueChanged(e){this.hasModel&&e&&"object"==typeof e&&this.setValues(e)}onMergedFormErrorsChanged(e){this.errorObserverRegistry.filter(e=>"form"===e.type).observe(e)}onMergedFieldErrorsChanged(e){this.errorObserverRegistry.filter(e=>"input"===e.type).observe(e)}created(){this.initProxy()}getFormValues(){return this.proxy}onFormSubmit(){return this.hasValidationErrors().then(e=>e?void 0:function e(t){if("object"!=typeof t)return t;const r=Array.isArray(t)?[]:{};for(const i in t)u(t,i)&&(r[i]=h(t[i])?t[i]:e(t[i]));return r}(this.proxy)).then(e=>{void 0!==e?this.$emit("submit",e):this.$emit("error")})}onFormularioFieldValidation(e){this.$emit("validation",e)}addErrorObserver(e){this.errorObserverRegistry.add(e),"form"===e.type?e.callback(this.mergedFormErrors):e.field&&u(this.mergedFieldErrors,e.field)&&e.callback(this.mergedFieldErrors[e.field])}removeErrorObserver(e){this.errorObserverRegistry.remove(e)}register(e,t){this.registry.register(e,t)}deregister(e){this.registry.remove(e)}initProxy(){this.hasInitialValue&&(this.proxy=this.initialValues)}setValues(e){const t=Array.from(new Set([...Object.keys(e),...Object.keys(this.proxy)]));let r=!1;t.forEach(t=>{this.registry.hasNested(t)&&this.registry.getNested(t).forEach((t,i)=>{const o=this.registry.get(i),s=p(this.proxy,i),a=p(e,i);c(a,s)||(this.setFieldValue(i,a,!1),r=!0),c(a,o.proxy)||(o.context.model=a)})}),this.initProxy(),r&&this.$emit("input",Object.assign({},this.proxy))}setFieldValue(e,t,r=!0){if(void 0===t){const t=this.proxy,r=e,i=(t[r],y(t,["symbol"==typeof r?r:r+""]));this.proxy=i}else!function(e,t,r){const i=t.split(".");let o=e;for(let e=0;e(e.push(t.runValidation()&&t.hasValidationErrors()),e),[])).then(e=>e.some(e=>e))}resetValidation(){this.localFormErrors=[],this.localFieldErrors={},this.registry.forEach(e=>{e.resetValidation()})}};function $(e,t,r,i,o,s,a,n,l,u){"boolean"!=typeof a&&(l=n,n=a,a=!1);const d="function"==typeof r?r.options:r;let h;if(e&&e.render&&(d.render=e.render,d.staticRenderFns=e.staticRenderFns,d._compiled=!0,o&&(d.functional=!0)),i&&(d._scopeId=i),s?(h=function(e){(e=e||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext)||"undefined"==typeof __VUE_SSR_CONTEXT__||(e=__VUE_SSR_CONTEXT__),t&&t.call(this,l(e)),e&&e._registeredComponents&&e._registeredComponents.add(s)},d._ssrRegister=h):t&&(h=a?function(e){t.call(this,u(e,this.$root.$options.shadowRoot))}:function(e){t.call(this,n(e))}),h)if(d.functional){const e=d.render;d.render=function(t,r){return h.call(r),e(t,r)}}else{const e=d.beforeCreate;d.beforeCreate=e?[].concat(e,h):[h]}return r}g([i("input",{default:()=>({})})],x.prototype,"formularioValue",void 0),g([o({default:()=>({})})],x.prototype,"errors",void 0),g([o({default:()=>[]})],x.prototype,"formErrors",void 0),g([s()],x.prototype,"path",void 0),g([a("formularioValue",{deep:!0})],x.prototype,"onFormularioValueChanged",null),g([a("mergedFormErrors")],x.prototype,"onMergedFormErrorsChanged",null),g([a("mergedFieldErrors",{deep:!0,immediate:!0})],x.prototype,"onMergedFieldErrorsChanged",null),g([s()],x.prototype,"getFormValues",null),g([s()],x.prototype,"onFormularioFieldValidation",null),g([s()],x.prototype,"addErrorObserver",null),g([s()],x.prototype,"removeErrorObserver",null),g([s("formularioRegister")],x.prototype,"register",null),g([s("formularioDeregister")],x.prototype,"deregister",null),g([s("formularioSetter")],x.prototype,"setFieldValue",null),x=g([n({name:"FormularioForm"})],x);const N=x;var F=function(){var e=this,t=e.$createElement;return(e._self._c||t)("form",{on:{submit:function(t){return t.preventDefault(),e.onFormSubmit(t)}}},[e._t("default",null,{errors:e.mergedFormErrors})],2)};F._withStripped=!0;const V=$({render:F,staticRenderFns:[]},void 0,N,void 0,!1,void 0,!1,void 0,void 0,void 0);let O=class extends r{get groupPath(){return this.isArrayItem?`${this.path}[${this.name}]`:""===this.path?this.name:`${this.path}.${this.name}`}};g([l({default:""})],O.prototype,"path",void 0),g([o({required:!0})],O.prototype,"name",void 0),g([o({default:!1})],O.prototype,"isArrayItem",void 0),g([s("path")],O.prototype,"groupPath",null),O=g([n({name:"FormularioGrouping"})],O);const P=O;var R=function(){var e=this.$createElement;return(this._self._c||e)("div",[this._t("default")],2)};R._withStripped=!0;const j=$({render:R,staticRenderFns:[]},void 0,P,void 0,!1,void 0,!1,void 0,void 0,void 0);function w(e,t,r,i){return o=>Promise.resolve(e(o,...r)).then(e=>e?null:{rule:t,args:r,context:o,message:i(o,...r)})}function A(e){return/^[\^]/.test(e.charAt(0))?[f(e.substr(1)),e.charAt(0)]:[f(e),null]}function _(e,t,r){return"function"==typeof e?[e,null,null]:Array.isArray(e)&&e.length?function(e,t,r){const i=e.slice(),o=i.shift();if("function"==typeof o)return[o,null,null];if("string"!=typeof o)throw new Error("[Formulario]: For array constraint first element must be rule name or Validator function");const[s,a]=A(o);if(u(t,s))return[w(t[s],s,i,r[s]||r.default),s,a];throw new Error("[Formulario] Can't create validator for constraint: "+JSON.stringify(e))}(e,t,r):"string"==typeof e?function(e,t,r){const i=e.split(":"),[o,s]=A(i.shift()||"");if(u(t,o))return[w(t[o],o,i.length?i.join(":").split(","):[],r[o]||r.default),o,s];throw new Error("[Formulario] Can't create validator for constraint: "+e)}(e,t,r):[()=>Promise.resolve(null),null,null]}function D(e){const t=[];if(e.length){let r=e.shift();t.push(r),e.forEach(e=>{e.bail||e.bail!==r.bail?(r=Object.assign({},e),t.push(r)):r.validators.push(...e.validators)})}return t}function M(e,t){return new Promise(r=>{const i=(e,o=[])=>{if(e.length){const s=e.shift();(function(e,t){return Promise.all(e.validators.map(e=>e(t))).then(e=>e.filter(e=>null!==e))})(s,t).then(t=>0!==t.length&&s.bail||!e.length?r(o.concat(t)):i(e,o.concat(t)))}else r([])};i(function(e){const t=([e,,t])=>({validators:[e],bail:"^"===t}),r=[],i=e.findIndex(([,e])=>e&&"bail"===e.toLowerCase());return i>=0?(r.push(...D(e.splice(0,i+1).slice(0,-1).map(t))),r.push(...e.map(([e])=>({validators:[e],bail:!0})))):r.push(...D(e.map(t))),r}(e))})}const k={DEMAND:"demand",LIVE:"live",SUBMIT:"submit"};let C=class extends r{constructor(){super(...arguments),this.proxy=this.getInitialValue(),this.localErrors=[],this.violations=[],this.validationRun=Promise.resolve()}get fullQualifiedName(){return""!==this.path?`${this.path}.${this.name}`:this.name}get model(){const e=this.hasModel?"value":"proxy";return void 0===this[e]?"":this[e]}set model(e){c(e,this.proxy)||(this.proxy=e),this.$emit("input",e),"function"==typeof this.formularioSetter&&this.formularioSetter(this.context.name,e)}get context(){return Object.defineProperty({name:this.fullQualifiedName,runValidation:this.runValidation.bind(this),violations:this.violations,errors:this.localErrors,allErrors:[...this.localErrors,...this.violations.map(e=>e.message)]},"model",{get:()=>this.model,set:e=>{this.model=e}})}get normalizedValidationRules(){const e={};return Object.keys(this.validationRules).forEach(t=>{e[f(t)]=this.validationRules[t]}),e}get normalizedValidationMessages(){const e={};return Object.keys(this.validationMessages).forEach(t=>{e[f(t)]=this.validationMessages[t]}),e}get hasModel(){return u(this.$options.propsData||{},"value")}onProxyChanged(e,t){this.hasModel||c(e,t)||(this.context.model=e),this.validationBehavior===k.LIVE?this.runValidation():this.violations=[]}onValueChanged(e,t){this.hasModel&&!c(e,t)&&(this.context.model=e)}created(){this.initProxy(),"function"==typeof this.formularioRegister&&this.formularioRegister(this.fullQualifiedName,this),"function"!=typeof this.addErrorObserver||this.errorsDisabled||this.addErrorObserver({callback:this.setErrors,type:"input",field:this.fullQualifiedName}),this.validationBehavior===k.LIVE&&this.runValidation()}beforeDestroy(){this.errorsDisabled||"function"!=typeof this.removeErrorObserver||this.removeErrorObserver(this.setErrors),"function"==typeof this.formularioDeregister&&this.formularioDeregister(this.fullQualifiedName)}getInitialValue(){return u(this.$options.propsData||{},"value")?this.value:""}initProxy(){c(this.context.model,this.proxy)||(this.context.model=this.proxy)}runValidation(){return this.validationRun=this.validate().then(e=>{const t=!c(e,this.violations);if(this.violations=e,t){const e={name:this.context.name,violations:this.violations};this.$emit("validation",e),"function"==typeof this.onFormularioFieldValidation&&this.onFormularioFieldValidation(e)}return this.violations}),this.validationRun}validate(){return M(function e(t,r,i){return"string"==typeof t?e(t.split("|").filter(e=>e.length),r,i):Array.isArray(t)?t.map(e=>_(e,r,i)):[]}(this.validation,this.$formulario.getRules(this.normalizedValidationRules),this.$formulario.getMessages(this,this.normalizedValidationMessages)),{value:this.context.model,name:this.context.name,formValues:this.getFormValues()})}hasValidationErrors(){return new Promise(e=>{this.$nextTick(()=>{this.validationRun.then(()=>e(this.violations.length>0))})})}setErrors(e){var t;this.localErrors=(t=e)?"string"==typeof t?[t]:Array.isArray(t)?t:"object"==typeof t?Object.values(t):[]:[]}resetValidation(){this.localErrors=[],this.violations=[]}};g([l({default:void 0})],C.prototype,"formularioSetter",void 0),g([l({default:()=>()=>{}})],C.prototype,"onFormularioFieldValidation",void 0),g([l({default:void 0})],C.prototype,"formularioRegister",void 0),g([l({default:void 0})],C.prototype,"formularioDeregister",void 0),g([l({default:()=>()=>({})})],C.prototype,"getFormValues",void 0),g([l({default:void 0})],C.prototype,"addErrorObserver",void 0),g([l({default:void 0})],C.prototype,"removeErrorObserver",void 0),g([l({default:""})],C.prototype,"path",void 0),g([i("input",{default:""})],C.prototype,"value",void 0),g([o({required:!0,validator:e=>"string"==typeof e&&e.length>0})],C.prototype,"name",void 0),g([o({default:""})],C.prototype,"validation",void 0),g([o({default:()=>({})})],C.prototype,"validationRules",void 0),g([o({default:()=>({})})],C.prototype,"validationMessages",void 0),g([o({default:k.DEMAND,validator:e=>Object.values(k).includes(e)})],C.prototype,"validationBehavior",void 0),g([o({default:!1})],C.prototype,"errorsDisabled",void 0),g([a("proxy")],C.prototype,"onProxyChanged",null),g([a("value")],C.prototype,"onValueChanged",null),C=g([n({name:"FormularioInput",inheritAttrs:!1})],C);const S=C;var I=function(){var e=this.$createElement;return(this._self._c||e)("div",{staticClass:"formulario-input"},[this._t("default",null,{context:this.context})],2)};I._withStripped=!0;const T=$({render:I,staticRenderFns:[]},void 0,S,void 0,!1,void 0,!1,void 0,void 0,void 0);var z=new class{constructor(){this.validationRules={},this.validationMessages={},this.validationRules=v,this.validationMessages=m}install(e,t){e.prototype.$formulario=this,e.component("FormularioForm",V),e.component("FormularioGrouping",j),e.component("FormularioInput",T),this.extend(t||{})}extend(e){if("object"==typeof e)return this.validationRules=d(this.validationRules,e.validationRules||{}),this.validationMessages=d(this.validationMessages,e.validationMessages||{}),this;throw new Error(`[Formulario]: Formulario.extend() should be passed an object (was ${typeof e})`)}getRules(e={}){return d(this.validationRules,e)}getMessages(e,t){const r=d(this.validationMessages||{},t),i={};for(const t in r)i[t]=(i,...o)=>"string"==typeof r[t]?r[t]:r[t](e,i,...o);return i}};export default z; diff --git a/dist/formulario.min.js b/dist/formulario.min.js index 006c302..5095c85 100644 --- a/dist/formulario.min.js +++ b/dist/formulario.min.js @@ -32,4 +32,4 @@ function v(e,t){var r={};for(var o in e)Object.prototype.hasOwnProperty.call(e,o * vue-class-component v7.2.3 * (c) 2015-present Evan You * @license MIT - */}function y(e){return(y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function g(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function b(e){return function(e){if(Array.isArray(e)){for(var t=0,r=new Array(e.length);t0&&j("Component class must inherit Vue or its descendant class when class property is used."),n}var P=["data","beforeCreate","created","beforeMount","mounted","beforeDestroy","destroyed","beforeUpdate","updated","activated","deactivated","render","errorCaptured","serverPrefetch"];function V(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};r.name=r.name||t._componentTag||t.name;var o=t.prototype;Object.getOwnPropertyNames(o).forEach((function(e){if("constructor"!==e)if(P.indexOf(e)>-1)r[e]=o[e];else{var t=Object.getOwnPropertyDescriptor(o,e);void 0!==t.value?"function"==typeof t.value?(r.methods||(r.methods={}))[e]=t.value:(r.mixins||(r.mixins=[])).push({data:function(){return g({},e,t.value)}}):(t.get||t.set)&&((r.computed||(r.computed={}))[e]={get:t.get,set:t.set})}})),(r.mixins||(r.mixins=[])).push({data:function(){return N(this,t)}});var i=t.__decorators__;i&&(i.forEach((function(e){return e(r)})),delete t.__decorators__);var n=Object.getPrototypeOf(t.prototype),a=n instanceof e?n.constructor:e,s=a.extend(r);return R(s,t,a),O()&&E(s,t),s}var w=["cid","super","options","superOptions","extendOptions","sealedOptions","component","directive","filter"],F={prototype:!0,arguments:!0,callee:!0,caller:!0};function R(e,t,r){Object.getOwnPropertyNames(t).forEach((function(o){if(!F[o]){var i=Object.getOwnPropertyDescriptor(e,o);if(!i||i.configurable){var n,a,s=Object.getOwnPropertyDescriptor(t,o);if(!$){if("cid"===o)return;var l=Object.getOwnPropertyDescriptor(r,o);if(n=s.value,a=y(n),null!=n&&("object"===a||"function"===a)&&l&&l.value===s.value)return}"production"!==process.env.NODE_ENV&&w.indexOf(o)>=0&&j("Static property name '".concat(o,"' declared on class '").concat(t.name,"' ")+"conflicts with reserved property name of Vue internal. It may cause unexpected behavior of the component. Consider renaming the property."),Object.defineProperty(e,o,s)}}}))}function A(e){return"function"==typeof e?V(e):function(t){return V(t,e)}}A.registerHooks=function(e){P.push.apply(P,b(e))};function M(e){return _((function(t,r){void 0===t.inject&&(t.inject={}),Array.isArray(t.inject)||(t.inject[r]=e||r)}))}function D(e){return _((function(t,r){var o,i=t.provide;("function"!=typeof(o=i)||!o.managed&&!o.managedReactive)&&(i=t.provide=function(e){var t=function(){var r=this,o="function"==typeof e?e.call(this):e;for(var i in(o=Object.create(o||null)).__reactiveInject__=this.__reactiveInject__||{},t.managed)o[t.managed[i]]=this[i];var n=function(e){o[t.managedReactive[e]]=a[e],Object.defineProperty(o.__reactiveInject__,t.managedReactive[e],{enumerable:!0,get:function(){return r[e]}})},a=this;for(var i in t.managedReactive)n(i);return o};return t.managed={},t.managedReactive={},t}(i)),i.managed[r]=e||r}))}var S="undefined"!=typeof Reflect&&void 0!==Reflect.getMetadata;function C(e,t,r){if(S&&!Array.isArray(e)&&"function"!=typeof e&&void 0===e.type){var o=Reflect.getMetadata("design:type",t,r);o!==Object&&(e.type=o)}}function k(e,t){return void 0===t&&(t={}),function(r,o){C(t,r,o),_((function(r,o){(r.props||(r.props={}))[o]=t,r.model={prop:o,event:e||o}}))(r,o)}}function I(e){return void 0===e&&(e={}),function(t,r){C(e,t,r),_((function(t,r){(t.props||(t.props={}))[r]=e}))(t,r)}}function T(e,t){void 0===t&&(t={});var r=t.deep,o=void 0!==r&&r,i=t.immediate,n=void 0!==i&&i;return _((function(t,r){"object"!=typeof t.watch&&(t.watch=Object.create(null));var i=t.watch;"object"!=typeof i[e]||Array.isArray(i[e])?void 0===i[e]&&(i[e]=[]):i[e]=[i[e]],i[e].push({handler:r,deep:o,immediate:n})}))}class z{constructor(e){this.registry=new Map,this.ctx=e}add(e,t){this.registry.set(e,t)}remove(e){this.registry.delete(e);const t=this.ctx.proxy,r=e,o=(t[r],v(t,["symbol"==typeof r?r:r+""]));this.ctx.proxy=o}has(e){return this.registry.has(e)}hasNested(e){for(const t of this.registry.keys())if(t===e||t.includes(e+"."))return!0;return!1}get(e){return this.registry.get(e)}getNested(e){const t=new Map;for(const r of this.registry.keys())(r===e||r.includes(e+"."))&&t.set(r,this.registry.get(r));return t}forEach(e){this.registry.forEach((t,r)=>{e(t,r)})}keys(){return Array.from(this.registry.keys())}register(e,t){if(this.registry.has(e))return;this.registry.set(e,t);const r=o(t.$options.propsData||{},"value");!r&&this.ctx.hasInitialValue&&void 0!==f(this.ctx.initialValues,e)?t.context.model=f(this.ctx.initialValues,e):r&&!c(t.proxy,f(this.ctx.initialValues,e))&&this.ctx.setFieldValue(e,t.proxy)}reduce(e,t){return this.registry.forEach((r,o)=>{t=e(t,r,o)}),t}}class W{constructor(e=[]){this.observers=[],this.observers=e}add(e){this.observers.some(t=>t.callback===e.callback)||this.observers.push(e)}remove(e){this.observers=this.observers.filter(t=>t.callback!==e)}filter(e){return new W(this.observers.filter(e))}some(e){return this.observers.some(e)}observe(e){this.observers.forEach(t=>{"form"===t.type?t.callback(e):t.field&&!Array.isArray(e)&&o(e,t.field)&&t.callback(e[t.field])})}}let Y=class extends e{constructor(){super(...arguments),this.path="",this.proxy={},this.registry=new z(this),this.errorObserverRegistry=new W,this.localFormErrors=[],this.localFieldErrors={}}get initialValues(){return this.hasModel&&"object"==typeof this.formularioValue?Object.assign({},this.formularioValue):{}}get mergedFormErrors(){return[...this.formErrors,...this.localFormErrors]}get mergedFieldErrors(){return i(this.errors||{},this.localFieldErrors)}get hasModel(){return o(this.$options.propsData||{},"formularioValue")}get hasInitialValue(){return this.formularioValue&&"object"==typeof this.formularioValue}onFormularioValueChanged(e){this.hasModel&&e&&"object"==typeof e&&this.setValues(e)}onMergedFormErrorsChanged(e){this.errorObserverRegistry.filter(e=>"form"===e.type).observe(e)}onMergedFieldErrorsChanged(e){this.errorObserverRegistry.filter(e=>"input"===e.type).observe(e)}created(){this.initProxy()}getFormValues(){return this.proxy}onFormSubmit(){return this.hasValidationErrors().then(e=>e?void 0:function e(t){if("object"!=typeof t)return t;const r=Array.isArray(t)?[]:{};for(const i in t)o(t,i)&&(r[i]=u(t[i])?t[i]:e(t[i]));return r}(this.proxy)).then(e=>{void 0!==e?this.$emit("submit",e):this.$emit("error")})}onFormularioFieldValidation(e){this.$emit("validation",e)}addErrorObserver(e){this.errorObserverRegistry.add(e),"form"===e.type?e.callback(this.mergedFormErrors):e.field&&o(this.mergedFieldErrors,e.field)&&e.callback(this.mergedFieldErrors[e.field])}removeErrorObserver(e){this.errorObserverRegistry.remove(e)}register(e,t){this.registry.register(e,t)}deregister(e){this.registry.remove(e)}initProxy(){this.hasInitialValue&&(this.proxy=this.initialValues)}setValues(e){const t=Array.from(new Set([...Object.keys(e),...Object.keys(this.proxy)]));let r=!1;t.forEach(t=>{this.registry.hasNested(t)&&this.registry.getNested(t).forEach((t,o)=>{const i=this.registry.get(o),n=f(this.proxy,o),a=f(e,o);c(a,n)||(this.setFieldValue(o,a,!1),r=!0),c(a,i.proxy)||(i.context.model=a)})}),this.initProxy(),r&&this.$emit("input",Object.assign({},this.proxy))}setFieldValue(e,t,r=!0){if(void 0===t){const t=this.proxy,r=e,o=(t[r],v(t,["symbol"==typeof r?r:r+""]));this.proxy=o}else!function(e,t,r){const o=t.split(".");let i=e;for(let e=0;e(e.push(t.runValidation()&&t.hasValidationErrors()),e),[])).then(e=>e.some(e=>e))}resetValidation(){this.localFormErrors=[],this.localFieldErrors={},this.registry.forEach(e=>{e.resetValidation()})}};function q(e,t,r,o,i,n,a,s,l,u){"boolean"!=typeof a&&(l=s,s=a,a=!1);const c="function"==typeof r?r.options:r;let d;if(e&&e.render&&(c.render=e.render,c.staticRenderFns=e.staticRenderFns,c._compiled=!0,i&&(c.functional=!0)),o&&(c._scopeId=o),n?(d=function(e){(e=e||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext)||"undefined"==typeof __VUE_SSR_CONTEXT__||(e=__VUE_SSR_CONTEXT__),t&&t.call(this,l(e)),e&&e._registeredComponents&&e._registeredComponents.add(n)},c._ssrRegister=d):t&&(d=a?function(e){t.call(this,u(e,this.$root.$options.shadowRoot))}:function(e){t.call(this,s(e))}),d)if(c.functional){const e=c.render;c.render=function(t,r){return d.call(r),e(t,r)}}else{const e=c.beforeCreate;c.beforeCreate=e?[].concat(e,d):[d]}return r}m([k("input",{default:()=>({})})],Y.prototype,"formularioValue",void 0),m([I({default:()=>({})})],Y.prototype,"errors",void 0),m([I({default:()=>[]})],Y.prototype,"formErrors",void 0),m([D()],Y.prototype,"path",void 0),m([T("formularioValue",{deep:!0})],Y.prototype,"onFormularioValueChanged",null),m([T("mergedFormErrors")],Y.prototype,"onMergedFormErrorsChanged",null),m([T("mergedFieldErrors",{immediate:!0})],Y.prototype,"onMergedFieldErrorsChanged",null),m([D()],Y.prototype,"getFormValues",null),m([D()],Y.prototype,"onFormularioFieldValidation",null),m([D()],Y.prototype,"addErrorObserver",null),m([D()],Y.prototype,"removeErrorObserver",null),m([D("formularioRegister")],Y.prototype,"register",null),m([D("formularioDeregister")],Y.prototype,"deregister",null),m([D("formularioSetter")],Y.prototype,"setFieldValue",null),Y=m([A({name:"FormularioForm"})],Y);const Q=Y;var U=function(){var e=this,t=e.$createElement;return(e._self._c||t)("form",{on:{submit:function(t){return t.preventDefault(),e.onFormSubmit(t)}}},[e._t("default",null,{errors:e.mergedFormErrors})],2)};U._withStripped=!0;const B=q({render:U,staticRenderFns:[]},void 0,Q,void 0,!1,void 0,!1,void 0,void 0,void 0);let L=class extends e{get groupPath(){return this.isArrayItem?`${this.path}[${this.name}]`:""===this.path?this.name:`${this.path}.${this.name}`}};m([M({default:""})],L.prototype,"path",void 0),m([I({required:!0})],L.prototype,"name",void 0),m([I({default:!1})],L.prototype,"isArrayItem",void 0),m([D("path")],L.prototype,"groupPath",null),L=m([A({name:"FormularioGrouping"})],L);const Z=L;var K=function(){var e=this.$createElement;return(this._self._c||e)("div",[this._t("default")],2)};K._withStripped=!0;const G=q({render:K,staticRenderFns:[]},void 0,Z,void 0,!1,void 0,!1,void 0,void 0,void 0);function X(e,t,r,o){return i=>Promise.resolve(e(i,...r)).then(e=>e?null:{rule:t,args:r,context:i,message:o(i,...r)})}function H(e){return/^[\^]/.test(e.charAt(0))?[d(e.substr(1)),e.charAt(0)]:[d(e),null]}function J(e,t,r){return"function"==typeof e?[e,null,null]:Array.isArray(e)&&e.length?function(e,t,r){const i=e.slice(),n=i.shift();if("function"==typeof n)return[n,null,null];if("string"!=typeof n)throw new Error("[Formulario]: For array constraint first element must be rule name or Validator function");const[a,s]=H(n);if(o(t,a))return[X(t[a],a,i,r[a]||r.default),a,s];throw new Error("[Formulario] Can't create validator for constraint: "+JSON.stringify(e))}(e,t,r):"string"==typeof e?function(e,t,r){const i=e.split(":"),[n,a]=H(i.shift()||"");if(o(t,n))return[X(t[n],n,i.length?i.join(":").split(","):[],r[n]||r.default),n,a];throw new Error("[Formulario] Can't create validator for constraint: "+e)}(e,t,r):[()=>Promise.resolve(null),null,null]}function ee(e){const t=[];if(e.length){let r=e.shift();t.push(r),e.forEach(e=>{e.bail||e.bail!==r.bail?(r=Object.assign({},e),t.push(r)):r.validators.push(...e.validators)})}return t}function te(e,t){return new Promise(r=>{const o=(e,i=[])=>{if(e.length){const n=e.shift();(function(e,t){return Promise.all(e.validators.map(e=>e(t))).then(e=>e.filter(e=>null!==e))})(n,t).then(t=>0!==t.length&&n.bail||!e.length?r(i.concat(t)):o(e,i.concat(t)))}else r([])};o(function(e){const t=([e,,t])=>({validators:[e],bail:"^"===t}),r=[],o=e.findIndex(([,e])=>e&&"bail"===e.toLowerCase());return o>=0?(r.push(...ee(e.splice(0,o+1).slice(0,-1).map(t))),r.push(...e.map(([e])=>({validators:[e],bail:!0})))):r.push(...ee(e.map(t))),r}(e))})}const re={DEMAND:"demand",LIVE:"live",SUBMIT:"submit"};let oe=class extends e{constructor(){super(...arguments),this.proxy=this.getInitialValue(),this.localErrors=[],this.violations=[],this.validationRun=Promise.resolve()}get fullQualifiedName(){return""!==this.path?`${this.path}.${this.name}`:this.name}get model(){const e=this.hasModel?"value":"proxy";return void 0===this[e]?"":this[e]}set model(e){c(e,this.proxy)||(this.proxy=e),this.$emit("input",e),"function"==typeof this.formularioSetter&&this.formularioSetter(this.context.name,e)}get context(){return Object.defineProperty({name:this.fullQualifiedName,runValidation:this.runValidation.bind(this),violations:this.violations,errors:this.localErrors,allErrors:[...this.localErrors,...this.violations.map(e=>e.message)]},"model",{get:()=>this.model,set:e=>{this.model=e}})}get normalizedValidationRules(){const e={};return Object.keys(this.validationRules).forEach(t=>{e[d(t)]=this.validationRules[t]}),e}get normalizedValidationMessages(){const e={};return Object.keys(this.validationMessages).forEach(t=>{e[d(t)]=this.validationMessages[t]}),e}get hasModel(){return o(this.$options.propsData||{},"value")}onProxyChanged(e,t){this.hasModel||c(e,t)||(this.context.model=e),this.validationBehavior===re.LIVE?this.runValidation():this.violations=[]}onValueChanged(e,t){this.hasModel&&!c(e,t)&&(this.context.model=e)}created(){this.initProxy(),"function"==typeof this.formularioRegister&&this.formularioRegister(this.fullQualifiedName,this),"function"!=typeof this.addErrorObserver||this.errorsDisabled||this.addErrorObserver({callback:this.setErrors,type:"input",field:this.fullQualifiedName}),this.validationBehavior===re.LIVE&&this.runValidation()}beforeDestroy(){this.errorsDisabled||"function"!=typeof this.removeErrorObserver||this.removeErrorObserver(this.setErrors),"function"==typeof this.formularioDeregister&&this.formularioDeregister(this.fullQualifiedName)}getInitialValue(){return o(this.$options.propsData||{},"value")?this.value:""}initProxy(){c(this.context.model,this.proxy)||(this.context.model=this.proxy)}runValidation(){return this.validationRun=this.validate().then(e=>{const t=!c(e,this.violations);if(this.violations=e,t){const e={name:this.context.name,violations:this.violations};this.$emit("validation",e),"function"==typeof this.onFormularioFieldValidation&&this.onFormularioFieldValidation(e)}return this.violations}),this.validationRun}validate(){return te(function e(t,r,o){return"string"==typeof t?e(t.split("|").filter(e=>e.length),r,o):Array.isArray(t)?t.map(e=>J(e,r,o)):[]}(this.validation,this.$formulario.getRules(this.normalizedValidationRules),this.$formulario.getMessages(this,this.normalizedValidationMessages)),{value:this.context.model,name:this.context.name,formValues:this.getFormValues()})}hasValidationErrors(){return new Promise(e=>{this.$nextTick(()=>{this.validationRun.then(()=>e(this.violations.length>0))})})}setErrors(e){var t;this.localErrors=(t=e)?"string"==typeof t?[t]:Array.isArray(t)?t:"object"==typeof t?Object.values(t):[]:[]}resetValidation(){this.localErrors=[],this.violations=[]}};m([M({default:void 0})],oe.prototype,"formularioSetter",void 0),m([M({default:()=>()=>{}})],oe.prototype,"onFormularioFieldValidation",void 0),m([M({default:void 0})],oe.prototype,"formularioRegister",void 0),m([M({default:void 0})],oe.prototype,"formularioDeregister",void 0),m([M({default:()=>()=>({})})],oe.prototype,"getFormValues",void 0),m([M({default:void 0})],oe.prototype,"addErrorObserver",void 0),m([M({default:void 0})],oe.prototype,"removeErrorObserver",void 0),m([M({default:""})],oe.prototype,"path",void 0),m([k("input",{default:""})],oe.prototype,"value",void 0),m([I({required:!0,validator:e=>"string"==typeof e&&e.length>0})],oe.prototype,"name",void 0),m([I({default:""})],oe.prototype,"validation",void 0),m([I({default:()=>({})})],oe.prototype,"validationRules",void 0),m([I({default:()=>({})})],oe.prototype,"validationMessages",void 0),m([I({default:re.DEMAND,validator:e=>Object.values(re).includes(e)})],oe.prototype,"validationBehavior",void 0),m([I({default:!1})],oe.prototype,"errorsDisabled",void 0),m([T("proxy")],oe.prototype,"onProxyChanged",null),m([T("value")],oe.prototype,"onValueChanged",null),oe=m([A({name:"FormularioInput",inheritAttrs:!1})],oe);const ie=oe;var ne=function(){var e=this.$createElement;return(this._self._c||e)("div",{staticClass:"formulario-input"},[this._t("default",null,{context:this.context})],2)};ne._withStripped=!0;const ae=q({render:ne,staticRenderFns:[]},void 0,ie,void 0,!1,void 0,!1,void 0,void 0,void 0);return new class{constructor(){this.validationRules={},this.validationMessages={},this.validationRules=h,this.validationMessages=p}install(e,t){e.prototype.$formulario=this,e.component("FormularioForm",B),e.component("FormularioGrouping",G),e.component("FormularioInput",ae),this.extend(t||{})}extend(e){if("object"==typeof e)return this.validationRules=i(this.validationRules,e.validationRules||{}),this.validationMessages=i(this.validationMessages,e.validationMessages||{}),this;throw new Error(`[Formulario]: Formulario.extend() should be passed an object (was ${typeof e})`)}getRules(e={}){return i(this.validationRules,e)}getMessages(e,t){const r=i(this.validationMessages||{},t),o={};for(const t in r)o[t]=(o,...i)=>"string"==typeof r[t]?r[t]:r[t](e,o,...i);return o}}}(Vue); + */}function y(e){return(y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function g(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function b(e){return function(e){if(Array.isArray(e)){for(var t=0,r=new Array(e.length);t0&&j("Component class must inherit Vue or its descendant class when class property is used."),n}var P=["data","beforeCreate","created","beforeMount","mounted","beforeDestroy","destroyed","beforeUpdate","updated","activated","deactivated","render","errorCaptured","serverPrefetch"];function V(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};r.name=r.name||t._componentTag||t.name;var o=t.prototype;Object.getOwnPropertyNames(o).forEach((function(e){if("constructor"!==e)if(P.indexOf(e)>-1)r[e]=o[e];else{var t=Object.getOwnPropertyDescriptor(o,e);void 0!==t.value?"function"==typeof t.value?(r.methods||(r.methods={}))[e]=t.value:(r.mixins||(r.mixins=[])).push({data:function(){return g({},e,t.value)}}):(t.get||t.set)&&((r.computed||(r.computed={}))[e]={get:t.get,set:t.set})}})),(r.mixins||(r.mixins=[])).push({data:function(){return N(this,t)}});var i=t.__decorators__;i&&(i.forEach((function(e){return e(r)})),delete t.__decorators__);var n=Object.getPrototypeOf(t.prototype),a=n instanceof e?n.constructor:e,s=a.extend(r);return R(s,t,a),O()&&E(s,t),s}var w=["cid","super","options","superOptions","extendOptions","sealedOptions","component","directive","filter"],F={prototype:!0,arguments:!0,callee:!0,caller:!0};function R(e,t,r){Object.getOwnPropertyNames(t).forEach((function(o){if(!F[o]){var i=Object.getOwnPropertyDescriptor(e,o);if(!i||i.configurable){var n,a,s=Object.getOwnPropertyDescriptor(t,o);if(!$){if("cid"===o)return;var l=Object.getOwnPropertyDescriptor(r,o);if(n=s.value,a=y(n),null!=n&&("object"===a||"function"===a)&&l&&l.value===s.value)return}"production"!==process.env.NODE_ENV&&w.indexOf(o)>=0&&j("Static property name '".concat(o,"' declared on class '").concat(t.name,"' ")+"conflicts with reserved property name of Vue internal. It may cause unexpected behavior of the component. Consider renaming the property."),Object.defineProperty(e,o,s)}}}))}function A(e){return"function"==typeof e?V(e):function(t){return V(t,e)}}A.registerHooks=function(e){P.push.apply(P,b(e))};function M(e){return _((function(t,r){void 0===t.inject&&(t.inject={}),Array.isArray(t.inject)||(t.inject[r]=e||r)}))}function D(e){return _((function(t,r){var o,i=t.provide;("function"!=typeof(o=i)||!o.managed&&!o.managedReactive)&&(i=t.provide=function(e){var t=function(){var r=this,o="function"==typeof e?e.call(this):e;for(var i in(o=Object.create(o||null)).__reactiveInject__=this.__reactiveInject__||{},t.managed)o[t.managed[i]]=this[i];var n=function(e){o[t.managedReactive[e]]=a[e],Object.defineProperty(o.__reactiveInject__,t.managedReactive[e],{enumerable:!0,get:function(){return r[e]}})},a=this;for(var i in t.managedReactive)n(i);return o};return t.managed={},t.managedReactive={},t}(i)),i.managed[r]=e||r}))}var S="undefined"!=typeof Reflect&&void 0!==Reflect.getMetadata;function C(e,t,r){if(S&&!Array.isArray(e)&&"function"!=typeof e&&void 0===e.type){var o=Reflect.getMetadata("design:type",t,r);o!==Object&&(e.type=o)}}function k(e,t){return void 0===t&&(t={}),function(r,o){C(t,r,o),_((function(r,o){(r.props||(r.props={}))[o]=t,r.model={prop:o,event:e||o}}))(r,o)}}function I(e){return void 0===e&&(e={}),function(t,r){C(e,t,r),_((function(t,r){(t.props||(t.props={}))[r]=e}))(t,r)}}function T(e,t){void 0===t&&(t={});var r=t.deep,o=void 0!==r&&r,i=t.immediate,n=void 0!==i&&i;return _((function(t,r){"object"!=typeof t.watch&&(t.watch=Object.create(null));var i=t.watch;"object"!=typeof i[e]||Array.isArray(i[e])?void 0===i[e]&&(i[e]=[]):i[e]=[i[e]],i[e].push({handler:r,deep:o,immediate:n})}))}class z{constructor(e){this.registry=new Map,this.ctx=e}add(e,t){this.registry.set(e,t)}remove(e){this.registry.delete(e);const t=this.ctx.proxy,r=e,o=(t[r],v(t,["symbol"==typeof r?r:r+""]));this.ctx.proxy=o}has(e){return this.registry.has(e)}hasNested(e){for(const t of this.registry.keys())if(t===e||t.includes(e+"."))return!0;return!1}get(e){return this.registry.get(e)}getNested(e){const t=new Map;for(const r of this.registry.keys())(r===e||r.includes(e+"."))&&t.set(r,this.registry.get(r));return t}forEach(e){this.registry.forEach((t,r)=>{e(t,r)})}keys(){return Array.from(this.registry.keys())}register(e,t){if(this.registry.has(e))return;this.registry.set(e,t);const r=o(t.$options.propsData||{},"value");!r&&this.ctx.hasInitialValue&&void 0!==f(this.ctx.initialValues,e)?t.context.model=f(this.ctx.initialValues,e):r&&!c(t.proxy,f(this.ctx.initialValues,e))&&this.ctx.setFieldValue(e,t.proxy)}reduce(e,t){return this.registry.forEach((r,o)=>{t=e(t,r,o)}),t}}class W{constructor(e=[]){this.observers=[],this.observers=e}add(e){this.observers.some(t=>t.callback===e.callback)||this.observers.push(e)}remove(e){this.observers=this.observers.filter(t=>t.callback!==e)}filter(e){return new W(this.observers.filter(e))}some(e){return this.observers.some(e)}observe(e){this.observers.forEach(t=>{"form"===t.type?t.callback(e):t.field&&!Array.isArray(e)&&o(e,t.field)&&t.callback(e[t.field])})}}let Y=class extends e{constructor(){super(...arguments),this.path="",this.proxy={},this.registry=new z(this),this.errorObserverRegistry=new W,this.localFormErrors=[],this.localFieldErrors={}}get initialValues(){return this.hasModel&&"object"==typeof this.formularioValue?Object.assign({},this.formularioValue):{}}get mergedFormErrors(){return[...this.formErrors,...this.localFormErrors]}get mergedFieldErrors(){return i(this.errors||{},this.localFieldErrors)}get hasModel(){return o(this.$options.propsData||{},"formularioValue")}get hasInitialValue(){return this.formularioValue&&"object"==typeof this.formularioValue}onFormularioValueChanged(e){this.hasModel&&e&&"object"==typeof e&&this.setValues(e)}onMergedFormErrorsChanged(e){this.errorObserverRegistry.filter(e=>"form"===e.type).observe(e)}onMergedFieldErrorsChanged(e){this.errorObserverRegistry.filter(e=>"input"===e.type).observe(e)}created(){this.initProxy()}getFormValues(){return this.proxy}onFormSubmit(){return this.hasValidationErrors().then(e=>e?void 0:function e(t){if("object"!=typeof t)return t;const r=Array.isArray(t)?[]:{};for(const i in t)o(t,i)&&(r[i]=u(t[i])?t[i]:e(t[i]));return r}(this.proxy)).then(e=>{void 0!==e?this.$emit("submit",e):this.$emit("error")})}onFormularioFieldValidation(e){this.$emit("validation",e)}addErrorObserver(e){this.errorObserverRegistry.add(e),"form"===e.type?e.callback(this.mergedFormErrors):e.field&&o(this.mergedFieldErrors,e.field)&&e.callback(this.mergedFieldErrors[e.field])}removeErrorObserver(e){this.errorObserverRegistry.remove(e)}register(e,t){this.registry.register(e,t)}deregister(e){this.registry.remove(e)}initProxy(){this.hasInitialValue&&(this.proxy=this.initialValues)}setValues(e){const t=Array.from(new Set([...Object.keys(e),...Object.keys(this.proxy)]));let r=!1;t.forEach(t=>{this.registry.hasNested(t)&&this.registry.getNested(t).forEach((t,o)=>{const i=this.registry.get(o),n=f(this.proxy,o),a=f(e,o);c(a,n)||(this.setFieldValue(o,a,!1),r=!0),c(a,i.proxy)||(i.context.model=a)})}),this.initProxy(),r&&this.$emit("input",Object.assign({},this.proxy))}setFieldValue(e,t,r=!0){if(void 0===t){const t=this.proxy,r=e,o=(t[r],v(t,["symbol"==typeof r?r:r+""]));this.proxy=o}else!function(e,t,r){const o=t.split(".");let i=e;for(let e=0;e(e.push(t.runValidation()&&t.hasValidationErrors()),e),[])).then(e=>e.some(e=>e))}resetValidation(){this.localFormErrors=[],this.localFieldErrors={},this.registry.forEach(e=>{e.resetValidation()})}};function q(e,t,r,o,i,n,a,s,l,u){"boolean"!=typeof a&&(l=s,s=a,a=!1);const c="function"==typeof r?r.options:r;let d;if(e&&e.render&&(c.render=e.render,c.staticRenderFns=e.staticRenderFns,c._compiled=!0,i&&(c.functional=!0)),o&&(c._scopeId=o),n?(d=function(e){(e=e||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext)||"undefined"==typeof __VUE_SSR_CONTEXT__||(e=__VUE_SSR_CONTEXT__),t&&t.call(this,l(e)),e&&e._registeredComponents&&e._registeredComponents.add(n)},c._ssrRegister=d):t&&(d=a?function(e){t.call(this,u(e,this.$root.$options.shadowRoot))}:function(e){t.call(this,s(e))}),d)if(c.functional){const e=c.render;c.render=function(t,r){return d.call(r),e(t,r)}}else{const e=c.beforeCreate;c.beforeCreate=e?[].concat(e,d):[d]}return r}m([k("input",{default:()=>({})})],Y.prototype,"formularioValue",void 0),m([I({default:()=>({})})],Y.prototype,"errors",void 0),m([I({default:()=>[]})],Y.prototype,"formErrors",void 0),m([D()],Y.prototype,"path",void 0),m([T("formularioValue",{deep:!0})],Y.prototype,"onFormularioValueChanged",null),m([T("mergedFormErrors")],Y.prototype,"onMergedFormErrorsChanged",null),m([T("mergedFieldErrors",{deep:!0,immediate:!0})],Y.prototype,"onMergedFieldErrorsChanged",null),m([D()],Y.prototype,"getFormValues",null),m([D()],Y.prototype,"onFormularioFieldValidation",null),m([D()],Y.prototype,"addErrorObserver",null),m([D()],Y.prototype,"removeErrorObserver",null),m([D("formularioRegister")],Y.prototype,"register",null),m([D("formularioDeregister")],Y.prototype,"deregister",null),m([D("formularioSetter")],Y.prototype,"setFieldValue",null),Y=m([A({name:"FormularioForm"})],Y);const Q=Y;var U=function(){var e=this,t=e.$createElement;return(e._self._c||t)("form",{on:{submit:function(t){return t.preventDefault(),e.onFormSubmit(t)}}},[e._t("default",null,{errors:e.mergedFormErrors})],2)};U._withStripped=!0;const B=q({render:U,staticRenderFns:[]},void 0,Q,void 0,!1,void 0,!1,void 0,void 0,void 0);let L=class extends e{get groupPath(){return this.isArrayItem?`${this.path}[${this.name}]`:""===this.path?this.name:`${this.path}.${this.name}`}};m([M({default:""})],L.prototype,"path",void 0),m([I({required:!0})],L.prototype,"name",void 0),m([I({default:!1})],L.prototype,"isArrayItem",void 0),m([D("path")],L.prototype,"groupPath",null),L=m([A({name:"FormularioGrouping"})],L);const Z=L;var K=function(){var e=this.$createElement;return(this._self._c||e)("div",[this._t("default")],2)};K._withStripped=!0;const G=q({render:K,staticRenderFns:[]},void 0,Z,void 0,!1,void 0,!1,void 0,void 0,void 0);function X(e,t,r,o){return i=>Promise.resolve(e(i,...r)).then(e=>e?null:{rule:t,args:r,context:i,message:o(i,...r)})}function H(e){return/^[\^]/.test(e.charAt(0))?[d(e.substr(1)),e.charAt(0)]:[d(e),null]}function J(e,t,r){return"function"==typeof e?[e,null,null]:Array.isArray(e)&&e.length?function(e,t,r){const i=e.slice(),n=i.shift();if("function"==typeof n)return[n,null,null];if("string"!=typeof n)throw new Error("[Formulario]: For array constraint first element must be rule name or Validator function");const[a,s]=H(n);if(o(t,a))return[X(t[a],a,i,r[a]||r.default),a,s];throw new Error("[Formulario] Can't create validator for constraint: "+JSON.stringify(e))}(e,t,r):"string"==typeof e?function(e,t,r){const i=e.split(":"),[n,a]=H(i.shift()||"");if(o(t,n))return[X(t[n],n,i.length?i.join(":").split(","):[],r[n]||r.default),n,a];throw new Error("[Formulario] Can't create validator for constraint: "+e)}(e,t,r):[()=>Promise.resolve(null),null,null]}function ee(e){const t=[];if(e.length){let r=e.shift();t.push(r),e.forEach(e=>{e.bail||e.bail!==r.bail?(r=Object.assign({},e),t.push(r)):r.validators.push(...e.validators)})}return t}function te(e,t){return new Promise(r=>{const o=(e,i=[])=>{if(e.length){const n=e.shift();(function(e,t){return Promise.all(e.validators.map(e=>e(t))).then(e=>e.filter(e=>null!==e))})(n,t).then(t=>0!==t.length&&n.bail||!e.length?r(i.concat(t)):o(e,i.concat(t)))}else r([])};o(function(e){const t=([e,,t])=>({validators:[e],bail:"^"===t}),r=[],o=e.findIndex(([,e])=>e&&"bail"===e.toLowerCase());return o>=0?(r.push(...ee(e.splice(0,o+1).slice(0,-1).map(t))),r.push(...e.map(([e])=>({validators:[e],bail:!0})))):r.push(...ee(e.map(t))),r}(e))})}const re={DEMAND:"demand",LIVE:"live",SUBMIT:"submit"};let oe=class extends e{constructor(){super(...arguments),this.proxy=this.getInitialValue(),this.localErrors=[],this.violations=[],this.validationRun=Promise.resolve()}get fullQualifiedName(){return""!==this.path?`${this.path}.${this.name}`:this.name}get model(){const e=this.hasModel?"value":"proxy";return void 0===this[e]?"":this[e]}set model(e){c(e,this.proxy)||(this.proxy=e),this.$emit("input",e),"function"==typeof this.formularioSetter&&this.formularioSetter(this.context.name,e)}get context(){return Object.defineProperty({name:this.fullQualifiedName,runValidation:this.runValidation.bind(this),violations:this.violations,errors:this.localErrors,allErrors:[...this.localErrors,...this.violations.map(e=>e.message)]},"model",{get:()=>this.model,set:e=>{this.model=e}})}get normalizedValidationRules(){const e={};return Object.keys(this.validationRules).forEach(t=>{e[d(t)]=this.validationRules[t]}),e}get normalizedValidationMessages(){const e={};return Object.keys(this.validationMessages).forEach(t=>{e[d(t)]=this.validationMessages[t]}),e}get hasModel(){return o(this.$options.propsData||{},"value")}onProxyChanged(e,t){this.hasModel||c(e,t)||(this.context.model=e),this.validationBehavior===re.LIVE?this.runValidation():this.violations=[]}onValueChanged(e,t){this.hasModel&&!c(e,t)&&(this.context.model=e)}created(){this.initProxy(),"function"==typeof this.formularioRegister&&this.formularioRegister(this.fullQualifiedName,this),"function"!=typeof this.addErrorObserver||this.errorsDisabled||this.addErrorObserver({callback:this.setErrors,type:"input",field:this.fullQualifiedName}),this.validationBehavior===re.LIVE&&this.runValidation()}beforeDestroy(){this.errorsDisabled||"function"!=typeof this.removeErrorObserver||this.removeErrorObserver(this.setErrors),"function"==typeof this.formularioDeregister&&this.formularioDeregister(this.fullQualifiedName)}getInitialValue(){return o(this.$options.propsData||{},"value")?this.value:""}initProxy(){c(this.context.model,this.proxy)||(this.context.model=this.proxy)}runValidation(){return this.validationRun=this.validate().then(e=>{const t=!c(e,this.violations);if(this.violations=e,t){const e={name:this.context.name,violations:this.violations};this.$emit("validation",e),"function"==typeof this.onFormularioFieldValidation&&this.onFormularioFieldValidation(e)}return this.violations}),this.validationRun}validate(){return te(function e(t,r,o){return"string"==typeof t?e(t.split("|").filter(e=>e.length),r,o):Array.isArray(t)?t.map(e=>J(e,r,o)):[]}(this.validation,this.$formulario.getRules(this.normalizedValidationRules),this.$formulario.getMessages(this,this.normalizedValidationMessages)),{value:this.context.model,name:this.context.name,formValues:this.getFormValues()})}hasValidationErrors(){return new Promise(e=>{this.$nextTick(()=>{this.validationRun.then(()=>e(this.violations.length>0))})})}setErrors(e){var t;this.localErrors=(t=e)?"string"==typeof t?[t]:Array.isArray(t)?t:"object"==typeof t?Object.values(t):[]:[]}resetValidation(){this.localErrors=[],this.violations=[]}};m([M({default:void 0})],oe.prototype,"formularioSetter",void 0),m([M({default:()=>()=>{}})],oe.prototype,"onFormularioFieldValidation",void 0),m([M({default:void 0})],oe.prototype,"formularioRegister",void 0),m([M({default:void 0})],oe.prototype,"formularioDeregister",void 0),m([M({default:()=>()=>({})})],oe.prototype,"getFormValues",void 0),m([M({default:void 0})],oe.prototype,"addErrorObserver",void 0),m([M({default:void 0})],oe.prototype,"removeErrorObserver",void 0),m([M({default:""})],oe.prototype,"path",void 0),m([k("input",{default:""})],oe.prototype,"value",void 0),m([I({required:!0,validator:e=>"string"==typeof e&&e.length>0})],oe.prototype,"name",void 0),m([I({default:""})],oe.prototype,"validation",void 0),m([I({default:()=>({})})],oe.prototype,"validationRules",void 0),m([I({default:()=>({})})],oe.prototype,"validationMessages",void 0),m([I({default:re.DEMAND,validator:e=>Object.values(re).includes(e)})],oe.prototype,"validationBehavior",void 0),m([I({default:!1})],oe.prototype,"errorsDisabled",void 0),m([T("proxy")],oe.prototype,"onProxyChanged",null),m([T("value")],oe.prototype,"onValueChanged",null),oe=m([A({name:"FormularioInput",inheritAttrs:!1})],oe);const ie=oe;var ne=function(){var e=this.$createElement;return(this._self._c||e)("div",{staticClass:"formulario-input"},[this._t("default",null,{context:this.context})],2)};ne._withStripped=!0;const ae=q({render:ne,staticRenderFns:[]},void 0,ie,void 0,!1,void 0,!1,void 0,void 0,void 0);return new class{constructor(){this.validationRules={},this.validationMessages={},this.validationRules=h,this.validationMessages=p}install(e,t){e.prototype.$formulario=this,e.component("FormularioForm",B),e.component("FormularioGrouping",G),e.component("FormularioInput",ae),this.extend(t||{})}extend(e){if("object"==typeof e)return this.validationRules=i(this.validationRules,e.validationRules||{}),this.validationMessages=i(this.validationMessages,e.validationMessages||{}),this;throw new Error(`[Formulario]: Formulario.extend() should be passed an object (was ${typeof e})`)}getRules(e={}){return i(this.validationRules,e)}getMessages(e,t){const r=i(this.validationMessages||{},t),o={};for(const t in r)o[t]=(o,...i)=>"string"==typeof r[t]?r[t]:r[t](e,o,...i);return o}}}(Vue); diff --git a/dist/formulario.umd.js b/dist/formulario.umd.js index 9c0f45c..1472954 100644 --- a/dist/formulario.umd.js +++ b/dist/formulario.umd.js @@ -13,4 +13,4 @@ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ -function c(e,t){var r={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(r[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o=0;n--)(o=e[n])&&(a=(s<3?o(a):s>3?o(t,r,a):o(t,r))||a);return s>3&&a&&Object.defineProperty(t,r,a),a}class p{constructor(e){this.registry=new Map,this.ctx=e}add(e,t){this.registry.set(e,t)}remove(e){this.registry.delete(e);const t=this.ctx.proxy,r=e,i=(t[r],c(t,["symbol"==typeof r?r:r+""]));this.ctx.proxy=i}has(e){return this.registry.has(e)}hasNested(e){for(const t of this.registry.keys())if(t===e||t.includes(e+"."))return!0;return!1}get(e){return this.registry.get(e)}getNested(e){const t=new Map;for(const r of this.registry.keys())(r===e||r.includes(e+"."))&&t.set(r,this.registry.get(r));return t}forEach(e){this.registry.forEach((t,r)=>{e(t,r)})}keys(){return Array.from(this.registry.keys())}register(e,t){if(this.registry.has(e))return;this.registry.set(e,t);const r=o(t.$options.propsData||{},"value");!r&&this.ctx.hasInitialValue&&void 0!==u(this.ctx.initialValues,e)?t.context.model=u(this.ctx.initialValues,e):r&&!n(t.proxy,u(this.ctx.initialValues,e))&&this.ctx.setFieldValue(e,t.proxy)}reduce(e,t){return this.registry.forEach((r,i)=>{t=e(t,r,i)}),t}}class v{constructor(e=[]){this.observers=[],this.observers=e}add(e){this.observers.some(t=>t.callback===e.callback)||this.observers.push(e)}remove(e){this.observers=this.observers.filter(t=>t.callback!==e)}filter(e){return new v(this.observers.filter(e))}some(e){return this.observers.some(e)}observe(e){this.observers.forEach(t=>{"form"===t.type?t.callback(e):t.field&&!Array.isArray(e)&&o(e,t.field)&&t.callback(e[t.field])})}}let m=class extends r{constructor(){super(...arguments),this.path="",this.proxy={},this.registry=new p(this),this.errorObserverRegistry=new v,this.localFormErrors=[],this.localFieldErrors={}}get initialValues(){return this.hasModel&&"object"==typeof this.formularioValue?Object.assign({},this.formularioValue):{}}get mergedFormErrors(){return[...this.formErrors,...this.localFormErrors]}get mergedFieldErrors(){return s(this.errors||{},this.localFieldErrors)}get hasModel(){return o(this.$options.propsData||{},"formularioValue")}get hasInitialValue(){return this.formularioValue&&"object"==typeof this.formularioValue}onFormularioValueChanged(e){this.hasModel&&e&&"object"==typeof e&&this.setValues(e)}onMergedFormErrorsChanged(e){this.errorObserverRegistry.filter(e=>"form"===e.type).observe(e)}onMergedFieldErrorsChanged(e){this.errorObserverRegistry.filter(e=>"input"===e.type).observe(e)}created(){this.initProxy()}getFormValues(){return this.proxy}onFormSubmit(){return this.hasValidationErrors().then(e=>e?void 0:function e(t){if("object"!=typeof t)return t;const r=Array.isArray(t)?[]:{};for(const i in t)o(t,i)&&(r[i]=a(t[i])?t[i]:e(t[i]));return r}(this.proxy)).then(e=>{void 0!==e?this.$emit("submit",e):this.$emit("error")})}onFormularioFieldValidation(e){this.$emit("validation",e)}addErrorObserver(e){this.errorObserverRegistry.add(e),"form"===e.type?e.callback(this.mergedFormErrors):e.field&&o(this.mergedFieldErrors,e.field)&&e.callback(this.mergedFieldErrors[e.field])}removeErrorObserver(e){this.errorObserverRegistry.remove(e)}register(e,t){this.registry.register(e,t)}deregister(e){this.registry.remove(e)}initProxy(){this.hasInitialValue&&(this.proxy=this.initialValues)}setValues(e){const t=Array.from(new Set([...Object.keys(e),...Object.keys(this.proxy)]));let r=!1;t.forEach(t=>{this.registry.hasNested(t)&&this.registry.getNested(t).forEach((t,i)=>{const o=this.registry.get(i),s=u(this.proxy,i),a=u(e,i);n(a,s)||(this.setFieldValue(i,a,!1),r=!0),n(a,o.proxy)||(o.context.model=a)})}),this.initProxy(),r&&this.$emit("input",Object.assign({},this.proxy))}setFieldValue(e,t,r=!0){if(void 0===t){const t=this.proxy,r=e,i=(t[r],c(t,["symbol"==typeof r?r:r+""]));this.proxy=i}else!function(e,t,r){const i=t.split(".");let o=e;for(let e=0;e(e.push(t.runValidation()&&t.hasValidationErrors()),e),[])).then(e=>e.some(e=>e))}resetValidation(){this.localFormErrors=[],this.localFieldErrors={},this.registry.forEach(e=>{e.resetValidation()})}};function y(e,t,r,i,o,s,a,n,l,u){"boolean"!=typeof a&&(l=n,n=a,a=!1);const d="function"==typeof r?r.options:r;let h;if(e&&e.render&&(d.render=e.render,d.staticRenderFns=e.staticRenderFns,d._compiled=!0,o&&(d.functional=!0)),i&&(d._scopeId=i),s?(h=function(e){(e=e||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext)||"undefined"==typeof __VUE_SSR_CONTEXT__||(e=__VUE_SSR_CONTEXT__),t&&t.call(this,l(e)),e&&e._registeredComponents&&e._registeredComponents.add(s)},d._ssrRegister=h):t&&(h=a?function(e){t.call(this,u(e,this.$root.$options.shadowRoot))}:function(e){t.call(this,n(e))}),h)if(d.functional){const e=d.render;d.render=function(t,r){return h.call(r),e(t,r)}}else{const e=d.beforeCreate;d.beforeCreate=e?[].concat(e,h):[h]}return r}f([i.Model("input",{default:()=>({})})],m.prototype,"formularioValue",void 0),f([i.Prop({default:()=>({})})],m.prototype,"errors",void 0),f([i.Prop({default:()=>[]})],m.prototype,"formErrors",void 0),f([i.Provide()],m.prototype,"path",void 0),f([i.Watch("formularioValue",{deep:!0})],m.prototype,"onFormularioValueChanged",null),f([i.Watch("mergedFormErrors")],m.prototype,"onMergedFormErrorsChanged",null),f([i.Watch("mergedFieldErrors",{immediate:!0})],m.prototype,"onMergedFieldErrorsChanged",null),f([i.Provide()],m.prototype,"getFormValues",null),f([i.Provide()],m.prototype,"onFormularioFieldValidation",null),f([i.Provide()],m.prototype,"addErrorObserver",null),f([i.Provide()],m.prototype,"removeErrorObserver",null),f([i.Provide("formularioRegister")],m.prototype,"register",null),f([i.Provide("formularioDeregister")],m.prototype,"deregister",null),f([i.Provide("formularioSetter")],m.prototype,"setFieldValue",null),m=f([i.Component({name:"FormularioForm"})],m);const g=m;var b=function(){var e=this,t=e.$createElement;return(e._self._c||t)("form",{on:{submit:function(t){return t.preventDefault(),e.onFormSubmit(t)}}},[e._t("default",null,{errors:e.mergedFormErrors})],2)};b._withStripped=!0;const E=y({render:b,staticRenderFns:[]},void 0,g,void 0,!1,void 0,!1,void 0,void 0,void 0);let x=class extends r{get groupPath(){return this.isArrayItem?`${this.path}[${this.name}]`:""===this.path?this.name:`${this.path}.${this.name}`}};f([i.Inject({default:""})],x.prototype,"path",void 0),f([i.Prop({required:!0})],x.prototype,"name",void 0),f([i.Prop({default:!1})],x.prototype,"isArrayItem",void 0),f([i.Provide("path")],x.prototype,"groupPath",null),x=f([i.Component({name:"FormularioGrouping"})],x);const P=x;var $=function(){var e=this.$createElement;return(this._self._c||e)("div",[this._t("default")],2)};$._withStripped=!0;const N=y({render:$,staticRenderFns:[]},void 0,P,void 0,!1,void 0,!1,void 0,void 0,void 0);function F(e,t,r,i){return o=>Promise.resolve(e(o,...r)).then(e=>e?null:{rule:t,args:r,context:o,message:i(o,...r)})}function V(e){return/^[\^]/.test(e.charAt(0))?[l(e.substr(1)),e.charAt(0)]:[l(e),null]}function O(e,t,r){return"function"==typeof e?[e,null,null]:Array.isArray(e)&&e.length?function(e,t,r){const i=e.slice(),s=i.shift();if("function"==typeof s)return[s,null,null];if("string"!=typeof s)throw new Error("[Formulario]: For array constraint first element must be rule name or Validator function");const[a,n]=V(s);if(o(t,a))return[F(t[a],a,i,r[a]||r.default),a,n];throw new Error("[Formulario] Can't create validator for constraint: "+JSON.stringify(e))}(e,t,r):"string"==typeof e?function(e,t,r){const i=e.split(":"),[s,a]=V(i.shift()||"");if(o(t,s))return[F(t[s],s,i.length?i.join(":").split(","):[],r[s]||r.default),s,a];throw new Error("[Formulario] Can't create validator for constraint: "+e)}(e,t,r):[()=>Promise.resolve(null),null,null]}function j(e){const t=[];if(e.length){let r=e.shift();t.push(r),e.forEach(e=>{e.bail||e.bail!==r.bail?(r=Object.assign({},e),t.push(r)):r.validators.push(...e.validators)})}return t}function R(e,t){return new Promise(r=>{const i=(e,o=[])=>{if(e.length){const s=e.shift();(function(e,t){return Promise.all(e.validators.map(e=>e(t))).then(e=>e.filter(e=>null!==e))})(s,t).then(t=>0!==t.length&&s.bail||!e.length?r(o.concat(t)):i(e,o.concat(t)))}else r([])};i(function(e){const t=([e,,t])=>({validators:[e],bail:"^"===t}),r=[],i=e.findIndex(([,e])=>e&&"bail"===e.toLowerCase());return i>=0?(r.push(...j(e.splice(0,i+1).slice(0,-1).map(t))),r.push(...e.map(([e])=>({validators:[e],bail:!0})))):r.push(...j(e.map(t))),r}(e))})}const w={DEMAND:"demand",LIVE:"live",SUBMIT:"submit"};let A=class extends r{constructor(){super(...arguments),this.proxy=this.getInitialValue(),this.localErrors=[],this.violations=[],this.validationRun=Promise.resolve()}get fullQualifiedName(){return""!==this.path?`${this.path}.${this.name}`:this.name}get model(){const e=this.hasModel?"value":"proxy";return void 0===this[e]?"":this[e]}set model(e){n(e,this.proxy)||(this.proxy=e),this.$emit("input",e),"function"==typeof this.formularioSetter&&this.formularioSetter(this.context.name,e)}get context(){return Object.defineProperty({name:this.fullQualifiedName,runValidation:this.runValidation.bind(this),violations:this.violations,errors:this.localErrors,allErrors:[...this.localErrors,...this.violations.map(e=>e.message)]},"model",{get:()=>this.model,set:e=>{this.model=e}})}get normalizedValidationRules(){const e={};return Object.keys(this.validationRules).forEach(t=>{e[l(t)]=this.validationRules[t]}),e}get normalizedValidationMessages(){const e={};return Object.keys(this.validationMessages).forEach(t=>{e[l(t)]=this.validationMessages[t]}),e}get hasModel(){return o(this.$options.propsData||{},"value")}onProxyChanged(e,t){this.hasModel||n(e,t)||(this.context.model=e),this.validationBehavior===w.LIVE?this.runValidation():this.violations=[]}onValueChanged(e,t){this.hasModel&&!n(e,t)&&(this.context.model=e)}created(){this.initProxy(),"function"==typeof this.formularioRegister&&this.formularioRegister(this.fullQualifiedName,this),"function"!=typeof this.addErrorObserver||this.errorsDisabled||this.addErrorObserver({callback:this.setErrors,type:"input",field:this.fullQualifiedName}),this.validationBehavior===w.LIVE&&this.runValidation()}beforeDestroy(){this.errorsDisabled||"function"!=typeof this.removeErrorObserver||this.removeErrorObserver(this.setErrors),"function"==typeof this.formularioDeregister&&this.formularioDeregister(this.fullQualifiedName)}getInitialValue(){return o(this.$options.propsData||{},"value")?this.value:""}initProxy(){n(this.context.model,this.proxy)||(this.context.model=this.proxy)}runValidation(){return this.validationRun=this.validate().then(e=>{const t=!n(e,this.violations);if(this.violations=e,t){const e={name:this.context.name,violations:this.violations};this.$emit("validation",e),"function"==typeof this.onFormularioFieldValidation&&this.onFormularioFieldValidation(e)}return this.violations}),this.validationRun}validate(){return R(function e(t,r,i){return"string"==typeof t?e(t.split("|").filter(e=>e.length),r,i):Array.isArray(t)?t.map(e=>O(e,r,i)):[]}(this.validation,this.$formulario.getRules(this.normalizedValidationRules),this.$formulario.getMessages(this,this.normalizedValidationMessages)),{value:this.context.model,name:this.context.name,formValues:this.getFormValues()})}hasValidationErrors(){return new Promise(e=>{this.$nextTick(()=>{this.validationRun.then(()=>e(this.violations.length>0))})})}setErrors(e){var t;this.localErrors=(t=e)?"string"==typeof t?[t]:Array.isArray(t)?t:"object"==typeof t?Object.values(t):[]:[]}resetValidation(){this.localErrors=[],this.violations=[]}};f([i.Inject({default:void 0})],A.prototype,"formularioSetter",void 0),f([i.Inject({default:()=>()=>{}})],A.prototype,"onFormularioFieldValidation",void 0),f([i.Inject({default:void 0})],A.prototype,"formularioRegister",void 0),f([i.Inject({default:void 0})],A.prototype,"formularioDeregister",void 0),f([i.Inject({default:()=>()=>({})})],A.prototype,"getFormValues",void 0),f([i.Inject({default:void 0})],A.prototype,"addErrorObserver",void 0),f([i.Inject({default:void 0})],A.prototype,"removeErrorObserver",void 0),f([i.Inject({default:""})],A.prototype,"path",void 0),f([i.Model("input",{default:""})],A.prototype,"value",void 0),f([i.Prop({required:!0,validator:e=>"string"==typeof e&&e.length>0})],A.prototype,"name",void 0),f([i.Prop({default:""})],A.prototype,"validation",void 0),f([i.Prop({default:()=>({})})],A.prototype,"validationRules",void 0),f([i.Prop({default:()=>({})})],A.prototype,"validationMessages",void 0),f([i.Prop({default:w.DEMAND,validator:e=>Object.values(w).includes(e)})],A.prototype,"validationBehavior",void 0),f([i.Prop({default:!1})],A.prototype,"errorsDisabled",void 0),f([i.Watch("proxy")],A.prototype,"onProxyChanged",null),f([i.Watch("value")],A.prototype,"onValueChanged",null),A=f([i.Component({name:"FormularioInput",inheritAttrs:!1})],A);const M=A;var _=function(){var e=this.$createElement;return(this._self._c||e)("div",{staticClass:"formulario-input"},[this._t("default",null,{context:this.context})],2)};_._withStripped=!0;const D=y({render:_,staticRenderFns:[]},void 0,M,void 0,!1,void 0,!1,void 0,void 0,void 0);return new class{constructor(){this.validationRules={},this.validationMessages={},this.validationRules=d,this.validationMessages=h}install(e,t){e.prototype.$formulario=this,e.component("FormularioForm",E),e.component("FormularioGrouping",N),e.component("FormularioInput",D),this.extend(t||{})}extend(e){if("object"==typeof e)return this.validationRules=s(this.validationRules,e.validationRules||{}),this.validationMessages=s(this.validationMessages,e.validationMessages||{}),this;throw new Error(`[Formulario]: Formulario.extend() should be passed an object (was ${typeof e})`)}getRules(e={}){return s(this.validationRules,e)}getMessages(e,t){const r=s(this.validationMessages||{},t),i={};for(const t in r)i[t]=(i,...o)=>"string"==typeof r[t]?r[t]:r[t](e,i,...o);return i}}})); +function c(e,t){var r={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(r[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o=0;n--)(o=e[n])&&(a=(s<3?o(a):s>3?o(t,r,a):o(t,r))||a);return s>3&&a&&Object.defineProperty(t,r,a),a}class p{constructor(e){this.registry=new Map,this.ctx=e}add(e,t){this.registry.set(e,t)}remove(e){this.registry.delete(e);const t=this.ctx.proxy,r=e,i=(t[r],c(t,["symbol"==typeof r?r:r+""]));this.ctx.proxy=i}has(e){return this.registry.has(e)}hasNested(e){for(const t of this.registry.keys())if(t===e||t.includes(e+"."))return!0;return!1}get(e){return this.registry.get(e)}getNested(e){const t=new Map;for(const r of this.registry.keys())(r===e||r.includes(e+"."))&&t.set(r,this.registry.get(r));return t}forEach(e){this.registry.forEach((t,r)=>{e(t,r)})}keys(){return Array.from(this.registry.keys())}register(e,t){if(this.registry.has(e))return;this.registry.set(e,t);const r=o(t.$options.propsData||{},"value");!r&&this.ctx.hasInitialValue&&void 0!==u(this.ctx.initialValues,e)?t.context.model=u(this.ctx.initialValues,e):r&&!n(t.proxy,u(this.ctx.initialValues,e))&&this.ctx.setFieldValue(e,t.proxy)}reduce(e,t){return this.registry.forEach((r,i)=>{t=e(t,r,i)}),t}}class v{constructor(e=[]){this.observers=[],this.observers=e}add(e){this.observers.some(t=>t.callback===e.callback)||this.observers.push(e)}remove(e){this.observers=this.observers.filter(t=>t.callback!==e)}filter(e){return new v(this.observers.filter(e))}some(e){return this.observers.some(e)}observe(e){this.observers.forEach(t=>{"form"===t.type?t.callback(e):t.field&&!Array.isArray(e)&&o(e,t.field)&&t.callback(e[t.field])})}}let m=class extends r{constructor(){super(...arguments),this.path="",this.proxy={},this.registry=new p(this),this.errorObserverRegistry=new v,this.localFormErrors=[],this.localFieldErrors={}}get initialValues(){return this.hasModel&&"object"==typeof this.formularioValue?Object.assign({},this.formularioValue):{}}get mergedFormErrors(){return[...this.formErrors,...this.localFormErrors]}get mergedFieldErrors(){return s(this.errors||{},this.localFieldErrors)}get hasModel(){return o(this.$options.propsData||{},"formularioValue")}get hasInitialValue(){return this.formularioValue&&"object"==typeof this.formularioValue}onFormularioValueChanged(e){this.hasModel&&e&&"object"==typeof e&&this.setValues(e)}onMergedFormErrorsChanged(e){this.errorObserverRegistry.filter(e=>"form"===e.type).observe(e)}onMergedFieldErrorsChanged(e){this.errorObserverRegistry.filter(e=>"input"===e.type).observe(e)}created(){this.initProxy()}getFormValues(){return this.proxy}onFormSubmit(){return this.hasValidationErrors().then(e=>e?void 0:function e(t){if("object"!=typeof t)return t;const r=Array.isArray(t)?[]:{};for(const i in t)o(t,i)&&(r[i]=a(t[i])?t[i]:e(t[i]));return r}(this.proxy)).then(e=>{void 0!==e?this.$emit("submit",e):this.$emit("error")})}onFormularioFieldValidation(e){this.$emit("validation",e)}addErrorObserver(e){this.errorObserverRegistry.add(e),"form"===e.type?e.callback(this.mergedFormErrors):e.field&&o(this.mergedFieldErrors,e.field)&&e.callback(this.mergedFieldErrors[e.field])}removeErrorObserver(e){this.errorObserverRegistry.remove(e)}register(e,t){this.registry.register(e,t)}deregister(e){this.registry.remove(e)}initProxy(){this.hasInitialValue&&(this.proxy=this.initialValues)}setValues(e){const t=Array.from(new Set([...Object.keys(e),...Object.keys(this.proxy)]));let r=!1;t.forEach(t=>{this.registry.hasNested(t)&&this.registry.getNested(t).forEach((t,i)=>{const o=this.registry.get(i),s=u(this.proxy,i),a=u(e,i);n(a,s)||(this.setFieldValue(i,a,!1),r=!0),n(a,o.proxy)||(o.context.model=a)})}),this.initProxy(),r&&this.$emit("input",Object.assign({},this.proxy))}setFieldValue(e,t,r=!0){if(void 0===t){const t=this.proxy,r=e,i=(t[r],c(t,["symbol"==typeof r?r:r+""]));this.proxy=i}else!function(e,t,r){const i=t.split(".");let o=e;for(let e=0;e(e.push(t.runValidation()&&t.hasValidationErrors()),e),[])).then(e=>e.some(e=>e))}resetValidation(){this.localFormErrors=[],this.localFieldErrors={},this.registry.forEach(e=>{e.resetValidation()})}};function y(e,t,r,i,o,s,a,n,l,u){"boolean"!=typeof a&&(l=n,n=a,a=!1);const d="function"==typeof r?r.options:r;let h;if(e&&e.render&&(d.render=e.render,d.staticRenderFns=e.staticRenderFns,d._compiled=!0,o&&(d.functional=!0)),i&&(d._scopeId=i),s?(h=function(e){(e=e||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext)||"undefined"==typeof __VUE_SSR_CONTEXT__||(e=__VUE_SSR_CONTEXT__),t&&t.call(this,l(e)),e&&e._registeredComponents&&e._registeredComponents.add(s)},d._ssrRegister=h):t&&(h=a?function(e){t.call(this,u(e,this.$root.$options.shadowRoot))}:function(e){t.call(this,n(e))}),h)if(d.functional){const e=d.render;d.render=function(t,r){return h.call(r),e(t,r)}}else{const e=d.beforeCreate;d.beforeCreate=e?[].concat(e,h):[h]}return r}f([i.Model("input",{default:()=>({})})],m.prototype,"formularioValue",void 0),f([i.Prop({default:()=>({})})],m.prototype,"errors",void 0),f([i.Prop({default:()=>[]})],m.prototype,"formErrors",void 0),f([i.Provide()],m.prototype,"path",void 0),f([i.Watch("formularioValue",{deep:!0})],m.prototype,"onFormularioValueChanged",null),f([i.Watch("mergedFormErrors")],m.prototype,"onMergedFormErrorsChanged",null),f([i.Watch("mergedFieldErrors",{deep:!0,immediate:!0})],m.prototype,"onMergedFieldErrorsChanged",null),f([i.Provide()],m.prototype,"getFormValues",null),f([i.Provide()],m.prototype,"onFormularioFieldValidation",null),f([i.Provide()],m.prototype,"addErrorObserver",null),f([i.Provide()],m.prototype,"removeErrorObserver",null),f([i.Provide("formularioRegister")],m.prototype,"register",null),f([i.Provide("formularioDeregister")],m.prototype,"deregister",null),f([i.Provide("formularioSetter")],m.prototype,"setFieldValue",null),m=f([i.Component({name:"FormularioForm"})],m);const g=m;var b=function(){var e=this,t=e.$createElement;return(e._self._c||t)("form",{on:{submit:function(t){return t.preventDefault(),e.onFormSubmit(t)}}},[e._t("default",null,{errors:e.mergedFormErrors})],2)};b._withStripped=!0;const E=y({render:b,staticRenderFns:[]},void 0,g,void 0,!1,void 0,!1,void 0,void 0,void 0);let x=class extends r{get groupPath(){return this.isArrayItem?`${this.path}[${this.name}]`:""===this.path?this.name:`${this.path}.${this.name}`}};f([i.Inject({default:""})],x.prototype,"path",void 0),f([i.Prop({required:!0})],x.prototype,"name",void 0),f([i.Prop({default:!1})],x.prototype,"isArrayItem",void 0),f([i.Provide("path")],x.prototype,"groupPath",null),x=f([i.Component({name:"FormularioGrouping"})],x);const P=x;var $=function(){var e=this.$createElement;return(this._self._c||e)("div",[this._t("default")],2)};$._withStripped=!0;const N=y({render:$,staticRenderFns:[]},void 0,P,void 0,!1,void 0,!1,void 0,void 0,void 0);function F(e,t,r,i){return o=>Promise.resolve(e(o,...r)).then(e=>e?null:{rule:t,args:r,context:o,message:i(o,...r)})}function V(e){return/^[\^]/.test(e.charAt(0))?[l(e.substr(1)),e.charAt(0)]:[l(e),null]}function O(e,t,r){return"function"==typeof e?[e,null,null]:Array.isArray(e)&&e.length?function(e,t,r){const i=e.slice(),s=i.shift();if("function"==typeof s)return[s,null,null];if("string"!=typeof s)throw new Error("[Formulario]: For array constraint first element must be rule name or Validator function");const[a,n]=V(s);if(o(t,a))return[F(t[a],a,i,r[a]||r.default),a,n];throw new Error("[Formulario] Can't create validator for constraint: "+JSON.stringify(e))}(e,t,r):"string"==typeof e?function(e,t,r){const i=e.split(":"),[s,a]=V(i.shift()||"");if(o(t,s))return[F(t[s],s,i.length?i.join(":").split(","):[],r[s]||r.default),s,a];throw new Error("[Formulario] Can't create validator for constraint: "+e)}(e,t,r):[()=>Promise.resolve(null),null,null]}function j(e){const t=[];if(e.length){let r=e.shift();t.push(r),e.forEach(e=>{e.bail||e.bail!==r.bail?(r=Object.assign({},e),t.push(r)):r.validators.push(...e.validators)})}return t}function R(e,t){return new Promise(r=>{const i=(e,o=[])=>{if(e.length){const s=e.shift();(function(e,t){return Promise.all(e.validators.map(e=>e(t))).then(e=>e.filter(e=>null!==e))})(s,t).then(t=>0!==t.length&&s.bail||!e.length?r(o.concat(t)):i(e,o.concat(t)))}else r([])};i(function(e){const t=([e,,t])=>({validators:[e],bail:"^"===t}),r=[],i=e.findIndex(([,e])=>e&&"bail"===e.toLowerCase());return i>=0?(r.push(...j(e.splice(0,i+1).slice(0,-1).map(t))),r.push(...e.map(([e])=>({validators:[e],bail:!0})))):r.push(...j(e.map(t))),r}(e))})}const w={DEMAND:"demand",LIVE:"live",SUBMIT:"submit"};let A=class extends r{constructor(){super(...arguments),this.proxy=this.getInitialValue(),this.localErrors=[],this.violations=[],this.validationRun=Promise.resolve()}get fullQualifiedName(){return""!==this.path?`${this.path}.${this.name}`:this.name}get model(){const e=this.hasModel?"value":"proxy";return void 0===this[e]?"":this[e]}set model(e){n(e,this.proxy)||(this.proxy=e),this.$emit("input",e),"function"==typeof this.formularioSetter&&this.formularioSetter(this.context.name,e)}get context(){return Object.defineProperty({name:this.fullQualifiedName,runValidation:this.runValidation.bind(this),violations:this.violations,errors:this.localErrors,allErrors:[...this.localErrors,...this.violations.map(e=>e.message)]},"model",{get:()=>this.model,set:e=>{this.model=e}})}get normalizedValidationRules(){const e={};return Object.keys(this.validationRules).forEach(t=>{e[l(t)]=this.validationRules[t]}),e}get normalizedValidationMessages(){const e={};return Object.keys(this.validationMessages).forEach(t=>{e[l(t)]=this.validationMessages[t]}),e}get hasModel(){return o(this.$options.propsData||{},"value")}onProxyChanged(e,t){this.hasModel||n(e,t)||(this.context.model=e),this.validationBehavior===w.LIVE?this.runValidation():this.violations=[]}onValueChanged(e,t){this.hasModel&&!n(e,t)&&(this.context.model=e)}created(){this.initProxy(),"function"==typeof this.formularioRegister&&this.formularioRegister(this.fullQualifiedName,this),"function"!=typeof this.addErrorObserver||this.errorsDisabled||this.addErrorObserver({callback:this.setErrors,type:"input",field:this.fullQualifiedName}),this.validationBehavior===w.LIVE&&this.runValidation()}beforeDestroy(){this.errorsDisabled||"function"!=typeof this.removeErrorObserver||this.removeErrorObserver(this.setErrors),"function"==typeof this.formularioDeregister&&this.formularioDeregister(this.fullQualifiedName)}getInitialValue(){return o(this.$options.propsData||{},"value")?this.value:""}initProxy(){n(this.context.model,this.proxy)||(this.context.model=this.proxy)}runValidation(){return this.validationRun=this.validate().then(e=>{const t=!n(e,this.violations);if(this.violations=e,t){const e={name:this.context.name,violations:this.violations};this.$emit("validation",e),"function"==typeof this.onFormularioFieldValidation&&this.onFormularioFieldValidation(e)}return this.violations}),this.validationRun}validate(){return R(function e(t,r,i){return"string"==typeof t?e(t.split("|").filter(e=>e.length),r,i):Array.isArray(t)?t.map(e=>O(e,r,i)):[]}(this.validation,this.$formulario.getRules(this.normalizedValidationRules),this.$formulario.getMessages(this,this.normalizedValidationMessages)),{value:this.context.model,name:this.context.name,formValues:this.getFormValues()})}hasValidationErrors(){return new Promise(e=>{this.$nextTick(()=>{this.validationRun.then(()=>e(this.violations.length>0))})})}setErrors(e){var t;this.localErrors=(t=e)?"string"==typeof t?[t]:Array.isArray(t)?t:"object"==typeof t?Object.values(t):[]:[]}resetValidation(){this.localErrors=[],this.violations=[]}};f([i.Inject({default:void 0})],A.prototype,"formularioSetter",void 0),f([i.Inject({default:()=>()=>{}})],A.prototype,"onFormularioFieldValidation",void 0),f([i.Inject({default:void 0})],A.prototype,"formularioRegister",void 0),f([i.Inject({default:void 0})],A.prototype,"formularioDeregister",void 0),f([i.Inject({default:()=>()=>({})})],A.prototype,"getFormValues",void 0),f([i.Inject({default:void 0})],A.prototype,"addErrorObserver",void 0),f([i.Inject({default:void 0})],A.prototype,"removeErrorObserver",void 0),f([i.Inject({default:""})],A.prototype,"path",void 0),f([i.Model("input",{default:""})],A.prototype,"value",void 0),f([i.Prop({required:!0,validator:e=>"string"==typeof e&&e.length>0})],A.prototype,"name",void 0),f([i.Prop({default:""})],A.prototype,"validation",void 0),f([i.Prop({default:()=>({})})],A.prototype,"validationRules",void 0),f([i.Prop({default:()=>({})})],A.prototype,"validationMessages",void 0),f([i.Prop({default:w.DEMAND,validator:e=>Object.values(w).includes(e)})],A.prototype,"validationBehavior",void 0),f([i.Prop({default:!1})],A.prototype,"errorsDisabled",void 0),f([i.Watch("proxy")],A.prototype,"onProxyChanged",null),f([i.Watch("value")],A.prototype,"onValueChanged",null),A=f([i.Component({name:"FormularioInput",inheritAttrs:!1})],A);const M=A;var _=function(){var e=this.$createElement;return(this._self._c||e)("div",{staticClass:"formulario-input"},[this._t("default",null,{context:this.context})],2)};_._withStripped=!0;const D=y({render:_,staticRenderFns:[]},void 0,M,void 0,!1,void 0,!1,void 0,void 0,void 0);return new class{constructor(){this.validationRules={},this.validationMessages={},this.validationRules=d,this.validationMessages=h}install(e,t){e.prototype.$formulario=this,e.component("FormularioForm",E),e.component("FormularioGrouping",N),e.component("FormularioInput",D),this.extend(t||{})}extend(e){if("object"==typeof e)return this.validationRules=s(this.validationRules,e.validationRules||{}),this.validationMessages=s(this.validationMessages,e.validationMessages||{}),this;throw new Error(`[Formulario]: Formulario.extend() should be passed an object (was ${typeof e})`)}getRules(e={}){return s(this.validationRules,e)}getMessages(e,t){const r=s(this.validationMessages||{},t),i={};for(const t in r)i[t]=(i,...o)=>"string"==typeof r[t]?r[t]:r[t](e,i,...o);return i}}})); diff --git a/package.json b/package.json index 28495ee..7651641 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@retailcrm/vue-formulario", - "version": "0.3.0", + "version": "0.3.1", "author": "RetailDriverLLC ", "scripts": { "build": "npm run build:esm & npm run build:umd & npm run build:iife & wait && echo \"Build complete:\nesm: $(gzip -c dist/formulario.esm.js | wc -c)b gzip\numd: $(gzip -c dist/formulario.umd.js | wc -c)b gzip\nmin: $(gzip -c dist/formulario.min.js | wc -c)b gzip\"",