Skip to content

Commit

Permalink
feat(py): Schema Registry
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dygalo <[email protected]>
  • Loading branch information
Stranger6667 committed Feb 7, 2025
1 parent ab71a6d commit 6a529dd
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 69 deletions.
4 changes: 4 additions & 0 deletions crates/jsonschema-py/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Added

- Added `Registry` class for schema reuse and reference resolution.

### Performance

- Significantly improved validator compilation speed by using pointer-based references to schema fragments instead of cloning them during traversal.
Expand Down
55 changes: 55 additions & 0 deletions crates/jsonschema-py/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,61 @@ validator.is_valid({
}) # False
```

## Schema Registry

For applications that frequently use the same schemas, you can create a registry to store and reference them efficiently:

```python
import jsonschema_rs

# Create a registry with schemas
registry = jsonschema_rs.Registry([
("https://example.com/address.json", {
"type": "object",
"properties": {
"street": {"type": "string"},
"city": {"type": "string"}
}
}),
("https://example.com/person.json", {
"type": "object",
"properties": {
"name": {"type": "string"},
"address": {"$ref": "https://example.com/address.json"}
}
})
])

# Use the registry with any validator
validator = jsonschema_rs.validator_for(
{"$ref": "https://example.com/person.json"},
registry=registry
)

# Validate instances
assert validator.is_valid({
"name": "John",
"address": {"street": "Main St", "city": "Boston"}
})
```

The registry can be configured with a draft version and a retriever for external references:

```python
import jsonschema_rs

registry = jsonschema_rs.Registry(
resources=[
(
"https://example.com/address.json",
{}
)
], # Your schemas
draft=jsonschema_rs.Draft202012, # Optional
retriever=lambda uri: {} # Optional
)
```

## Error Handling

`jsonschema-rs` provides detailed validation errors through the `ValidationError` class, which includes both basic error information and specific details about what caused the validation to fail:
Expand Down
Loading

0 comments on commit 6a529dd

Please sign in to comment.