JSON Schema Rust Validator
jsonschema-rs is a high-performance JSON Schema validator for Python, backed by a Rust implementation. It provides fast validation of JSON data against JSON schemas according to various draft specifications. The library is actively maintained with frequent releases, often synchronized across its Python, Ruby, and Rust bindings.
Warnings
- gotcha The `multipleOf` validation for negative numeric instances was incorrectly handled and fixed in version `0.45.1`. If your schemas or data relied on the previous incorrect behavior (e.g., negative numbers incorrectly passing `multipleOf` validation), your validations may now fail as per specification.
- gotcha The `duration` format keyword previously had incorrect handling for certain patterns (e.g., hours/seconds without minutes, years/days without months). This was fixed in version `0.45.1`. If your schema used `duration` format and your data relied on the incorrect parsing, validation might now fail.
- deprecated Flat invocation of the CLI (`jsonschema schema.json -i ...`) was deprecated in favor of explicit subcommands (`jsonschema validate` and `jsonschema bundle`).
- gotcha The `hostname` format validation behavior changed in version `0.44.1`. It now applies legacy RFC 1034 semantics in Draft 4/6 and keeps IDNA A-label validation in Draft 7+. This might affect validation results for `hostname` strings when using older schema drafts.
Install
-
pip install jsonschema-rs
Imports
- JSONSchema
from jsonschema_rs import JSONSchema
- ValidationError
from jsonschema_rs import ValidationError
- ValidationOptions
from jsonschema_rs import ValidationOptions
Quickstart
import jsonschema_rs
import json
schema_str = '''
{
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer", "minimum": 0}
},
"required": ["name", "age"]
}
'''
instance_valid_str = '''
{
"name": "Alice",
"age": 30
}
'''
instance_invalid_str = '''
{
"name": "Bob",
"age": -5
}
'''
schema = json.loads(schema_str)
instance_valid = json.loads(instance_valid_str)
instance_invalid = json.loads(instance_invalid_str)
validator = jsonschema_rs.JSONSchema.from_dict(schema)
# Validate a valid instance
try:
validator.validate(instance_valid)
print("Valid instance passed validation.")
except jsonschema_rs.ValidationError as e:
print(f"Valid instance failed validation unexpectedly: {e}")
# Validate an invalid instance
try:
validator.validate(instance_invalid)
print("Invalid instance passed validation unexpectedly.")
except jsonschema_rs.ValidationError as e:
print(f"Invalid instance caught validation error: {e.message}")