fastjsonschema

Fast JSON schema validator for Python that compiles schemas to generated code

Library
PyPI
v2.22.2
496stars
BSD 3-Clause License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
73/100Good
Development Activity88
Maintenance48
Community76
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
82/100Excellent
Architecture80
Code Quality78
Innovation84
Learning Curve85

fastjsonschema is a Python library that validates JSON documents against JSON Schema (drafts 04, 06, and 07) by compiling each schema into generated Python source code rather than interpreting it at validation time. This code-generation approach makes it dramatically faster than interpreter-based validators like jsonschema — the project’s own benchmarks show roughly 25ms for valid inputs versus multi-second run times for popular alternatives — which matters when validation runs on every request in a high-throughput service.

The library is used as the JSON Schema validation engine inside Jupyter’s nbformat, making it one of the most widely deployed validators in the Python data-science ecosystem. It exposes both a one-shot validate() function and a compile() function that returns a reusable validator, so callers who validate the same schema repeatedly can pay the compilation cost once and reuse the generated function afterward.

What You Get

  • A validate(schema, data) function for one-off, one-shot JSON Schema validation
  • A compile(schema) function that generates and returns a reusable validator function for repeated validation of the same schema
  • Support for JSON Schema drafts 04, 06, and 07, each implemented in its own module (draft04.py, draft06.py, draft07.py)
  • Optional code-generation-to-file mode for squeezing out extra performance in hot paths
  • Descriptive JsonSchemaException errors that point to the offending path and value

Common Use Cases

  • Validating request/response payloads in high-throughput APIs where interpreter-based validators are too slow
  • Validating notebook documents against the nbformat JSON Schema (its largest real-world deployment, via Jupyter)
  • Enforcing config-file or data-pipeline schema conformance in CLI tools and data engineering scripts
  • Any workload that validates the same schema many times and can benefit from a compiled, reusable validator

Under The Hood

Architecture: The core is a CodeGenerator (fastjsonschema/generator.py) shared by draft-specific subclasses (CodeGeneratorDraft04, Draft06, Draft07) that walk a JSON Schema document and emit Python source implementing each keyword’s validation logic, joined via an indent.py helper for readable generated code. A ref_resolver.py module resolves $ref pointers across the schema (including remote refs) before generation, and __init__.py exposes the public validate()/compile() entry points that tie generation, exec(), and caching together.

Tech Stack: Pure Python (3.3+) standard library only — no runtime dependencies — which keeps the package lightweight and easy to vendor; the JSON-Schema-Test-Suite git submodule provides the official cross-language JSON Schema conformance tests used in CI.

Code Quality: The tests/ directory runs the official JSON-Schema-Test-Suite plus custom tests per draft and per keyword, giving broad spec-conformance coverage; pylintrc and tox.ini wire up linting and multi-version testing. The README explicitly documents known deviations from strict JSON Schema semantics (e.g. Python regex dialect, $ anchor handling), which is a notably honest and useful practice for a validation library.

API Design: The public surface is deliberately tiny — validate(schema, data) for convenience and compile(schema) for performance-sensitive reuse — mirroring the ergonomics of Python’s re module (match-once vs. compile-and-reuse). Errors raise a single JsonSchemaException type carrying a human-readable message and the failing path, so callers don’t need to learn a large exception hierarchy.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search