Dynamically create useful data classes from JSON schemas
Project description
Schema Models
Use this library to turn a JSON Schema into a plain 'ol Python dataclass.
Installation
Install this package using the usual suspects.
pip install schemamodels
Usage
This library only supports JSON schemas of type: object
:
{
"$id": "https://schema.dev/fake-schema.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "fake-schema",
"description": "Blue Blah",
"type": "object",
"properties": {
"property_a": {
"default": 5,
"type": "integer"
},
"property_b": {
"type": "string"
}
}
}
from schemamodels import SchemaModelFactory
schema_string = '..'
my_json_schema = json.loads(schema_string)
factory = SchemaModelFactory()
factory.register(my_json_schema)
Use your new dataclass
from schemamodels import exceptions
from schemamodels.dynamic import FakeSchema
your_data_instance = FakeSchema(property_a=2334) # OK
with pytest.raises(exceptions.ValueTypeViolation):
your_data_instance = FakeSchema(property_a="hello")
Why this library exists
Faster than defining dataclasses manually
The class-like syntax of creating dataclasses is a useful way to model a valid JSON schema. This library essentially makes your existing JSON schemas usable as a Python dataclass.
Useable everywhere
Taking the time to construct a plain ol' Pythjon dataclass, ensures widespread utility across of multiple domains of study and applications.
Just-enough validation
The dataclasses are not completely dumb. Every dataclass object that originated from a valid JSON schema, will perform runtime checks on the input you provide it.
These basic checks are performed every time you create a new instance of a generated dataclass:
- Are the field names correct?
- Are the required fields present?
- Does the input match the datatype expectations set forth by the generated dataclass?
License
This codebase is licensed under the GPLv3+ and therefore the use, inclusion, modification, and distribution of this software is governed by the GPL.
To opt-out of the obligations of the GPL, inquire about the commercial license by sending an email to: license@civichacker.com.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for schemamodels-0.9.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7d8acf0061aa6b96dfe2a277845b79c970e520db3121c64ee23222b2704f19f |
|
MD5 | c6bf2341ee789714a544efb2a9822c1f |
|
BLAKE2b-256 | fa34f61692da09c59ef2e756ad52c1f5b4b7dc1e05a25c7a35998cbddf1914e4 |