Mini JSON Schema Validator

hard
Unit 2 · Decorators0/3 to milestone

LLM structured outputs and API requests are validated against JSON Schema. Implement a useful subset.

validate(instance, schema) returns a list of error strings (empty if valid). Each error is "<path>: <message>", where the path is like $, $.user.age or $.tags[2]. Support:

  • "type": one of "object", "array", "string", "integer", "number", "boolean" or "null". Booleans are not integers or numbers, and integers count as numbers. On a type mismatch, report it and check nothing else for that value.
  • "properties" (validate each present key) and "required" (report each missing key at the object's path as missing required property '<key>').
  • "items": a schema for every array element.
  • "enum": the value must be one of the listed values.
  • "minimum" / "maximum" for numbers, and "minLength" / "maxLength" for strings.

Collect all errors, in the order you check them: type, enum, min/max checks, required (in listed order), properties (in schema order), then items (in index order). The message wording is up to you, as long as the path is right and each problem produces exactly one error.

#validation#recursion#structured-output2 visible + 4 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Mini JSON Schema Validator · TechGradify