UNPKG

ll-package

Version:

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