$ show horejsek/python-fastjsonschema
This repository provides `fastjsonschema`, a Python library for validating JSON data against JSON Schema drafts 04, 06, 07, and 2019-09. It achieves high performance by generating optimized Python validation code at compile time and executing it via `exec()`. The library's architecture uses a hierarchy of code generators (`CodeGeneratorDraft04` → `CodeGeneratorDraft06` → `CodeGeneratorDraft07` → `CodeGeneratorDraft2019`) to handle draft-specific keywords. A `RefResolver` manages URI resolution and `$ref` dereferencing. The project includes a comprehensive test suite (including the official JSON Schema Test Suite) and a CLI entry point (`__main__.py`) for generating validator code to a file. It is designed for maximum speed in repeated validation scenarios, and exposes `compile()`, `compile_to_code()`, and a convenience `validate()` function.
The LLM identified 3 files worth highlighting for reuse:
Provides a decorator `indent` that allows a method to be used both as a normal method and as a context manager for auto-indenting code blocks. The `Indent` class manages indentation levels, scope tracking, and optional block merging when consecutive blocks have identical conditions. This is a clean, reusable pattern for any code generation task that requires indented blocks.
Defines three exception classes: `JsonSchemaException` (base), `JsonSchemaValueException` (raised on validation failure), and `JsonSchemaDefinitionException` (raised on invalid schema). `JsonSchemaValueException` includes a `path` property computed from the `name` attribute by splitting on '.', '[', and ']', and a `rule_definition` property that extracts the relevant schema keyword value. This is a well-designed exception hierarchy that provides structured error information and could be extracted for any validation library.
Implements `RefResolver`, a class that resolves JSON Schema `$ref` URIs and manages resolution scopes. It walks the schema tree, caches remote schemas, handles plain-name fragments, and enforces a maximum walk depth to prevent denial of service. The `resolve()` and `in_scope()` context managers provide a clean API for dereferencing. This module is largely self-contained (depends only on `urllib.parse`, `json`, and a local exception class) and could be reused in other JSON Schema tools.