https://github.com/jsstevenson/oxley
Dynamically generate Pydantic classes from JSONschema
https://github.com/jsstevenson/oxley
json-schema pydantic python
Last synced: 2 months ago
JSON representation
Dynamically generate Pydantic classes from JSONschema
- Host: GitHub
- URL: https://github.com/jsstevenson/oxley
- Owner: jsstevenson
- License: mit
- Created: 2022-04-12T02:37:58.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-08-29T02:04:32.000Z (almost 4 years ago)
- Last Synced: 2026-03-01T18:54:24.403Z (4 months ago)
- Topics: json-schema, pydantic, python
- Language: Python
- Homepage: https://pypi.org/project/oxley/
- Size: 68.4 KB
- Stars: 26
- Watchers: 1
- Forks: 1
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Oxley: Pydantic classes from JSON schema
**Oxley** generates [Pydantic](https://github.com/samuelcolvin/pydantic) classes at runtime from user-provided JSON schema documents. Heavily indebted to packages like [Python-JSONschema-Objects](https://github.com/cwacek/python-jsonschema-objects), Oxley enables data validation pipelines to function dynamically, and with the help of Pydantic, interface directly with popular web frameworks such as [FastAPI](https://github.com/tiangolo/fastapi) and [Starlite](https://github.com/starlite-api/starlite).
## Quick start
Install from PIP:
```shell
python3 -m pip install oxley
```
Given a simple JSONschema document:
```json
{
"$id": "https://github.com/jsstevenson/oxley",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"$defs": {
"User": {
"type": "object",
"properties": {
"username": {"type": "string"},
"user_id": {"type": "number"}
},
"required": ["username", "user_id"]
},
"Post": {
"type": "object",
"properties": {
"author": {"$ref": "#/$defs/User"},
"content": {"type": "string"},
"allow_responses": {"type": "boolean"}
},
"required": ["author", "content"]
}
}
}
```
Provide a schema and construct classes:
``` python
from oxley import ClassBuilder
schema_path = "path/to/my_jsonschema_document.json"
cb = ClassBuilder(schema_path)
User, Post = cb.build_classes()
```
The resulting objects are functioning Pydantic classes, providing features like runtime data validation and matching schema output.
``` python
dril = User(username="dril", user_id=99)
post = Post(author=dril, content="should i learn Letters first? or choose the path of Numbers? a queston every baby must ask it self")
another_post = Post(author=dril) # raises pydantic.ValidationError
```
## Development
Clone and install dev and test dependencies:
``` shell
git clone https://github.com/jsstevenson/oxley
cd oxley
# make virtual environment of your choosing
python3 -m pip install ".[dev,test]"
```
Install pre-commit hooks:
``` shell
pre-commit install
```
Run tests with tox:
```
tox
```