Tools for generating Python models from JSON Schema documents.
Project description
Statham Schema
A tool for generating Python objects from JSON Schema documents.
This project aims to simplify the experience of integrating with external sources, by providing:
- External validation: Ensure that incoming data matches what you expect.
- Internal validation: Ensure your application logic is consistent with the schema. Static type checking (see mypy) can do this job for you.
- Extensibility: Extend the generated models with properties and methods useful to you.
- Visibility: Update the model layer against schema changes automatically, and let static tools identify any issues.
The models generated by this tool are declared using a JSONSchema DSL. This DSL can easily be used to write schemas by hand, and extend models with extra features.
Example DSL Definition
from typing import List
from statham.dsl.elements import Array, Integer, Object, String
from statham.dsl.property import Property
class Choice(Object):
choice_text: str = Property(String(maxLength=200), required=True)
votes: int = Property(Integer(default=0))
class Poll(Object):
question: str = Property(String(), required=True)
choices: List[Choice] = Property(Array(Choice), required=True)
Requirements
This package is currently tested for Python 3.6.
Installation
This project may be installed using pip:
pip install statham-schema
Generating python classes
Class definitions may be generated with the following command:
statham --input /path/to/schema.json
This will write generated python classes to stdout. Optionally specify an --output
path to write to file.
Command-line arguments
Required arguments:
--input INPUT Specify the path to the JSON Schema to be generated.
If the target schema is not at the root of a document, specify the
JSON Pointer in the same format as a JSONSchema `$ref`, e.g.
`--input path/to/document.json#/definitions/schema`
Optional arguments:
--output OUTPUT Output directory or file in which to write the output.
If the provided path is a directory, the command will derive the name
from the input argument. If not passed, the command will write to
stdout.
-h, --help Display this help message and exit.
Using custom format keywords
JSONSchema allows use of custom string formats as specified here. Custom validation logic for string format may be added like so:
from statham.validators import format_checker
@format_checker.register("my_format")
def _check_my_format(value: str) -> bool:
...
Supported JSONSchema features
This library is tested to support JSON Schema Draft 6, with the following notable exceptions:
- Recursive references are not supported.
- Location-independent references are not supported.
- Changes of base URI via the
$id
keyword are not detected.
Priority order for remaining features is roughly as follows:
- Support for features of more recent drafts.
- Support for the exceptions listed above.
- Support for features of less recent drafts.
However, contributions in any of these areas are welcome.
Development
- Clone the repository:
git clone git@github.com:jacksmith15/statham-schema.git && cd statham-schema
- Install the requirements:
pip install -r requirements.txt -r requirements-test.txt
- Run
pre-commit install
- Run the tests:
bash run_test.sh -c -a
This project uses the following QA tools:
- PyTest - for running unit tests.
- PyLint - for enforcing code style.
- MyPy - for static type checking.
- Travis CI - for continuous integration.
- Black - for uniform code formatting.
License
This project is distributed under the MIT license.
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 statham_schema-0.9.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 223f21de2a873d996a6eec0cc5e257ee7d67c6289a4cb417a1d80d25beaba1ff |
|
MD5 | 1ebf119d45a8a1caf33f9dc0918d0893 |
|
BLAKE2b-256 | 3156a9ebd7273171a4e946ea5e160c530a83dcbd6a47f9ebac0e33d2bb1ecb74 |