Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

- Custom retrievers for external references. [#372](https://github.com/Stranger6667/jsonschema/issues/372)

### Changed

- Improved error message for unknown formats.
Expand Down
39 changes: 39 additions & 0 deletions crates/jsonschema-py/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,45 @@ On instance:
"unknown"'''
```

## External References

By default, `jsonschema-rs` resolves HTTP references and file references from the local file system. You can implement a custom retriever to handle external references. Here's an example that uses a static map of schemas:

```python
import jsonschema_rs

def retrieve(uri: str):
schemas = {
"https://example.com/person.json": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
},
"required": ["name", "age"]
}
}
if uri not in schemas:
raise KeyError(f"Schema not found: {uri}")
return schemas[uri]

schema = {
"$ref": "https://example.com/person.json"
}

validator = jsonschema_rs.validator_for(schema, retriever=retrieve)

# This is valid
validator.is_valid({
"name": "Alice",
"age": 30
})

# This is invalid (missing "age")
validator.is_valid({
"name": "Bob"
}) # False

## Performance

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