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