Skip to main content

A high-performance JSON Schema validator for Python

Project description

jsonschema-rs

Build Version Python versions License Supported Dialects

A high-performance JSON Schema validator for Python.

import jsonschema_rs

schema = {"maxLength": 5}
instance = "foo"

# One-off validation
try:
    jsonschema_rs.validate(schema, "incorrect")
except jsonschema_rs.ValidationError as exc:
    assert str(exc) == '''"incorrect" is longer than 5 characters

Failed validating "maxLength" in schema

On instance:
    "incorrect"'''

# Build & reuse (faster)
validator = jsonschema_rs.validator_for(schema)

# Iterate over errors
for error in validator.iter_errors(instance):
    print(f"Error: {error}")
    print(f"Location: {error.instance_path}")

# Boolean result
assert validator.is_valid(instance)

⚠️ Upgrading from pre-0.20.0? Check our Migration Guide for key changes.

Highlights

  • 📚 Support for popular JSON Schema drafts
  • 🌐 Remote reference fetching (network/file)
  • 🔧 Custom format validators

Supported drafts

Compliance levels vary across drafts, with newer versions having some unimplemented keywords.

  • Draft 2020-12
  • Draft 2019-09
  • Draft 7
  • Draft 6
  • Draft 4

You can check the current status on the Bowtie Report.

Limitations

  • No support for arbitrary precision numbers

Installation

To install jsonschema-rs via pip run the following command:

pip install jsonschema-rs

Usage

If you have a schema as a JSON string, then you could pass it to validator_for to avoid parsing on the Python side:

validator = jsonschema_rs.validator_for('{"minimum": 42}')
...

You can use draft-specific validators for different JSON Schema versions:

import jsonschema_rs

# Automatic draft detection
validator = jsonschema_rs.validator_for({"minimum": 42})

# Draft-specific validators
validator = jsonschema_rs.Draft7Validator({"minimum": 42})
validator = jsonschema_rs.Draft201909Validator({"minimum": 42})
validator = jsonschema_rs.Draft202012Validator({"minimum": 42})

For backwards compatibility, you can still use the JSONSchema class with the draft argument, but this is deprecated:

import jsonschema_rs

# Deprecated: Use draft-specific validators instead
validator = jsonschema_rs.JSONSchema(
    {"minimum": 42}, 
    draft=jsonschema_rs.Draft7
)

JSON Schema allows for format validation through the format keyword. While jsonschema-rs provides built-in validators for standard formats, you can also define custom format validators for domain-specific string formats.

To implement a custom format validator:

  1. Define a function that takes a str and returns a bool.
  2. Pass it with the formats argument.
import jsonschema_rs

def is_currency(value):
    # The input value is always a string
    return len(value) == 3 and value.isascii()


validator = jsonschema_rs.validator_for(
    {"type": "string", "format": "currency"}, 
    formats={"currency": is_currency}
)
validator.is_valid("USD")  # True
validator.is_valid("invalid")  # False

Performance

jsonschema-rs is designed for high performance, outperforming other Python JSON Schema validators in most scenarios:

  • Up to 30-390x faster than jsonschema for complex schemas and large instances
  • Generally 2-5x faster than fastjsonschema on CPython
  • Comparable or slightly slower performance for very small schemas

For detailed benchmarks, see our full performance comparison.

Python support

jsonschema-rs supports CPython 3.8, 3.9, 3.10, 3.11, and 3.12.

Acknowledgements

This library draws API design inspiration from the Python jsonschema package. We're grateful to the Python jsonschema maintainers and contributors for their pioneering work in JSON Schema validation.

Support

If you have questions, need help, or want to suggest improvements, please use GitHub Discussions.

Sponsorship

If you find jsonschema-rs useful, please consider sponsoring its development.

Contributing

We welcome contributions! Here's how you can help:

See CONTRIBUTING.md for more details.

License

Licensed under MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonschema_rs-0.20.0.tar.gz (1.3 MB view hashes)

