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
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\'>"]'})
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.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 252903a2b4c84ac9ff9388147221d72629fe2fd829465f6d761e882460888963 |
|
MD5 | 5044cf4bddfe966a8d0e468df7bb4f93 |
|
BLAKE2b-256 | fa4a669d29d392af33c6160b194c80f236437d274b3ee1b86ebe524bb1eaaf27 |
Close
Hashes for dataclass_type_validator-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bb2aefb87be049de7f962cc6e3b6f40541b839c29988912605e76df8399902b |
|
MD5 | e1bd745e535527b38722020bbea26897 |
|
BLAKE2b-256 | b4d226231cec370f2010e23450f4d57c949109ed73e4af53cc31d4dc01027d6a |