Skip to main content

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!):

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

schemey-4.4.0.tar.gz (18.6 kB view hashes)

Uploaded Source

Built Distribution

schemey-4.4.0-py3-none-any.whl (38.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page