Convention over configuration Object Schemas for python
Project description
Schemey - Json Schemas for Python.
This project allows for generation of json schema objects based on python dataclasses, along with customization of said schemas. Schemas may then be used to validate object graphs.
The general idea is that the framework should not insist on any particular data structure or paradigm - it is designed to be extensible, and out of the box support is provided for iterable types, dataclasses, enums, timestamps and primitives.
Serialization is provided using marshy.
Current test coverage is at 100%
Why did you build this?
There were undocumented gaps in the functionality of existing solutions that made using them untenable.
Installation
pip install schemey
Examples
Hello World
This demonstrates generating a standard schema for a dataclass.
JSON Conversion
This demonstrates dumping a json schema for a dataclass
Deserializing json schemas is currently minimally supported. (Due to the complexity of extracting references from anywhere in the document and the evolving nature of the spec). The intended workflow is to start with a Python dataclass and then convert to a json schema rather than the other way around.
JSON Conversion
Self referencing data structures are supported out of the box.
Adding Custom Schemas for Dataclass Fields
Add a schema to the schemey
attribute of a metadata field for a dataclass to specify a custom schema
The following schemas are defined out of the box (Feel free to add your own!):
- AnyOfSchema: For polymorphic constraints
- ArraySchema: For arrays
- BooleanSchema: For boolean values
- ConstSchema: For constant values
- DeferredSchema: Deferred schema - (used for self referential schemas)
- EnumSchema: For enums
- IntegerSchema: For integer validation
- NullSchema: For null validation
- NumberSchema: For float validation
- NullSchema: For None / null values
- ObjectSchema: For objects
- OptionalSchema: For optional values
- StringSchema: For string validation. (Including regex, format and length constraints)
- TupleSchema: For tuple validation.
Architectural Concepts.
- A Schema is used to validate instances of a type
- A Factory is used to create schemas for python types
- A Loader is used to generate schemas from raw json. (An alternative to factories)
- A Context coordinates the operations between schemas and factories (Using the default context leads to a shorter syntax, but less flexibility)
Specifying a Schema for a Class
Below we outline the process of completely customizing schema generation and marshalling. The simplest way to specify a schema for a class is to define the schema_factory class property. For example, imagine a situation where a 3D point is defined in javascript as an array of 3 numbers, [x, y, z].
You write a dataclass to describe this in python, with a custom marshaller (As per the marshy documentation), but the marshalled schema no longer matches the marshalled dataclass. You will need to define a custom schema and marshaller for the class too.
Specify a Schema for a Class by factory
Instead of overriding the schema_factory / marshaller_factory
methods, it is possible to register a factory for your schema with
your schema context using the register_schema
method.
It is also possible to register implementations for abstract classes / duck typing via the
Building The Project
You need an account on pypi before this will work:
pip install setuptools wheel
python setup.py sdist bdist_wheel
pip install twine
python -m twine upload dist/*
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.