mdrachuk / serious

Dataclass Model Toolkit
https://serious.readthedocs.io
MIT License
7 stars 1 forks source link
dataclasses forhumans python serialization validation

serious

PyPI Build Status Test Coverage Supported Python Documentation

A dataclass model toolkit: serialization, validation, and more.

Documentation

Features

Basics

Installation

Available from PyPI:

pip install serious

Quick Example

Central part of Serious API are different Models.

Given a regular dataclass:

from dataclasses import dataclass

@dataclass
class Person:
    name: str

Let’s create a JsonModel:

from serious.json import JsonModel

model = JsonModel(Person)

And use its dump/load methods:

person = Person('Albert Einstein')

model.dump(person) # {"name": "Albert Einstein"}

Validation

To add validation to the example above all we need is to add __validate__ method to person:

from dataclasses import dataclass
from typing import Optional
from serious import ValidationError, Email

@dataclass
class Person:
    name: str
    email: Optional[Email]
    phone: Optional[str]

    def __validate__(self):
        if len(self.name) == 0:
            raise ValidationError('Every person needs a name')
        if self.phone is None and self.email is None:
            raise ValidationError('At least some contact should be present')

More on validation.

Supported formats:

Supported field types

More in docs.

A bigger example

from dataclasses import dataclass
from serious import JsonModel, ValidationError
from typing import List
from enum import Enum

class Specialty(Enum):
    Worker = 1
    Fool = 2

@dataclass(frozen=True)
class Minion:
    name: str
    type: Specialty

@dataclass(frozen=True)
class Boss:
    name: str
    minions: List[Minion]

    def __validate__(self):
        if len(self.minions) < 2:
            raise ValidationError('What kind of boss are you?')

boss = Boss("me", [Minion('evil minion', Specialty.Fool), Minion('very evil minion', Specialty.Worker)])
boss_json = """{
    "name": "me",
    "minions": [
        {
            "name": "evil minion",
            "type": "Fool"
        },
        {
            "name": "very evil minion",
            "type": "Worker"
        }
    ]
}"""

model = JsonModel(Boss, indent=4)

assert model.dump(boss) == boss_json
assert model.load(boss_json) == boss

Acknowledgements

Initially, a fork of @lidatong/dataclasses-json.