Skip to content

0016. Keep Zod out of the core runtime; validate documents at load time

  • Status: Accepted (2026-11-27, Phase 1a gate)
  • Date: 2026-11-27
  • Phase: 1

ADR 0007 makes Zod the schema source of truth. The core runtime has a 15 kB budget, and components must stay plain data that AI tools can edit. If every runtime package imported Zod, every game would pay for it even when its data was already validated.

  • @ludrel/core has no runtime dependencies. Components may carry an optional schema typed as Standard Schema (~standard.validate), which Zod 4 implements, so schemas plug in without importing Zod into core.
  • Document validation (manifest, scene, level, prefab, content pack) lives in @ludrel/schema and runs when a project is loaded (loadProject), on every hot reload, and in the headless runner/MCP tools.
  • Validation is injected, not imported: startGame({ validate }) and loadProject(files, { validate }) take a DocumentValidator. The standard one, schemaValidator, lives in the @ludrel/framework/validate subpath, the only framework module that imports @ludrel/schema at runtime.
  • Development and tools validate: games import the validator behind import.meta.env.DEV (dead code in production builds), and the headless runner validates by default.
  • Production builds validate at build time: the platformData Vite plugin (@ludrel/devtools/vite) validates every document when the build starts (errors fail the build) and replaces each JSON document in the bundle with its normalized form (migrated, defaults applied). At runtime, documents are trusted; only cross-references are checked (no Zod needed).
  • Implemented in Phase 1b. The demo bundle went from 262 kB to 156 kB raw (Zod removed).
  • A production game that loads documents from anywhere other than its own build (e.g. content packs at runtime) must validate them, either server-side (Phase 3+ platform pipeline) or by passing a validator.
  • Vite’s config must load workspace TypeScript sources, so game scripts run Vite with --configLoader runner. Core stays ~5 kB. Component-level validation of JSON data happens where data enters (addComponentsFromData), reporting issues instead of throwing so a bad level never crashes a running game.