hi-guardian
Version:
Runtime type guard generator for TypeScript. Create validators for objects, arrays, tuples and unions with automatic type inference. 3x faster than io-ts with zero dependencies.
2 lines • 1.66 kB
JavaScript
import {isStrictObject,isEmptyObject}from'hi-datatype-operation';function p(e){return t=>{try{if(!isStrictObject(t))throw new Error("Not a plain object");if(isEmptyObject(t))throw new Error("Empty object");return Object.keys(e).every(r=>{let o=e[r];return o(t[r])})}catch(r){return console.error(r),false}}}function d(e){return t=>{try{if(!Array.isArray(t))throw new Error("Not an array");if(t.length===0)throw new Error("Empty array");return t.every(r=>e(r))}catch(r){return console.error(r),false}}}function c(e,t=Array.from({length:e.length},(r,o)=>o)){let r=e.length-1,o=[...new Set(t)].sort((n,a)=>n-a);o.forEach(n=>{if(n<0||n>r)throw new Error(`Invalid required index ${n}. Valid range: 0-${r}`)});let u=o.length>0?Math.max(...o)+1:0;return n=>{try{if(!Array.isArray(n))throw new Error("Input is not an array");if(n.length<u||n.length>e.length)throw new Error(`Invalid tuple length. Required ${u}-${e.length}, got ${n.length}`);return o.forEach(a=>{if(a>=n.length||n[a]===void 0)throw new Error(`Required element at index ${a} is missing`)}),n.every((a,i)=>{let y=e[i];return y?.(a)??!1})}catch(a){return console.error("[Tuple Guard Error]",a),false}}}function g(e){if(!Array.isArray(e)||e.length===0)throw new Error("Union type guard requires at least one validator");return t=>{try{return e.some(r=>{let o=r(t);if(typeof o!="boolean")throw new Error(`Validator must return boolean, got ${typeof o}`);return o})}catch(r){return console.error("[Union Type Guard Error]",r),false}}}export{d as createArrayTypeGuard,p as createObjTypeGuard,c as createTupleTypeGuard,g as createUnionTypeGuard};//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map