UNPKG

vue-types

Version:
9 lines (8 loc) 9.28 kB
(function(u,b){typeof exports=="object"&&typeof module!="undefined"?b(exports):typeof define=="function"&&define.amd?define(["exports"],b):(u=typeof globalThis!="undefined"?globalThis:u||self,b(u.VueTypes={}))})(void 0,(function(u){"use strict";const b={silent:!1,logLevel:"warn"};function p(e){if(typeof e!="object"||e===null)return!1;const t=Object.getPrototypeOf(e);return(t===null||t===Object.prototype||Object.getPrototypeOf(t)===null)&&!(Symbol.toStringTag in e)&&!(Symbol.iterator in e)}var P=Object.getOwnPropertySymbols,X=Object.prototype.hasOwnProperty,Z=Object.prototype.propertyIsEnumerable,x=(e,t)=>{var r={};for(var n in e)X.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(e!=null&&P)for(var n of P(e))t.indexOf(n)<0&&Z.call(e,n)&&(r[n]=e[n]);return r};const V=/^\s*function (\w+)/;function E(e){var t;const r=(t=e==null?void 0:e.type)!=null?t:e;if(r){const n=r.toString().match(V);return n?n[1]:""}return""}function ee(e){if(e==null)return"";const t=e.constructor.toString().match(V);return t?t[1].replace(/^Async/,""):""}function q(e){return"structuredClone"in globalThis?structuredClone(e):g(e)?[...e]:p(e)?Object.assign({},e):e}function A(){}let s=A;process.env.NODE_ENV!=="production"&&(s=typeof console!="undefined"?(e,t=b.logLevel)=>{b.silent||console[t](`[VueTypes warn]: ${e}`)}:A);const te=Number.isInteger||function(e){return typeof e=="number"&&isFinite(e)&&Math.floor(e)===e},g=Array.isArray,m=Object.hasOwn,O=e=>toString.call(e)==="[object Function]",_=(e,t)=>p(e)&&m(e,"_vueTypes_name")&&(!t||e._vueTypes_name===t),N=e=>p(e)&&(m(e,"type")||["_vueTypes_name","validator","default","required"].some(t=>m(e,t)));function $(e,t){return Object.defineProperty(e.bind(t),"__original",{value:e})}function ne(e){var t;return(t=e.__original)!=null?t:e}function h(e,t,r=!1){let n,i=!0,o="";p(e)?n=e:n={type:e};const l=_(n)?n._vueTypes_name+" - ":"";if(N(n)&&n.type!==null){if(n.type===void 0||n.type===!0||!n.required&&t==null)return i;g(n.type)?(i=n.type.some(a=>h(a,t,!0)===!0),o=n.type.map(a=>E(a)).join(" or ")):(o=E(n),o==="Array"?i=g(t):o==="Object"?i=p(t):o==="String"||o==="Number"||o==="Boolean"||o==="Function"?i=ee(t)===o:i=t instanceof n.type)}if(!i){const a=`${l}value "${t}" should be of type "${o}"`;return r?a:(s(a),!1)}if(m(n,"validator")&&O(n.validator)){const a=s,y=[];if(s=f=>{y.push(f)},i=n.validator(t),s=a,!i){const f=(y.length>1?"* ":"")+y.join(` * `);return y.length=0,r?f:(s(f),i)}}return i}function c(e,t){const r=Object.defineProperties(t,{_vueTypes_name:{value:e,writable:!0},isRequired:{get(){return this.required=!0,this}},def:{value(i){return i===void 0?this.type===Boolean||g(this.type)&&this.type.includes(Boolean)?(this.default=void 0,this):(m(this,"default")&&delete this.default,this):!O(i)&&h(this,i,!0)!==!0?(s(`${this._vueTypes_name} - invalid default value: "${i}"`),this):(g(i)?this.default=()=>q(i):p(i)?this.default=()=>q(i):this.default=i,this)}}}),{validator:n}=r;return O(n)&&(r.validator=$(n,r)),r}function v(e,t){const r=c(e,t);return Object.defineProperty(r,"validate",{value(n){return O(this.validator)&&s(`${this._vueTypes_name} - calling .validate() will overwrite the current custom validator function. Validator info: ${JSON.stringify(this)}`),this.validator=$(n,this),this}})}function re(e){return Object.create(Object.getPrototypeOf(e),Object.getOwnPropertyDescriptors(e))}function ie(e,t,r){const n=re(t);if(n._vueTypes_name=e,!p(r))return n;const i=r,{validator:o}=i,l=x(i,["validator"]);if(O(o)){let{validator:a}=n;a&&(a=ne(a)),n.validator=$(a?function(y,f){return a.call(this,y,f)&&o.call(this,y,f)}:o,n)}return Object.assign(n,l)}function j(e){return e.replace(/^(?!\s*$)/gm," ")}const oe=()=>({func:()=>{},bool:!0,string:"",number:0,array:()=>[],object:()=>({}),integer:0}),D=()=>v("any",{}),L=()=>v("function",{type:Function}),w=()=>v("boolean",{type:Boolean}),B=()=>v("string",{type:String}),F=()=>v("number",{type:Number}),k=()=>v("array",{type:Array}),C=()=>v("object",{type:Object}),I=()=>c("integer",{type:Number,validator(e){const t=te(e);return t||s(`integer - "${e==null?void 0:e.toString()}" is not an integer`),t}}),Y=()=>c("symbol",{validator(e){const t=typeof e=="symbol";return t||s(`symbol - invalid value "${e==null?void 0:e.toString()}"`),t}}),M=()=>Object.defineProperty({type:null,validator(e){const t=e===null;return t||s("nullable - value should be null"),t}},"_vueTypes_name",{value:"nullable"});function z(e,t="custom validation failed"){if(typeof e!="function")throw new TypeError("[VueTypes error]: You must provide a function as argument");return c(e.name||"<<anonymous function>>",{type:null,validator(r){const n=e(r);return n||s(`${this._vueTypes_name} - ${t}`),n}})}function J(e){if(!g(e))throw new TypeError("[VueTypes error]: You must provide an array as argument.");const t=`oneOf - value should be one of "${e.map(n=>typeof n=="symbol"?n.toString():n).join('", "')}".`,r={validator(n){const i=e.indexOf(n)!==-1;return i||s(t),i}};if(e.indexOf(null)===-1){const n=e.reduce((i,o)=>{if(o!=null){const l=o.constructor;i.indexOf(l)===-1&&i.push(l)}return i},[]);n.length>0&&(r.type=n)}return c("oneOf",r)}function R(e){if(!g(e))throw new TypeError("[VueTypes error]: You must provide an array as argument");let t=!1,r=!1,n=[];for(let o=0;o<e.length;o+=1){const l=e[o];if(N(l)){if(O(l.validator)&&(t=!0),_(l,"oneOf")&&l.type){n=n.concat(l.type);continue}if(_(l,"nullable")){r=!0;continue}if(l.type===!0||!l.type){s('oneOfType - invalid usage of "true" and "null" as types.');continue}n=n.concat(l.type)}else n.push(l)}n=n.filter((o,l)=>n.indexOf(o)===l);const i=!r&&n.length>0?n:null;return t?c("oneOfType",{type:i,validator(o){const l=[],a=e.some(y=>{const f=h(y,o,!0);return typeof f=="string"&&l.push(f),f===!0});return a||s(`oneOfType - provided value does not match any of the ${l.length} passed-in validators: ${j(l.join(` `))}`),a}}):c("oneOfType",{type:i})}function U(e){return c("arrayOf",{type:Array,validator(t){let r="";const n=t.every(i=>(r=h(e,i,!0),r===!0));return n||s(`arrayOf - value validation error: ${j(r)}`),n}})}function G(e){return c("instanceOf",{type:e})}function H(e){return c("objectOf",{type:Object,validator(t){let r="";if(!p(t))return!1;const n=Object.keys(t).every(i=>(r=h(e,t[i],!0),r===!0));return n||s(`objectOf - value validation error: ${j(r)}`),n}})}function K(e){const t=Object.keys(e),r=t.filter(i=>{var o;return!!((o=e[i])!=null&&o.required)}),n=c("shape",{type:Object,validator(i){var o;if(!p(i))return!1;const l=Object.keys(i);if(r.some(a=>l.indexOf(a)===-1)){const a=r.filter(y=>l.indexOf(y)===-1);return a.length===1?s(`shape - required property "${(o=a[0])==null?void 0:o.toString()}" is not defined.`):s(`shape - required properties "${a.join('", "')}" are not defined.`),!1}return l.every(a=>{if(t.indexOf(a)===-1)return this._vueTypes_isLoose?!0:(s(`shape - shape definition does not include a "${a.toString()}" property. Allowed keys: "${t.join('", "')}".`),!1);const y=e[a],f=h(y,i[a],!0);return typeof f=="string"&&s(`shape - "${a==null?void 0:a.toString()}" property validation error: ${j(f)}`),f===!0})}});return Object.defineProperty(n,"_vueTypes_isLoose",{writable:!0,value:!1}),Object.defineProperty(n,"loose",{get(){return this._vueTypes_isLoose=!0,this}}),n}var ae=Object.defineProperty,Q=Object.getOwnPropertySymbols,ue=Object.prototype.hasOwnProperty,le=Object.prototype.propertyIsEnumerable,S=(e,t,r)=>t in e?ae(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,T=(e,t)=>{for(var r in t||(t={}))ue.call(t,r)&&S(e,r,t[r]);if(Q)for(var r of Q(t))le.call(t,r)&&S(e,r,t[r]);return e},d=(e,t,r)=>S(e,typeof t!="symbol"?t+"":t,r);function se(e,t,r=!1){return r?v(e,t):c(e,t)}const fe=(()=>{var e;return e=class{static get any(){return D()}static get func(){return L().def(this.defaults.func)}static get bool(){return this.defaults.bool===void 0?w():w().def(this.defaults.bool)}static get string(){return B().def(this.defaults.string)}static get number(){return F().def(this.defaults.number)}static get array(){return k().def(this.defaults.array)}static get object(){return C().def(this.defaults.object)}static get integer(){return I().def(this.defaults.integer)}static get symbol(){return Y()}static get nullable(){return M()}static extend(...t){s("VueTypes.extend has been removed. Use the ES6+ method instead. See https://vue-types.codeful.dev/namespaced-usage/extend.html#extending-namespaced-validators-in-es6 for details.")}},d(e,"defaults",{}),d(e,"sensibleDefaults"),d(e,"config",b),d(e,"custom",z),d(e,"oneOf",J),d(e,"instanceOf",G),d(e,"oneOfType",R),d(e,"arrayOf",U),d(e,"objectOf",H),d(e,"shape",K),d(e,"utils",{validate(t,r){return h(r,t,!0)===!0},toType:se}),e})();function W(e=oe()){var t;return t=class extends fe{static get sensibleDefaults(){return T({},this.defaults)}static set sensibleDefaults(r){if(r===!1){this.defaults={};return}if(r===!0){this.defaults=T({},e);return}this.defaults=T({},r)}},d(t,"defaults",T({},e)),t}class ce extends W(){}u.any=D,u.array=k,u.arrayOf=U,u.bool=w,u.config=b,u.createTypes=W,u.custom=z,u.default=ce,u.fromType=ie,u.func=L,u.instanceOf=G,u.integer=I,u.nullable=M,u.number=F,u.object=C,u.objectOf=H,u.oneOf=J,u.oneOfType=R,u.shape=K,u.string=B,u.symbol=Y,u.toType=c,u.toValidableType=v,u.validateType=h,Object.defineProperty(u,"__esModule",{value:!0})}));