CONSUME-AS-DATA-01: Open the read() door for typed frontmatter
What it demonstrates
Section titled “What it demonstrates”contract.read returns a typed Doc whose frontmatter fields you pull with no separate types file.
Builds on: nothing — this is the first rung of Consume as Typed Data: Reading the Document as a Model.
How it’s done
Section titled “How it’s done”A TypeScript program against the library API; inline comments show the resulting values and behavior.
import { contract, sections, section } from "markdown-contract";import { z } from "zod";
const c = contract({ frontmatter: z.object({ id: z.string(), status: z.string() }).strict(), body: sections({ allowUnknown: true }, [section("Summary")]),});
const doc = c.read(src, { path: "notes/note.md" }); // Doc, or throws ContractErrorconsole.log(doc.frontmatter.id); // typed from the same contract that validatedSurfaces exercised
Section titled “Surfaces exercised”contract()contract.read(src,{path})Doc.frontmatter typed from the frontmatter Zod schema