Dataclass with data validation.
Project description
ValidatedDC
Dataclass with data validation. Checks the value of its fields by their annotations.
Capabilities
ValidatedDC
is a regular Python dataclass, but with the ability to check the validity of the data by which this dataclass was initialized. Also, you can check the data at any time during the life of the instance.
- Support for standard and custom Python classes.
- Support for some aliases from the
typing
module, namely:Any
,List
,Literal
,Optional
,Union
. - Replacing the
dict
with an instance of thedataclass
from the field annotation (for example, when receiving data by api). If this behavior is not necessary, then the replacement can be disabled as follows:
@dataclass
class CustomValidatedDC(ValidatedDC):
def _init_validation(self) -> None:
super()._init_validation()
self._replace = False
See detailed examples in examples.py
.
Installation
pip install validated-dc
Simple example
from validated_dc import ValidatedDC
from dataclasses import dataclass
from typing import List, Union
@dataclass
class Foo(ValidatedDC):
foo: int
@dataclass
class Bar(ValidatedDC):
bar: Union[Foo, List[Foo]]
foo = {'foo': 1}
instance = Bar(bar=foo)
print(instance.get_errors()) # None
print(instance) # Bar(bar=Foo(foo=1))
list_foo = [{'foo': 1}, {'foo': 2}]
instance = Bar(bar=list_foo)
print(instance.get_errors()) # None
print(instance) # Bar(bar=[Foo(foo=1), Foo(foo=2)])
instance.bar.append({'foo': '3'})
print(instance.is_valid()) # False
print(instance.get_errors())
# {'bar': {'VALUE': [Foo(foo=1), Foo(foo=2), {'foo': '3'}], 'TYPE':
# typing.Union[__main__.Foo, typing.List[__main__.Foo]], 'ERRORS':
# [([Foo(foo=1), Foo(foo=2), {'foo': '3'}], <class '__main__.Foo'>),
# {<class '__main__.Foo'>: {'foo': {'VALUE': '3', 'TYPE': <class 'int'>,
# 'ERRORS': [('3', <class 'int'>)]}}}, ([Foo(foo=1), Foo(foo=2),
# {'foo': '3'}], typing.List[__main__.Foo]), ([Foo(foo=1), Foo(foo=2),
# {'foo': '3'}], typing.Union[__main__.Foo, typing.List[__main__.Foo]])]}}
print(instance) # Bar(bar=[Foo(foo=1), Foo(foo=2), {'foo': '3'}])
instance.bar[2]['foo'] = 3
print(instance) # Bar(bar=[Foo(foo=1), Foo(foo=2), {'foo': 3}])
print(instance.is_valid()) # True
print(instance.get_errors()) # None
print(instance) # Bar(bar=[Foo(foo=1), Foo(foo=2), Foo(foo=3)]
instance.bar[2].foo = '3'
print(instance) # Bar(bar=[Foo(foo=1), Foo(foo=2), Foo(foo='3')])
print(instance.is_valid()) # False
print(instance.get_errors())
# {'bar': {'VALUE': [Foo(foo=1), Foo(foo=2), Foo(foo='3')], 'TYPE':
# typing.Union[__main__.Foo, typing.List[__main__.Foo]], 'ERRORS':
# [([Foo(foo=1), Foo(foo=2), Foo(foo='3')], <class '__main__.Foo'>),
# {<class '__main__.Foo'>: {'foo': {'VALUE': '3', 'TYPE': <class 'int'>,
# 'ERRORS': [('3', <class 'int'>)]}}}, ([Foo(foo=1), Foo(foo=2),
# Foo(foo='3')], typing.List[__main__.Foo]), ([Foo(foo=1), Foo(foo=2),
# Foo(foo='3')], typing.Union[__main__.Foo, typing.List[__main__.Foo]])]}}
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
validated-dc-0.2.2.tar.gz
(6.4 kB
view hashes)
Built Distribution
Close
Hashes for validated_dc-0.2.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9e4500d6b2d1097ea3d385f16b828d3436b6fe1fe39ada6ad47755139c66f94 |
|
MD5 | c87a3c4e156f750547ae49b467c7ec07 |
|
BLAKE2b-256 | 7622ff1de4a06570d14684b2338e1df169ea8a0d01354ce65f6d9ed78a777e66 |