diff options
Diffstat (limited to 'kvmd/validators/__init__.py')
-rw-r--r-- | kvmd/validators/__init__.py | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/kvmd/validators/__init__.py b/kvmd/validators/__init__.py index 9a372002..d6caa835 100644 --- a/kvmd/validators/__init__.py +++ b/kvmd/validators/__init__.py @@ -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 @@ -37,6 +38,10 @@ class ValidatorError(ValueError): # ===== +_RetvalSeqT = TypeVar("_RetvalSeqT", bound=Sequence) + + +# ===== def raise_error(arg: Any, name: str, hide: bool=False) -> NoReturn: arg_str = " " if not hide: @@ -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: |