sampleOf
A value of schema s to start a new row from: each field a value of its type (the first of a union's, preferring
one that isn't nothing), optional fields left out.
import type { Schema } from "#codebase/tcmts/core/schema"; export function sampleOf(s: Schema, defs: Schema[], depth = 0): unknown { if (depth > 6) return null; switch (s.t) { case "ref": return defs[s.id] ? sampleOf(defs[s.id]!, defs, depth + 1) : null; case "string": return ""; case "number": return 0; case "boolean": return false; case "bigint": return 0n; case "date": return new Date(); case "literal": return s.value; case "union": return sampleOf( s.of.find((m) => m.t !== "null" && m.t !== "undefined") ?? s.of[0] ?? { t: "null" }, defs, depth + 1, ); case "array": return []; case "set": return new Set(); case "map": return new Map(); case "tuple": return s.of.slice(0, s.of.length - s.optional).map((m) => sampleOf(m, defs, depth + 1)); case "object": return Object.fromEntries( Object.entries(s.props) .filter(([, p]) => !p.optional) .map(([k, p]) => [k, sampleOf(p.s, defs, depth + 1)]), ); default: return null; } }