Decorator for validating function arguments and result.
Project description
valdec
Decorator for validating arguments and/or result of functions and methods of a class against annotations.
Can be used in synchronous or asynchronous version.
Installation
pip install valdec
Quick example
Default
Based on the validator pydantic.BaseModel
:
pip install pydantic
from typing import List
from pydantic import BaseModel, StrictInt, StrictStr
from valdec.decorators import validate
@validate # all with annotations and return
def func(i: StrictInt, s: StrictStr) -> StrictInt:
return i
assert func(1, "s") == 1
@validate("s") # only "s"
def func(i: StrictInt, s: StrictStr) -> StrictInt:
return i
assert func("not int", "s") == "not int"
@validate("s", "return") # only "s" and return
def func(i: StrictInt, s: StrictStr) -> StrictInt:
return int(i)
assert func("1", "s") == 1
@validate("i", exclude=True) # only "s" and return
def func(i: StrictInt, s: StrictStr) -> StrictInt:
return int(i)
assert func("1", "s") == 1
class Profile(BaseModel):
age: StrictInt
city: StrictStr
class Student(BaseModel):
name: StrictStr
profile: Profile
@validate("group") # only "group"
def func(i: StrictInt, s: StrictStr, group: List[Student]):
return group
group = [
{"name": "Peter", "profile": {"age": 22, "city": "Samara"}},
{"name": "Elena", "profile": {"age": 20, "city": "Kazan"}},
]
result = func("any type", "any type", group)
for i, student in enumerate(result):
assert student.name == group[i]["name"]
assert student.profile.age == group[i]["profile"]["age"]
assert student.profile.city == group[i]["profile"]["city"]
validated-dc
Based on the validator validated_dc.ValidatedDC
:
pip install validated-dc
from dataclasses import dataclass
from typing import List
from valdec.data_classes import Settings
from valdec.decorators import validate as _validate
from valdec.validators import validated_dc_validator
from validated_dc import ValidatedDC
custom_settings = Settings(
validator=validated_dc_validator,
)
def validate(*args, **kwargs):
kwargs["settings"] = custom_settings
return _validate(*args, **kwargs)
@validate # all with annotations and return
def func(i: int, s: str) -> int:
return i
assert func(1, "s") == 1
@validate("s") # only "s"
def func(i: int, s: str) -> int:
return i
assert func("not int", "s") == "not int"
@validate("s", "return") # only "s" and return
def func(i: int, s: str) -> int:
return int(i)
assert func("1", "s") == 1
@validate("i", exclude=True) # only "s" and return
def func(i: int, s: str) -> int:
return int(i)
assert func("1", "s") == 1
@dataclass
class Profile(ValidatedDC):
age: int
city: str
@dataclass
class Student(ValidatedDC):
name: str
profile: Profile
@validate("group") # only "group"
def func(i: int, s: str, group: List[Student]):
return group
group = [
{"name": "Peter", "profile": {"age": 22, "city": "Samara"}},
{"name": "Elena", "profile": {"age": 20, "city": "Kazan"}},
]
result = func("any type", "any type", group)
for i, student in enumerate(result):
assert student.name == group[i]["name"]
assert student.profile.age == group[i]["profile"]["age"]
assert student.profile.city == group[i]["profile"]["city"]
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
valdec-1.0.1.tar.gz
(9.0 kB
view hashes)
Built Distribution
valdec-1.0.1-py3-none-any.whl
(9.8 kB
view hashes)