Dataclass Type Validator Library
Project description
dataclass-type-validator
The dataclass-type-validator
is a type validation library for the properties of dataclasses.dataclass
using Python type hint information.
Installation
pip install dataclass-type-validator
or add dataclass-type-validator
line to requirements.txt
A Simple Example
Explicitly calling dataclass_type_validator from within your dataclass
from dataclasses import dataclass
from typing import List
from dataclass_type_validator import dataclass_type_validator
from dataclass_type_validator import TypeValidationError
@dataclass()
class User:
id: int
name: str
friend_ids: List[int]
def __post_init__(self):
dataclass_type_validator(self)
# Valid User
User(id=10, name='John Smith', friend_ids=[1, 2])
# => User(id=10, name='John Smith', friend_ids=[1, 2])
# Invalid User
try:
User(id='a', name=['John', 'Smith'], friend_ids=['a'])
except TypeValidationError as e:
print(e)
# => TypeValidationError: Dataclass Type Validation (errors = {
# 'id': "must be an instance of <class 'int'>, but received <class 'str'>",
# 'name': "must be an instance of <class 'str'>, but received <class 'list'>",
# 'friend_ids': 'must be an instance of typing.List[int], but there are some errors:
# ["must be an instance of <class \'int\'>, but received <class \'str\'>"]'})
The same, but using the class decorator instead
from dataclasses import dataclass
from typing import List
from dataclass_type_validator import dataclass_validate
from dataclass_type_validator import TypeValidationError
@dataclass_validate
@dataclass()
class User:
id: int
name: str
friend_ids: List[int]
# Valid User
User(id=10, name='John Smith', friend_ids=[1, 2])
# => User(id=10, name='John Smith', friend_ids=[1, 2])
# Invalid User
try:
User(id='a', name=['John', 'Smith'], friend_ids=['a'])
except TypeValidationError as e:
print(e)
# => TypeValidationError: Dataclass Type Validation (errors = {
# 'id': "must be an instance of <class 'int'>, but received <class 'str'>",
# 'name': "must be an instance of <class 'str'>, but received <class 'list'>",
# 'friend_ids': 'must be an instance of typing.List[int], but there are some errors:
# ["must be an instance of <class \'int\'>, but received <class \'str\'>"]'})
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
Built Distribution
Close
Hashes for dataclass-type-validator-0.0.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 19563112a3a99beb0e1406ec88b890e7aa999649a2151d902508a766f282656b |
|
MD5 | af1e5ea72e21fd087b548c468d89b9e1 |
|
BLAKE2b-256 | f65f34eef56313e6c9a3169c614235597ab7085b93f36d3e222acd12aafbbb51 |
Close
Hashes for dataclass_type_validator-0.0.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5be506f2c119274ca511716be2623f1a5fdfebf7d57fdfe28f6eaf1e68ac58e7 |
|
MD5 | 020804ee2ac8d29d210da23431c0f8ac |
|
BLAKE2b-256 | 3707aa1c9f77979aa507ea6f7891af4552de4619cd54161b94950e25f946e943 |