Schema Enforcement at the Data Layer

Schema Enforcement at the Data Layer

The Lesson

Adding runtime schema validation to your data loading layer catches entire categories of bugs that would otherwise surface as confusing UI glitches. The cost is a one-time schema definition and a few lines of validation code. The payoff is immediate, clear error messages instead of silent wrong behavior.

Context

A quiz application loaded exam data from JSON files. Without schema validation, a missing field (e.g., no correct-answer on a question) would silently produce a quiz where no answer was ever marked correct. A wrong type (e.g., difficulty: 3 instead of difficulty: "intermediate") would break filtering without any error.

What Happened

  1. The application initially loaded exam data from JSON files without any validation. Missing fields or wrong types only surfaced as broken UI — a quiz where no answer was marked correct, a filter that silently excluded everything.
  2. A JSON Schema (Draft 2020-12) was written to define the exact shape of exam data: metadata fields, question structure, choice constraints, hint levels, and difficulty enums.
  3. An ExamLoader class was created that validates every exam file against this schema on load using Ajv, loaded from a CDN for the browser and aliased to node_modules in the test config.
  4. Validation errors are now surfaced immediately with the specific JSON path and constraint that failed (e.g., questions[14].difficulty must be one of: basic, intermediate, advanced).
  5. The schema doubled as living documentation — new contributors read it to understand the data format instead of reverse-engineering the parser.

Key Insights

Related Lessons