Skip to content

CONSUME-AS-DATA-10: Contrast validate() with read()

validate() returns {findings, doc, tree} and never throws, while read() hands back a typed Doc or throws ContractError on error-level findings.

Builds on: CONSUME-AS-DATA-01: Open the read() door for typed frontmatter

A TypeScript program against the library API; inline comments show the resulting values and behavior.

// Collect everything, never throw:
const { findings, doc, tree } = c.validate(src, { path: "decision.md" });
findings.forEach(f => console.log(f.level, f.path, f.message));
if (doc) doc.frontmatter.id; // doc present only when valid
// Or take the typed door and let errors stop you:
try { const d = c.read(src, { path: "decision.md" }); }
catch (e) { /* ContractError carries the error-level findings */ }
  • contract.validate(src,{path}) → {findings, doc, tree}
  • Finding.level/.path/.message
  • contract.read + ContractError