Skip to main content

Dynamically generate pydantic classes from JSONschema at runtime

Project description

Oxley: Pydantic classes from JSON schema

Oxley generates Pydantic classes at runtime from user-provided JSON schema documents. Heavily indebted to packages like 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 and Starlite.

Quick start

Install from PIP:

python3 -m pip install oxley

Given a simple JSONschema document:

{
  "$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:

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.

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:

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:

pre-commit install

Run tests with tox:

tox

MIT License

Copyright (c) 2022 James Stevenson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

oxley-0.0.2.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

oxley-0.0.2-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file oxley-0.0.2.tar.gz.

File metadata

  • Download URL: oxley-0.0.2.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for oxley-0.0.2.tar.gz
Algorithm Hash digest
SHA256 13b298fb5d69071c2cdaa3a6001f17b994b8b5b7c96a0479204353bc98fda78a
MD5 f32ea1c578c4198e180b90b598c0e7a6
BLAKE2b-256 5a9a86aea45c66278f35706f929278cb7194967aa7f794ec292668f3a1bad642

See more details on using hashes here.

File details

Details for the file oxley-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: oxley-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for oxley-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 26fc506e043b5a45b3e42f9a1e2363a21b0206785e07ff724772c19d99eab0c7
MD5 bd1a4ffd3ab4f0b263b1a27d8eb52abe
BLAKE2b-256 40c3840cc2ef3abfa7b01748d093c98a9808ff224c218bcc03460afcf681cc35

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page