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.
- Support for standard types and custom Python classes.
- Support for some aliases from the
typingmodule, namely:Any,List,Literal,Optional,Union. These aliases can be embedded in each other. - When initializing an instance of a class, you can use the value of the field
dictinstead of theValidatedDCinstance specified in the field annotation (useful, for example, when retrieving data via api). - Data validation occurs immediately after an instance is created, and can also be run by the
is_valid()function at any time. - The
get_errors()function will show the full traceback of errors in the fields, including errors of nested classes.
See detailed in the examples folder.
Installation
pip install validated-dc
Python versions support
Versions 3.7, 3.8 and 3.9 are currently supported.
To work with version python 3.7 you need to install:
pip install typing_extensions
Quick example
from dataclasses import dataclass
from typing import List, Union
from validated_dc import ValidatedDC, get_errors, is_valid
# Some combinations of List and Union
@dataclass
class Foo(ValidatedDC):
value: Union[int, List[int]]
@dataclass
class Bar(ValidatedDC):
foo: Union[Foo, List[Foo]]
# --- Valid input ---
foo = {'value': 1}
instance = Bar(foo=foo)
assert get_errors(instance) is None
assert instance == Bar(foo=Foo(value=1))
foo = {'value': [1, 2]}
instance = Bar(foo=foo)
assert get_errors(instance) is None
assert instance == Bar(foo=Foo(value=[1, 2]))
foo = [{'value': 1}, {'value': 2}]
instance = Bar(foo=foo)
assert get_errors(instance) is None
assert instance == Bar(foo=[Foo(value=1), Foo(value=2)])
foo = [{'value': [1, 2]}, {'value': [3, 4]}]
instance = Bar(foo=foo)
assert get_errors(instance) is None
assert instance == Bar(foo=[Foo(value=[1, 2]), Foo(value=[3, 4])])
# --- Invalid input ---
foo = {'value': 'S'}
instance = Bar(foo=foo)
assert get_errors(instance)
assert instance == Bar(foo={'value': 'S'})
# fix
instance.foo['value'] = 1
assert is_valid(instance)
assert get_errors(instance) is None
assert instance == Bar(foo=Foo(value=1))
foo = [{'value': [1, 2]}, {'value': ['S', 4]}]
instance = Bar(foo=foo)
assert get_errors(instance)
assert instance == Bar(foo=[{'value': [1, 2]}, {'value': ['S', 4]}])
# fix
instance.foo[1]['value'][0] = 3
assert is_valid(instance)
assert get_errors(instance) is None
assert instance == Bar(foo=[Foo(value=[1, 2]), Foo(value=[3, 4])])
# --- get_errors() ---
foo = {'value': 'S'}
instance = Bar(foo=foo)
print(get_errors(instance))
# {
# 'foo': [
# # An unsuccessful attempt to use the dictionary to create a Foo instance
# InstanceValidationError(
# value_repr="{'value': 'S'}",
# value_type=<class 'dict'>,
# annotation=<class '__main__.Foo'>,
# exception=None,
# errors={
# 'value': [
# BasicValidationError( # because the str isn't an int
# value_repr='S', value_type=<class 'str'>,
# annotation=<class 'int'>, exception=None
# ),
# BasicValidationError( # and the str is not a list of int
# value_repr='S', value_type=<class 'str'>,
# annotation=typing.List[int], exception=None
# )
# ]
# }
# ),
# BasicValidationError( # the dict is not a list of Foo
# value_repr="{'value': 'S'}",
# value_type=<class 'dict'>,
# annotation=typing.List[__main__.Foo],
# exception=None
# )
# ]
# }
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-1.3.3.tar.gz
(7.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file validated-dc-1.3.3.tar.gz.
File metadata
- Download URL: validated-dc-1.3.3.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4a2e018b5d42a641e48addc9d8bd2804d38046a62ba112eca7c03cae98f2335
|
|
| MD5 |
2ad895ab4b1a25ffd242bf3ccb42e614
|
|
| BLAKE2b-256 |
36b9509310a15ac9c1e6bca9bd59a62ff924a3a2cd296885b37cf7c2c2fe0cdf
|
File details
Details for the file validated_dc-1.3.3-py3-none-any.whl.
File metadata
- Download URL: validated_dc-1.3.3-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f4c849c23cdf6a21b95828da141f0a703fadd7e3f5252857a4b84b9b8196c21
|
|
| MD5 |
5e4ec74c574d5dd1a3ed3e306055b5e3
|
|
| BLAKE2b-256 |
325c3c1ccdda94efd64cb3c3c0e1d4ccebab5848f64f12f3bb150b571ab55df3
|