Uploaded Source

Built Distributions

jsonschema_rs-0.20.0-cp312-none-win_amd64.whl (1.8 MB view hashes)

Uploaded CPython 3.12 Windows x86-64

jsonschema_rs-0.20.0-cp312-none-win32.whl (1.7 MB view hashes)

Uploaded CPython 3.12 Windows x86

jsonschema_rs-0.20.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

jsonschema_rs-0.20.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.0 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

jsonschema_rs-0.20.0-cp312-cp312-manylinux_2_12_i686.manylinux2010_i686.whl (2.0 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.12+ i686

jsonschema_rs-0.20.0-cp312-cp312-macosx_10_12_x86_64.whl (1.9 MB view hashes)

Uploaded CPython 3.12 macOS 10.12+ x86-64

jsonschema_rs-0.20.0-cp312-cp312-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (3.7 MB view hashes)

Uploaded CPython 3.12 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

jsonschema_rs-0.20.0-cp311-none-win_amd64.whl (1.8 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

jsonschema_rs-0.20.0-cp311-none-win32.whl (1.7 MB view hashes)

Uploaded CPython 3.11 Windows x86

jsonschema_rs-0.20.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

jsonschema_rs-0.20.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.0 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

jsonschema_rs-0.20.0-cp311-cp311-manylinux_2_12_i686.manylinux2010_i686.whl (2.0 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.12+ i686

jsonschema_rs-0.20.0-cp311-cp311-macosx_10_12_x86_64.whl (1.9 MB view hashes)

Uploaded CPython 3.11 macOS 10.12+ x86-64

jsonschema_rs-0.20.0-cp311-cp311-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (3.7 MB view hashes)

Uploaded CPython 3.11 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

jsonschema_rs-0.20.0-cp310-none-win_amd64.whl (1.8 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

jsonschema_rs-0.20.0-cp310-none-win32.whl (1.7 MB view hashes)

Uploaded CPython 3.10 Windows x86

jsonschema_rs-0.20.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

jsonschema_rs-0.20.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.0 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

jsonschema_rs-0.20.0-cp310-cp310-manylinux_2_12_i686.manylinux2010_i686.whl (2.0 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.12+ i686

jsonschema_rs-0.20.0-cp310-cp310-macosx_10_12_x86_64.whl (1.9 MB view hashes)

Uploaded CPython 3.10 macOS 10.12+ x86-64

jsonschema_rs-0.20.0-cp310-cp310-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (3.7 MB view hashes)

Uploaded CPython 3.10 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

jsonschema_rs-0.20.0-cp39-none-win_amd64.whl (1.8 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

jsonschema_rs-0.20.0-cp39-none-win32.whl (1.7 MB view hashes)

Uploaded CPython 3.9 Windows x86

jsonschema_rs-0.20.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

jsonschema_rs-0.20.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.0 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

jsonschema_rs-0.20.0-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl (2.0 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ i686

jsonschema_rs-0.20.0-cp39-cp39-macosx_10_12_x86_64.whl (1.9 MB view hashes)

Uploaded CPython 3.9 macOS 10.12+ x86-64

jsonschema_rs-0.20.0-cp39-cp39-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (3.7 MB view hashes)

Uploaded CPython 3.9 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

jsonschema_rs-0.20.0-cp38-none-win_amd64.whl (1.8 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

jsonschema_rs-0.20.0-cp38-none-win32.whl (1.7 MB view hashes)

Uploaded CPython 3.8 Windows x86

jsonschema_rs-0.20.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

jsonschema_rs-0.20.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.0 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

jsonschema_rs-0.20.0-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl (2.0 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ i686

jsonschema_rs-0.20.0-cp38-cp38-macosx_10_12_x86_64.whl (1.9 MB view hashes)

Uploaded CPython 3.8 macOS 10.12+ x86-64

jsonschema_rs-0.20.0-cp38-cp38-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (3.7 MB view hashes)

Uploaded CPython 3.8 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page