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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file schemamodels-0.9.1.tar.gz.
File metadata
- Download URL: schemamodels-0.9.1.tar.gz
- Upload date:
- Size: 16.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/6.5.0-18-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2816e9612f86b9368aeae57c5374b2008a2737b32ade5138463027e5a3e90144
|
|
| MD5 |
fecf4b11583b8cd92a26679e5efb1c0d
|
|
| BLAKE2b-256 |
af4e5a0a31c8ba009b50189becd114bc8aaefda2d6e82c96d0f7196d42797f54
|
File details
Details for the file schemamodels-0.9.1-py3-none-any.whl.
File metadata
- Download URL: schemamodels-0.9.1-py3-none-any.whl
- Upload date:
- Size: 32.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/6.5.0-18-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2f675fb538d8e3f90c91b1b1b0e2fe9914a888fd6c7e3449dfe887d8c581aa42
|
|
| MD5 |
696d1ff4bfe396be577be95c58f41c4b
|
|
| BLAKE2b-256 |
545e5a1cd38db9cbf2f9422214323e16855e5d399746b866a48b93b2fdfa979b
|