UNPKG

vuelidate

Version:

Simple, lightweight model-based validation for Vue.js

1 lines 10.5 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.vuelidate=e():t.vuelidate=e()}(window,function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:n})},r.r=function(t){Object.defineProperty(t,"__esModule",{value:!0})},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="/",r(r.s=28)}({1:function(t,e,r){"use strict";function n(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}Object.defineProperty(e,"__esModule",{value:!0}),e.pushParams=s,e.popParams=l,e.withParams=function(t,e){if("object"==typeof t&&void 0!==e)return r=t,n=e,u(t=>(function(...e){return t(r),n.apply(this,e)}));var r,n;return u(t)},e._setTarget=e.target=void 0;const o=[];let i=null;e.target=i;function s(){null!==i&&o.push(i),e.target=i={}}function l(){const t=i,r=e.target=i=o.pop()||null;return r&&(Array.isArray(r.$sub)||(r.$sub=[]),r.$sub.push(t)),t}function a(t){if("object"!=typeof t||Array.isArray(t))throw new Error("params must be an object");e.target=i=function(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{},o=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(o=o.concat(Object.getOwnPropertySymbols(r).filter(function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable}))),o.forEach(function(e){n(t,e,r[e])})}return t}({},i,t)}function u(t){const e=t(a);return function(...t){s();try{return e.apply(this,t)}finally{l()}}}e._setTarget=(t=>{e.target=i=t})},27:function(t,e,r){"use strict";function n(t){return null===t||void 0===t}function o(t){return null!==t&&void 0!==t}function i(t,e){return e.tag===t.tag&&e.key===t.key}function s(t){const e=t.tag;t.vm=new e({data:t.args})}function l(t,e,r){let n,i;const s={};for(n=e;n<=r;++n)o(i=t[n].key)&&(s[i]=n);return s}function a(t,e,r){for(;e<=r;++e)s(t[e])}function u(t,e,r){for(;e<=r;++e){const r=t[e];o(r)&&(r.vm.$destroy(),r.vm=null)}}function c(t,e){t!==e&&(e.vm=t.vm,function(t){const e=Object.keys(t.args);for(let r=0;r<e.length;r++)e.forEach(e=>{t.vm[e]=t.args[e]})}(e))}Object.defineProperty(e,"__esModule",{value:!0}),e.patchChildren=function(t,e){o(t)&&o(e)?t!==e&&function(t,e){let r,d,h,f=0,y=0,p=t.length-1,v=t[0],m=t[p],g=e.length-1,b=e[0],M=e[g];for(;f<=p&&y<=g;)n(v)?v=t[++f]:n(m)?m=t[--p]:i(v,b)?(c(v,b),v=t[++f],b=e[++y]):i(m,M)?(c(m,M),m=t[--p],M=e[--g]):i(v,M)?(c(v,M),v=t[++f],M=e[--g]):i(m,b)?(c(m,b),m=t[--p],b=e[++y]):(n(r)&&(r=l(t,f,p)),n(d=o(b.key)?r[b.key]:null)?(s(b),b=e[++y]):i(h=t[d],b)?(c(h,b),t[d]=void 0,b=e[++y]):(s(b),b=e[++y]));f>p?a(e,y,g):y>g&&u(t,f,p)}(t,e):o(e)?a(e,0,e.length-1):o(t)&&u(t,0,t.length-1)},e.h=function(t,e,r){return{tag:t,key:e,args:r}}},28:function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.Vuelidate=P,Object.defineProperty(e,"withParams",{enumerable:!0,get:function(){return o.withParams}}),e.default=e.validationMixin=void 0;var n=r(27),o=r(1);function i(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{},n=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(r).filter(function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable}))),n.forEach(function(e){s(t,e,r[e])})}return t}function s(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}const l=()=>null,a=(t,e,r)=>t.reduce((t,n)=>(t[r?r(n):n]=e(n),t),{});function u(t){return"function"==typeof t}function c(t){return null!==t&&("object"==typeof t||u(t))}const d=(t,e,r,n)=>{if("function"==typeof r)return r.call(t,e,n);r=Array.isArray(r)?r:r.split(".");for(let t=0;t<r.length;t++){if(!e||"object"!=typeof e)return n;e=e[r[t]]}return void 0===e?n:e},h="__isVuelidateAsyncVm";const f={$invalid(){const t=this.proxy;return this.nestedKeys.some(t=>this.refProxy(t).$invalid)||this.ruleKeys.some(e=>!t[e])},$dirty(){return!!this.dirty||0!==this.nestedKeys.length&&this.nestedKeys.every(t=>this.refProxy(t).$dirty)},$anyDirty(){return!!this.dirty||0!==this.nestedKeys.length&&this.nestedKeys.some(t=>this.refProxy(t).$anyDirty)},$error(){return this.$dirty&&!this.$pending&&this.$invalid},$anyError(){return this.$anyDirty&&!this.$pending&&this.$invalid},$pending(){return this.ruleKeys.some(t=>this.getRef(t).$pending)||this.nestedKeys.some(t=>this.refProxy(t).$pending)},$params(){const t=this.validations;return i({},a(this.nestedKeys,e=>t[e]&&t[e].$params||null),a(this.ruleKeys,t=>this.getRef(t).$params))}};function y(t){this.dirty=t;const e=this.proxy,r=t?"$touch":"$reset";this.nestedKeys.forEach(t=>{e[t][r]()})}const p={$touch(){y.call(this,!0)},$reset(){y.call(this,!1)},$flattenParams(){const t=this.proxy;let e=[];for(const r in this.$params)if(this.isNested(r)){const n=t[r].$flattenParams();for(let t=0;t<n.length;t++)n[t].path.unshift(r);e=e.concat(n)}else e.push({path:[],name:r,params:this.$params[r]});return e}},v=Object.keys(f),m=Object.keys(p);let g=null;const b=t=>{if(g)return g;const e=t.extend({computed:{refs(){const t=this._vval;this._vval=this.children,(0,n.patchChildren)(t,this._vval);const e={};return this._vval.forEach(t=>{e[t.key]=t.vm}),e}},beforeCreate(){this._vval=null},beforeDestroy(){this._vval&&((0,n.patchChildren)(this._vval),this._vval=null)},methods:{getModel(){return this.lazyModel?this.lazyModel(this.prop):this.model},getModelKey(t){var e=this.getModel();if(e)return e[t]},hasIter:()=>!1}}),r=e.extend({data:()=>({rule:null,lazyModel:null,model:null,lazyParentModel:null,rootModel:null}),methods:{runRule(e){const r=this.getModel();(0,o.pushParams)();const n=this.rule.call(this.rootModel,r,e),i=c(s=n)&&u(s.then)?function(t,e){const r=new t({data:{p:!0,v:!1}});return e.then(t=>{r.p=!1,r.v=t},t=>{throw r.p=!1,r.v=!1,t}),r[h]=!0,r}(t,n):n;var s;const l=(0,o.popParams)();return{output:i,params:l&&l.$sub?l.$sub.length>1?l:l.$sub[0]:null}}},computed:{run(){const t=this.lazyParentModel();if(Array.isArray(t)&&t.__ob__){const e=t.__ob__.dep;e.depend();const r=e.constructor.target;if(!this._indirectWatcher){const e=r.constructor;this._indirectWatcher=new e(this,()=>this.runRule(t),null,{lazy:!0})}const n=this.getModel();if(!this._indirectWatcher.dirty&&this._lastModel===n)return this._indirectWatcher.depend(),r.value;this._lastModel=n,this._indirectWatcher.evaluate(),this._indirectWatcher.depend()}else this._indirectWatcher&&(this._indirectWatcher.teardown(),this._indirectWatcher=null);return this._indirectWatcher?this._indirectWatcher.value:this.runRule(t)},$params(){return this.run.params},proxy(){const t=this.run.output;return t[h]?!!t.v:!!t},$pending(){const t=this.run.output;return!!t[h]&&t.p}},destroyed(){this._indirectWatcher&&(this._indirectWatcher.teardown(),this._indirectWatcher=null)}}),s=e.extend({data:()=>({dirty:!1,validations:null,lazyModel:null,model:null,prop:null,lazyParentModel:null,rootModel:null}),methods:i({},p,{refProxy(t){return this.getRef(t).proxy},getRef(t){return this.refs[t]},isNested(t){return"function"!=typeof this.validations[t]}}),computed:i({},f,{nestedKeys(){return this.keys.filter(this.isNested)},ruleKeys(){return this.keys.filter(t=>!this.isNested(t))},keys(){return Object.keys(this.validations).filter(t=>"$params"!==t)},proxy(){const t=a(this.keys,t=>({enumerable:!0,configurable:!0,get:()=>this.refProxy(t)})),e=a(v,t=>({enumerable:!0,configurable:!0,get:()=>this[t]})),r=a(m,t=>({enumerable:!1,configurable:!0,get:()=>this[t]})),n=this.hasIter()?{$iter:{enumerable:!0,value:Object.defineProperties({},i({},t))}}:{};return Object.defineProperties({},i({},t,n,{$model:{enumerable:!0,get:()=>{const t=this.lazyParentModel();return null!=t?t[this.prop]:null},set:t=>{const e=this.lazyParentModel();null!=e&&(e[this.prop]=t,this.$touch())}}},e,r))},children(){return[...this.nestedKeys.map(t=>M(this,t)),...this.ruleKeys.map(t=>$(this,t))].filter(Boolean)}})}),y=s.extend({methods:{isNested(t){return void 0!==this.validations[t]()},getRef(t){const e=this;return{get proxy(){return e.validations[t]()||!1}}}}}),b=s.extend({computed:{keys(){var t=this.getModel();return c(t)?Object.keys(t):[]},tracker(){const t=this.validations.$trackBy;return t?e=>`${d(this.rootModel,this.getModelKey(e),t)}`:t=>`${t}`},getModelLazy(){return()=>this.getModel()},children(){const t=this.validations,e=this.getModel(),r=i({},t);delete r.$trackBy;let o={};return this.keys.map(t=>{const i=this.tracker(t);return o.hasOwnProperty(i)?null:(o[i]=!0,(0,n.h)(s,i,{validations:r,prop:t,lazyParentModel:this.getModelLazy,model:e[t],rootModel:this.rootModel}))}).filter(Boolean)}},methods:{isNested:()=>!0,getRef(t){return this.refs[this.tracker(t)]},hasIter:()=>!0}}),M=(t,e)=>{if("$each"===e)return(0,n.h)(b,e,{validations:t.validations[e],lazyParentModel:t.lazyParentModel,prop:e,lazyModel:t.getModel,rootModel:t.rootModel});const r=t.validations[e];if(Array.isArray(r)){const o=t.rootModel,i=a(r,t=>(function(){return d(o,o.$v,t)}),t=>Array.isArray(t)?t.join("."):t);return(0,n.h)(y,e,{validations:i,lazyParentModel:l,prop:e,lazyModel:l,rootModel:o})}return(0,n.h)(s,e,{validations:r,lazyParentModel:t.getModel,prop:e,lazyModel:t.getModelKey,rootModel:t.rootModel})},$=(t,e)=>(0,n.h)(r,e,{rule:t.validations[e],lazyParentModel:t.lazyParentModel,lazyModel:t.getModel,rootModel:t.rootModel});return g={VBase:e,Validation:s}};let M=null;const $=(t,e)=>{const r=function(t){if(M)return M;let e=t.constructor;for(;e.super;)e=e.super;return M=e,e}(t),o=b(r),i=o.Validation;return new(0,o.VBase)({computed:{children(){const r="function"==typeof e?e.call(t):e;return[(0,n.h)(i,"$v",{validations:r,lazyParentModel:l,prop:"$v",model:t,rootModel:t})]}}})},_={data(){const t=this.$options.validations;return t&&(this._vuelidate=$(this,t)),{}},beforeCreate(){const t=this.$options;t.validations&&(t.computed||(t.computed={}),t.computed.$v||(t.computed.$v=function(){return this._vuelidate?this._vuelidate.refs.$v.proxy:null}))},beforeDestroy(){this._vuelidate&&(this._vuelidate.$destroy(),this._vuelidate=null)}};function P(t){t.mixin(_)}e.validationMixin=_;var j=P;e.default=j}})});