UNPKG

check-types-mini

Version:
25 lines (16 loc) 5.55 kB
/** * @name check-types-mini * @fileoverview Validate options object * @version 8.0.23 * @author Roy Revelt, Codsen Ltd * @license MIT * {@link https://codsen.com/os/check-types-mini/} */ import i from"type-detect";import{traverse as N}from"ast-monkey-traverse";import{arrayiffy as j}from"arrayiffy-if-string";import E from"object-path";import{isMatch as S}from"matcher";import{isPlainObject as $,pullAll as g,intersection as k}from"codsen-utils";var w="8.0.23";var R=w,v={ignoreKeys:[],ignorePaths:[],acceptArrays:!1,acceptArraysIgnore:[],enforceStrictKeyset:!0,schema:{},msg:"check-types-mini",optsVarName:"opts"};function T(a,r,p){function y(s){return s!=null}function b(s,l){return typeof l=="string"&&(l=j(l)),Array.from(s).filter(n=>!l.some(t=>S(n,t,{caseSensitive:!0})))}let V=Object.prototype.hasOwnProperty,A=["any","anything","every","everything","all","whatever","whatevs"];if(!y(a))throw new Error("check-types-mini: [THROW_ID_01] First argument is missing!");let e={...v,...p};if(typeof e.ignoreKeys=="string"&&(e.ignoreKeys=[e.ignoreKeys]),typeof e.ignorePaths=="string"&&(e.ignorePaths=[e.ignorePaths]),typeof e.acceptArraysIgnore=="string"&&(e.acceptArraysIgnore=[e.acceptArraysIgnore]),e.msg=`${e.msg}`.trim(),e.msg[e.msg.length-1]===":"&&(e.msg=e.msg.slice(0,e.msg.length-1).trim()),$(e.schema))Object.keys(e.schema).forEach(s=>{if($(e.schema[s])){let l={};N(e.schema[s],(n,t,m)=>{let u=t!==void 0?t:n;return!Array.isArray(u)&&!$(u)&&(l[`${s}.${m.path}`]=u),u}),delete e.schema[s],e.schema={...e.schema,...l}}}),Object.keys(e.schema).forEach(s=>{Array.isArray(e.schema[s])||(e.schema[s]=[e.schema[s]]),e.schema[s]=e.schema[s].map(l=>`${l}`.toLowerCase().trim())});else if(e.schema!=null)throw new Error(`check-types-mini: opts.schema was customised to ${JSON.stringify(e.schema,null,0)} which is not object but ${typeof e.schema}`);if(y(r)||(r={}),e.enforceStrictKeyset)if(y(e.schema)&&Object.keys(e.schema).length){if(r&&b(g(Object.keys(a),Object.keys(r).concat(Object.keys(e.schema))),e.ignoreKeys).length){let s=g(Object.keys(a),Object.keys(r).concat(Object.keys(e.schema)));throw new TypeError(`${e.msg}: ${e.optsVarName}.enforceStrictKeyset is on and the following key${s.length>1?"s":""} ${s.length>1?"are":"is"} not covered by schema and/or reference objects: ${s.join(", ")}`)}}else if($(r)&&Object.keys(r).length){if(b(g(Object.keys(a),Object.keys(r)),e.ignoreKeys).length!==0){let s=g(Object.keys(a),Object.keys(r));throw new TypeError(`${e.msg}: The input object has key${s.length>1?"s":""} which ${s.length>1?"are":"is"} not covered by the reference object: ${s.join(", ")}`)}else if(b(g(Object.keys(r),Object.keys(a)),e.ignoreKeys).length!==0){let s=g(Object.keys(r),Object.keys(a));throw new TypeError(`${e.msg}: The reference object has key${s.length>1?"s":""} which ${s.length>1?"are":"is"} not present in the input object: ${s.join(", ")}`)}}else throw new TypeError(`${e.msg}: Both ${e.optsVarName}.schema and reference objects are missing! We don't have anything to match the keys as you requested via resolvedOpts.enforceStrictKeyset!`);let h=[];N(a,(s,l,n)=>{let t=l,m=s;if(n.parentType==="array"&&(m=void 0,t=s),Array.isArray(h)&&h.length&&h.some(o=>n.path.startsWith(o))||m&&e.ignoreKeys.some(o=>m&&S(m,o))||e.ignorePaths.some(o=>S(n.path,o)))return t;let u=!(!$(t)&&!Array.isArray(t)&&Array.isArray(n.parent)),f=!1;$(e.schema)&&V.call(e.schema,n.path)&&(f=!0);let d=!1;if($(r)&&E.has(r,n.path)&&(d=!0),e.enforceStrictKeyset&&u&&!f&&!d)throw new TypeError(`${e.msg}: ${e.optsVarName}.${n.path} is neither covered by reference object (second input argument), nor ${e.optsVarName}.schema! To stop this error, turn off ${e.optsVarName}.enforceStrictKeyset or provide some type reference (2nd argument or ${e.optsVarName}.schema). Debug info: obj = ${JSON.stringify(a,null,4)} ref = ${JSON.stringify(r,null,4)} innerObj = ${JSON.stringify(n,null,4)} resolvedOpts = ${JSON.stringify(e,null,4)} current = ${JSON.stringify(t,null,4)} `);if(f){let o=j(e.schema[n.path]).map(c=>`${c}`.toLowerCase());if(E.set(e.schema,n.path,o),k(o,A).length)h.push(n.path);else if(t!==!0&&t!==!1&&!o.includes(i(t).toLowerCase())||(t===!0||t===!1)&&!o.includes(String(t))&&!o.includes("boolean"))if(Array.isArray(t)&&e.acceptArrays){for(let c=0,O=t.length;c<O;c++)if(!o.includes(i(t[c]).toLowerCase()))throw new TypeError(`${e.msg}: ${e.optsVarName}.${n.path}.${c}, the ${c}th element (equal to ${JSON.stringify(t[c],null,0)}) is of a type ${i(t[c]).toLowerCase()}, but only the following are allowed by the ${e.optsVarName}.schema: ${o.join(", ")}`)}else throw new TypeError(`${e.msg}: ${e.optsVarName}.${n.path} was customised to ${i(t)!=="string"?'"':""}${JSON.stringify(t,null,0)}${i(t)!=="string"?'"':""} (type: ${i(t).toLowerCase()}) which is not among the allowed types in schema (which is equal to ${JSON.stringify(o,null,0)})`)}else if(r&&$(r)&&d){let o=E.get(r,n.path);if(e.acceptArrays&&Array.isArray(t)&&!e.acceptArraysIgnore.includes(s)){if(!t.every(O=>i(O).toLowerCase()===i(r[s]).toLowerCase()))throw new TypeError(`${e.msg}: ${e.optsVarName}.${n.path} was customised to be array, but not all of its elements are ${i(r[s]).toLowerCase()}-type`)}else if(i(t)!==i(o))throw new TypeError(`${e.msg}: ${e.optsVarName}.${n.path} was customised to ${i(t).toLowerCase()==="string"?"":'"'}${JSON.stringify(t,null,0)}${i(t).toLowerCase()==="string"?"":'"'} which is not ${i(o).toLowerCase()} but ${i(t).toLowerCase()}`)}return t})}function M(a,r,p){T(a,r,p)}export{M as checkTypesMini,v as defaults,R as version};