summaryrefslogtreecommitdiff
path: root/kvmd/validators/__init__.py
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2020-09-08 11:35:29 +0300
committerDevaev Maxim <[email protected]>2020-09-08 11:35:29 +0300
commit5ba0873c32be21fdff822dcac47095d944a29cd3 (patch)
tree8c423b5bcbbab9f4722f1b4992246dd8a881375b /kvmd/validators/__init__.py
parentcf5114264b863a7e966a38c0448cf6e5c1214507 (diff)
stronger validators
Diffstat (limited to 'kvmd/validators/__init__.py')
-rw-r--r--kvmd/validators/__init__.py11
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: