Tables as typed rows
read-02 · TypeScript
A table leaf with cell schemas reads back as an iterable of typed rows — no cell parsing in the consumer.
Builds on: One contract, two doors
The artifact
Section titled “The artifact”A TypeScript program against the library API; inline comments show the resulting values and behavior.
import { contract, sections, section, table } from "markdown-contract";import { z } from "zod";
const runbook = contract({ body: sections({ order: "none", allowUnknown: true }, [ section("Ports", { content: table({ columns: ["Name", "Port"], minRows: 1, cells: { Port: z.coerce.number().int() }, }), }), ]),});
const doc = runbook.read(src, { path: "runbooks/api.md" });
// The section's sole content is a table, so its key IS the table view.const ports = doc.body.Ports;for (const row of ports) { row.Name; // string — raw cell text row.Port; // number — the cells schema typed it}ports.column("Name"); // one column, across all rowsports.find((r) => r.Port === 8080); // the first matching rowports.rowPos(0); // source position of the first data rowWhat it exercises
Section titled “What it exercises”table leaf with cell schemasTableView iteration, column(), find()table promotion (the heading is the table)row positions for follow-up findings
Continue
Section titled “Continue”- Previous: One contract, two doors
- Next: Anchors make blocks addressable
- Group: Read markdown as typed data · All examples
- Reference: Typed model reference