Skip to main content

JSON (de)serialization, GraphQL and JSON schema generation using Python typing.

Project description

apischema

JSON (de)serialization, GraphQL and JSON schema generation using Python typing.

apischema makes your life easier when dealing with API data.

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)

PyPy3 is fully supported.

Why another library?

(If you wonder how this differs from the pydantic library, see the dedicated section of the documentation — there are many differences.)

This library fulfills the following goals:

  • stay as close as possible to the standard library (dataclasses, typing, etc.) — as a consequence we do not need plugins for editors/linters/etc.;
  • be adaptable, provide tools to support any types (ORM, etc.);
  • avoid dynamic things like using raw strings for attributes name — play nicely with your IDE.

No known alternative achieves all of this, and apischema is also faster than all of them.

On top of that, because APIs are not only JSON, apischema is also a complete GraphQL library.

Example

from collections.abc import Collection
from dataclasses import dataclass, field
from uuid import UUID, uuid4

from graphql import print_schema
from pytest import raises

from apischema import ValidationError, deserialize, serialize
from apischema.graphql import graphql_schema
from apischema.json_schema import deserialization_schema


# 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, resource) == data
# Validate during deserialization
with raises(ValidationError) as err:  # pytest checks exception is raised
    deserialize(Resource, {"id": "42", "name": "wyfo"})
assert err.value.errors == [
    {"loc": ["id"], "msg": "badly formed hexadecimal UUID string"}
]
# Generate JSON Schema
assert deserialization_schema(Resource) == {
    "$schema": "http://json-schema.org/draft/2020-12/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,
}


# Define GraphQL operations
def resources(tags: Collection[str] | None = None) -> Collection[Resource] | None:
    ...


# Generate GraphQL schema
schema = graphql_schema(query=[resources], id_types={UUID})
schema_str = """\
type Query {
  resources(tags: [String!]): [Resource!]
}

type Resource {
  id: ID!
  name: String!
  tags: [String!]!
}
"""
assert print_schema(schema) == schema_str

apischema works out of the box with your data model.

Let's start the apischema tour.

Changelog

See releases

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.17.2.tar.gz (510.2 kB view hashes)

Uploaded Source

Built Distributions

apischema-0.17.2-cp310-cp310-win_amd64.whl (110.5 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

apischema-0.17.2-cp310-cp310-win32.whl (110.4 kB view hashes)

Uploaded CPython 3.10 Windows x86

apischema-0.17.2-cp310-cp310-musllinux_1_1_x86_64.whl (3.8 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

apischema-0.17.2-cp310-cp310-musllinux_1_1_i686.whl (3.6 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

apischema-0.17.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.7 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

apischema-0.17.2-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (3.5 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

apischema-0.17.2-cp310-cp310-macosx_10_9_x86_64.whl (620.7 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

apischema-0.17.2-cp39-cp39-win_amd64.whl (110.5 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

apischema-0.17.2-cp39-cp39-win32.whl (110.4 kB view hashes)

Uploaded CPython 3.9 Windows x86

apischema-0.17.2-cp39-cp39-musllinux_1_1_x86_64.whl (3.8 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

apischema-0.17.2-cp39-cp39-musllinux_1_1_i686.whl (3.5 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

apischema-0.17.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.7 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

apischema-0.17.2-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (3.5 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

apischema-0.17.2-cp39-cp39-macosx_10_9_x86_64.whl (620.5 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

apischema-0.17.2-cp38-cp38-win_amd64.whl (110.5 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

apischema-0.17.2-cp38-cp38-win32.whl (110.4 kB view hashes)

Uploaded CPython 3.8 Windows x86

apischema-0.17.2-cp38-cp38-musllinux_1_1_x86_64.whl (4.0 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

apischema-0.17.2-cp38-cp38-musllinux_1_1_i686.whl (3.8 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

apischema-0.17.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.8 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

apischema-0.17.2-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (3.6 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

apischema-0.17.2-cp38-cp38-macosx_10_9_x86_64.whl (611.4 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

apischema-0.17.2-cp37-cp37m-win_amd64.whl (110.5 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

apischema-0.17.2-cp37-cp37m-win32.whl (110.5 kB view hashes)

Uploaded CPython 3.7m Windows x86

apischema-0.17.2-cp37-cp37m-musllinux_1_1_x86_64.whl (3.3 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

apischema-0.17.2-cp37-cp37m-musllinux_1_1_i686.whl (3.1 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ i686

apischema-0.17.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.3 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

apischema-0.17.2-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (2.9 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

apischema-0.17.2-cp37-cp37m-macosx_10_9_x86_64.whl (587.0 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

apischema-0.17.2-cp36-cp36m-win_amd64.whl (110.5 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

apischema-0.17.2-cp36-cp36m-win32.whl (110.5 kB view hashes)

Uploaded CPython 3.6m Windows x86

apischema-0.17.2-cp36-cp36m-musllinux_1_1_x86_64.whl (3.3 MB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ x86-64

apischema-0.17.2-cp36-cp36m-musllinux_1_1_i686.whl (3.1 MB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ i686

apischema-0.17.2-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.3 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

apischema-0.17.2-cp36-cp36m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (3.0 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

apischema-0.17.2-cp36-cp36m-macosx_10_9_x86_64.whl (589.4 kB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

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