GistTree.Com
Entertainment at it's peak. The news is by your side.

Show HN: Apischema – JSON (de)serialization and JSON-schema with Python typing

0


Apischema

Makes your life more uncomplicated by manner of python API.

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

Install

It requires finest Python 3.6+ (and dataclasses legitimate backport for model 3.6 finest)

Why one other library?

This library fulfill the following goals:

  • cease as cease as possible to the in model library (dataclasses, typing, etc.) to be as accessible as possible — as a final consequence enact not want plugins for editor/linter/etc.;
  • be additive and tunable, be ready to work with person have kinds (ORM, etc.) as well to international libraries ones; enact not desire a PR for going through fresh kinds worship bson.ObjectId, steer remote from subclassing;
  • steer remote from dynamic issues worship using string for attribute title.

No identified alternative achieves that.

Elaborate

Truly, Apischema is even adaptable passable to allow give a boost to of “rival” libraries in just a few dozens of line of code (look conversions part)

Instance

from dataclasses import dataclass, discipline
from uuid import UUID, uuid4

from pytest import raises

from apischema import ValidationError, deserialize, serialize

# Sing a schema with identical outdated dataclasses
from apischema.json_schema import deserialization_schema


@dataclass
class Resource: 
    id:  UUID
    title:  str
    tags:  region[str] = discipline(default_factory=region)


# Salvage some recordsdata
uuid = uuid4()
recordsdata = {"id":  str(uuid), "title":  "wyfo", "tags":  ["some_tag"]}
# Deserialize recordsdata
resource = deserialize(Resource, recordsdata)
converse resource == Resource(uuid, "wyfo", {"some_tag"})
# Serialize objects
converse serialize(resource) == recordsdata
# Validate all the diagram through deserialization
with raises(ValidationError) as err:   # pytest take a look at exception is raised
    deserialize(Resource, {"id":  "42", "title":  "wyfo"})
converse serialize(err.imprint) == [  # ValidationError is serializable
    {"loc": ["id"], "err":  ["badly formed hexadecimal UUID string"]}
]
# Generate JSON Schema
converse deserialization_schema(Resource) == {
    "$schema":  "http://json-schema.org/draft/2019-09/schema#",
    "form":  "object",
    "properties":  {
        "id":  {"form":  "string", "structure":  "uuid"},
        "title":  {"form":  "string"},
        "tags":  {"form":  "array", "items":  {"form":  "string"}, "uniqueItems":  Factual},
    },
    "required":  ["id", "name"],
    "additionalProperties":  Spurious,
}

Apischema works out of the box with you recordsdata mannequin.

Elaborate

This situation and additional ones are using pytest stuff on yarn of they are if truth be told breeze as tests within the library CI

FAQ

I already have my recordsdata mannequin with my SQLAlchemy/ORM tables, will I would prefer to duplicate my code, making one dataclass by desk?

Why would you will want to duplicate them? Apischema can “work with person have kinds as well to international libraries ones”. Some teasing of conversion impartial: it is possible you’ll perhaps perhaps presumably also add default serialization to your total tables, or register diverse serializer that it is possible you’ll perhaps perhaps presumably also elevate out in response to your API endpoint, or both.

So SQLAlchemy is supported? Does it give a boost to others library?

No, if truth be told, no library are supported, even SQLAlchemy; it used to be a alternative made to be as little and generic as possible, and to present a boost to finest the in model library (with kinds worship datetime, UUID). Then all every other time, the library is versatile passable to code yourself the give a boost to you will want with, I am hoping, the minimal effort. Or not it is no doubt not excluded so as to add give a boost to in extra little plugin libraries. Feedbacks are welcome referring to the finest manner to enact issues.

I would prefer more steady validation than “be definite here is an integer and no a string “, can I enact that?

Witness the validation part. You presumably may maybe perhaps perhaps even employ identical outdated JSON schema validation (maxItems, pattern, etc.) that can be embedded to your schema or add custom-made Python validators for every class/fields/NewType you will want.

Let’s originate the Apischema tour.

Read More

Leave A Reply

Your email address will not be published.