Skip to content

Commit e4059a8

Browse files
committed
feat(python): Custom retrievers for external references
Signed-off-by: Dmitry Dygalo <[email protected]>
1 parent 3c25034 commit e4059a8

File tree

4 files changed

+223
-24
lines changed

4 files changed

+223
-24
lines changed

crates/jsonschema-py/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Added
6+
7+
- Custom retrievers for external references. [#372](https://github.com/Stranger6667/jsonschema/issues/372)
8+
59
### Changed
610

711
- Improved error message for unknown formats.

crates/jsonschema-py/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,45 @@ On instance:
157157
"unknown"'''
158158
```
159159

160+
## External References
161+
162+
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:
163+
164+
```python
165+
import jsonschema_rs
166+
167+
def retrieve(uri: str):
168+
schemas = {
169+
"https://example.com/person.json": {
170+
"type": "object",
171+
"properties": {
172+
"name": {"type": "string"},
173+
"age": {"type": "integer"}
174+
},
175+
"required": ["name", "age"]
176+
}
177+
}
178+
if uri not in schemas:
179+
raise KeyError(f"Schema not found: {uri}")
180+
return schemas[uri]
181+
182+
schema = {
183+
"$ref": "https://example.com/person.json"
184+
}
185+
186+
validator = jsonschema_rs.validator_for(schema, retriever=retrieve)
187+
188+
# This is valid
189+
validator.is_valid({
190+
"name": "Alice",
191+
"age": 30
192+
})
193+
194+
# This is invalid (missing "age")
195+
validator.is_valid({
196+
"name": "Bob"
197+
}) # False
198+
160199
## Performance
161200

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

0 commit comments

Comments
 (0)