diff options
author | Devaev Maxim <[email protected]> | 2019-04-10 21:40:34 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2019-04-10 21:40:34 +0300 |
commit | 07c59485fcef43a8a3dc122d05c7352a3da8aa15 (patch) | |
tree | ed03ed4c9a314f1be301f90cc5bba64eff4ecbc7 /kvmd/validators/basic.py | |
parent | 4eb89c9399d3bd440577d275a9c07578c2f47484 (diff) |
moar validators
Diffstat (limited to 'kvmd/validators/basic.py')
-rw-r--r-- | kvmd/validators/basic.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/kvmd/validators/basic.py b/kvmd/validators/basic.py index e4a2336e..62f9b36d 100644 --- a/kvmd/validators/basic.py +++ b/kvmd/validators/basic.py @@ -20,7 +20,12 @@ # ========================================================================== # +import re + +from typing import List from typing import Type +from typing import Callable +from typing import Optional from typing import Union from typing import Any @@ -71,3 +76,27 @@ def valid_int_f1(arg: Any) -> int: def valid_float_f01(arg: Any) -> float: return float(valid_number(arg, min=0.1, type=float)) + + +def valid_string_list( + arg: Any, + delim: str=r"[,\t ]+", + subval: Optional[Callable[[Any], Any]]=None, + name: str="", +) -> List[str]: + + if not name: + name = "string list" + + if subval is None: + subval = (lambda item: check_not_none_string(item, name + " item")) + + if not isinstance(arg, (list, tuple)): + arg = check_not_none_string(arg, name) + arg = list(filter(None, re.split(delim, arg))) + if subval is not None: + try: + arg = list(map(subval, arg)) + except Exception: + raise ValidatorError("Failed sub-validator on one of the item of %r" % (arg)) + return arg |