LEVEL 1 · STARTER

JSON Schema Output Guard

A schema preflight and bounded-repair receipt for model output contracts. Call guard(raw,schema) at the application boundary, or repair(generate,schema,maxAttempts) where generate receives structured Issue[] and a one-based attempt number.

4 stages~8 hoursTypeScriptcore
Parse the untrusted boundaryWalk a schema recursivelyReject ambiguous and unsupported contractsRepair with a finite budget
YOU END UP WITH

A schema preflight and bounded-repair receipt for model output contracts.

USEFUL FOR
  • Call guard(raw,schema) at the application boundary, or repair(generate,schema,maxAttempts) where generate receives structured Issue[] and a one-based attempt number.
LEARN FIRST
  • Node 22.18+ and Python 3 for the grader
  • TypeScript unions, recursive functions, JSON and async callbacks.
PROJECT OUTLINE

JSON Schema Output Guard in 4 stages

A schema preflight and bounded-repair receipt for model output contracts.

  1. Parse the untrusted boundary

    A model response is a byte string. Parse the entire string as JSON, reject markdown wrappers and trailing text, and cap its UTF-8 byte size before parsing. Accept JSON primitives as well as objects: the schema, not the parser, determines whether a primitive is useful. Do not remove text until it happens to parse because that hides the actual output contract.

    starter · ~2 hours · TypeScript

    parseJSON
  2. Walk a schema recursively

    Implement object, array and primitive validation. Required properties are checked with own-property semantics. An inherited property does not satisfy the contract. Treat integer as a refinement of number, reject non-finite numbers, and preserve a path for every failure. Bound recursive descent at depth 32 so a schema and value cannot consume the stack indefinitely.

    core · ~2 hours · TypeScript

    validate
  3. Reject ambiguous and unsupported contracts

    Add numeric bounds, enum membership, minimum Unicode string length, array bounds, and closed objects. Escape slash and tilde inside property paths. A keyword outside this educational subset is a configuration error, not silent success. This implementation intentionally does not claim complete JSON Schema conformance: references, formats and combinators require additional work.

    core · ~2 hours · TypeScript

    validateguard
  4. Repair with a finite budget

    Feed only structured validation feedback into a generation callback. Count each call before interpreting its output. Stop immediately after acceptance and return an explicit exhausted state after the final rejected attempt. Provider failures propagate instead of being disguised as schema failures. The trace records each attempt so a successful third response does not conceal two earlier contract violations.

    core · ~2 hours · TypeScript

    repair

All projects · Course catalog · Source on GitHub