UNPKG

ts-data-objects

Version:

Simple lightweight library for enhancing plain JS objects with TypeScript wrappers enabling easy API data assertion and validation

53 lines (52 loc) 1.53 kB
import { D as A } from "./dataError-aVfBT4F-.js"; const F = (e, i) => { const o = (a, l) => { const p = l ?? i; for (const [c, [t, y]] of Object.entries(p)) { const r = a[c]; let n = !1; if (typeof t == "string" ? n = y ? Array.isArray(r) && r.every((u) => typeof u === t) : typeof r === t : n = y ? Array.isArray(r) && r.every((u) => o(u, t)) : o(r, t), !n) return !1; } return !0; }, s = (a) => o(a), f = (a, l) => { const p = l ?? i; for (const [c, [t, y]] of Object.entries(p)) { const r = a[c]; if (typeof t == "string") { if (y) { if (!(Array.isArray(r) && r.every((n) => typeof n === t))) return `[${c}]: "${r}" (invalid array of primitives)`; } else if (typeof r !== t) return `[${c}]: "${r}" (invalid primitive)`; } else if (y) { if (!Array.isArray(r)) return `[${c}]: "${r}" (invalid object array)`; for (const n of r) { const u = f(n, t); if (u != null) return u; } } else { const n = f(r, t); if (n != null) return n; } } }, _ = (a) => f(a), d = { [`is${e}`]: s }, v = { [`valid${e}`]: _ }, $ = { [`${e}Rules`]: i }; return { ...d, ...v, ...$ }; }, D = (e, i, o, s) => { const f = o(s); if (f) throw new A(e, s, f); return i(s); }, b = (e, i, o) => (s) => D(e, i, o, s); export { F as dataDeepGuard, b as dataDeepParser }; //# sourceMappingURL=deep.js.map