Tools for generating Python models from JSONSchema definitions.
Project description
Statham Schema
A tool for generating Python classes/models from JSONSchema 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 early.
- Internal validation: Ensure your application's use of external data sources is consistent with the schema. Declared models and static type checking (see mypy) can do this job for you.
- Visibility: Update the model layer against schema changes automatically, and let static tools identify any issues.
The tool will generate attrs dataclasses which can then be used and extended as a model layer for the external data source.
Requirements
This package is currently tested for Python 3.6.
Installation
This project is not currently packaged and so must be installed manually.
Clone the project with the following command:
git clone https://github.com/jacksmith15/statham-schema.git
Package requirements may be installed via pip install -r requirements.txt
. Use of a virtualenv is recommended.
Generating python classes
From the directory of the cloned repository, run
PYTHONPATH=. python 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.
See this example output.
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
- Basic types (primitves, array, object)
- Composite primitive types
- Type validation on generated classes
- Validation of
required
- Local references
- Type-specific validation (pattern, format, minimum, maximum etc)
- Custom string format validation
- Remote references
- Tuple validation of arrays
- Built-in string format validation #6
- Generic keywords:
enum
#8,const
#9 - Array keywords:
uniqueItems
#10,contains
- Composition keywords (
allOf
#12,anyOf
#13,oneOf
#11,not
#14) -
minProperties
,maxProperties
#15 - Property dependencies
- Schema dependencies
-
propertyNames
,patternProperties
,additionalProperties
(This is tricky withattrs
) -
additionalItems
keyword -
if
,then
,else
keywords
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.
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.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 315dea768efe5bf822558c2a7d7240eb9b52aaa069f39daaf07321b2387f52c5 |
|
MD5 | f7ea43944714795dc17ba0ffc4c094a3 |
|
BLAKE2b-256 | f969549b3c235fb4e83fe9f22fe0b90328ff40d657f78221eccb2fa2c9c5ed2f |