Skip to main content

Another Python API schema handling and JSON (de)serialization through typing annotation; light, simple, powerful.

Project description

Apischema

Makes your life easier when it comes to python API.

JSON (de)serialization + schema generation through python typing, with a spoonful of sugar.

Documentation

https://wyfo.github.io/apischema/

Install

pip install apischema

It requires only Python 3.6+ (and dataclasses official backport for version 3.6 only)

Why another library?

This library fulfill the following goals:

  • stay as close as possible to the standard library (dataclasses, typing, etc.) to be as accessible as possible — as a consequence do not need plugins for editor/linter/etc.;
  • be additive and tunable, be able to work with user own types as well as foreign libraries ones; do not need a PR for handling new types like bson.ObjectId;
  • avoid dynamic things like using string for attribute name.

No known alternative achieves that.

Example

from dataclasses import dataclass, field
from typing import List, Set
from uuid import UUID, uuid4

from pytest import raises

from apischema import ValidationError, deserialization_schema, deserialize, serialize


# Define a schema with standard dataclasses
@dataclass
class Resource:
    id: UUID
    name: str
    tags: Set[str] = field(default_factory=set)


# Get some data
uuid = uuid4()
data = {"id": str(uuid), "name": "wyfo", "tags": ["some_tag"]}
# Deserialize data
resource = deserialize(Resource, data)
assert resource == Resource(uuid, "wyfo", {"some_tag"})
# Serialize objects
assert serialize(resource) == data
# Validate during deserialization
with raises(ValidationError) as err:  # pytest check exception is raised
    deserialize(Resource, {"id": "42", "name": "wyfo"})
assert serialize(err.value) == [  # ValidationError is serializable
    {"loc": ["id"], "err": ["badly formed hexadecimal UUID string"]}
]
# Generate JSON Schema
assert deserialization_schema(Resource) == {
    "$schema": "http://json-schema.org/draft/2019-09/schema#",
    "type": "object",
    "properties": {
        "id": {"type": "string", "format": "uuid"},
        "name": {"type": "string"},
        "tags": {"type": "array", "items": {"type": "string"}, "uniqueItems": True},
    },
    "required": ["id", "name"],
    "additionalProperties": False,
}

Apischema works out of the box with your data model.

(This example and further ones are using pytest stuff because they are in fact run as tests in the library CI; that's very convenient)

Let's start the Apischema tour.

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

apischema-0.7.2.tar.gz (46.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

apischema-0.7.2-py3-none-any.whl (59.6 kB view details)

Uploaded Python 3

File details

Details for the file apischema-0.7.2.tar.gz.

File metadata

  • Download URL: apischema-0.7.2.tar.gz
  • Upload date:
  • Size: 46.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for apischema-0.7.2.tar.gz
Algorithm Hash digest
SHA256 3a24a0aab4bdf5abbf9dd610e9a687c6b26f87a708644cef683931d0ce1fbe9f
MD5 afe69400e05b824c9892400286060d43
BLAKE2b-256 6dd43021389cc6565f894842ab47a2d92f172905205f7741c26de288a5b567cd

See more details on using hashes here.

File details

Details for the file apischema-0.7.2-py3-none-any.whl.

File metadata

  • Download URL: apischema-0.7.2-py3-none-any.whl
  • Upload date:
  • Size: 59.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for apischema-0.7.2-py3-none-any.whl
Algorithm Hash digest
SHA256 85d652c9df827e097ab23a4aedddbd942daccaacbd15bc5562edae4ee3ffc943
MD5 4e07ca44e6571414342ff9f46a3536ac
BLAKE2b-256 aa5d790b10a0c1de5242b4bd6bb06a143c36a9c38c7d49bfed39f6eb2b36aebc

See more details on using hashes here.

Supported by

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