stronger validators

This commit is contained in:
Devaev Maxim
2020-09-08 11:35:29 +03:00
parent cf5114264b
commit 5ba0873c32
3 changed files with 19 additions and 3 deletions

View File

@@ -26,6 +26,7 @@ from typing import List
from typing import Mapping
from typing import Sequence
from typing import Callable
from typing import TypeVar
from typing import NoReturn
from typing import Union
from typing import Any
@@ -36,6 +37,10 @@ class ValidatorError(ValueError):
pass
# =====
_RetvalSeqT = TypeVar("_RetvalSeqT", bound=Sequence)
# =====
def raise_error(arg: Any, name: str, hide: bool=False) -> NoReturn:
arg_str = " "
@@ -77,6 +82,12 @@ def check_re_match(arg: Any, name: str, pattern: str, strip: bool=True, hide: bo
return arg
def check_len(arg: _RetvalSeqT, name: str, limit: int) -> _RetvalSeqT:
if len(arg) > limit:
raise_error(arg, name)
return arg
def check_any(arg: Any, name: str, validators: List[Callable[[Any], Any]]) -> Any:
for validator in validators:
try: