mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-12 09:10:30 +08:00
stronger validators
This commit is contained in:
parent
cf5114264b
commit
5ba0873c32
@ -26,6 +26,7 @@ from typing import List
|
|||||||
from typing import Mapping
|
from typing import Mapping
|
||||||
from typing import Sequence
|
from typing import Sequence
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
from typing import TypeVar
|
||||||
from typing import NoReturn
|
from typing import NoReturn
|
||||||
from typing import Union
|
from typing import Union
|
||||||
from typing import Any
|
from typing import Any
|
||||||
@ -36,6 +37,10 @@ class ValidatorError(ValueError):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# =====
|
||||||
|
_RetvalSeqT = TypeVar("_RetvalSeqT", bound=Sequence)
|
||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
def raise_error(arg: Any, name: str, hide: bool=False) -> NoReturn:
|
def raise_error(arg: Any, name: str, hide: bool=False) -> NoReturn:
|
||||||
arg_str = " "
|
arg_str = " "
|
||||||
@ -77,6 +82,12 @@ def check_re_match(arg: Any, name: str, pattern: str, strip: bool=True, hide: bo
|
|||||||
return arg
|
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:
|
def check_any(arg: Any, name: str, validators: List[Callable[[Any], Any]]) -> Any:
|
||||||
for validator in validators:
|
for validator in validators:
|
||||||
try:
|
try:
|
||||||
|
|||||||
@ -24,6 +24,7 @@ from typing import Any
|
|||||||
|
|
||||||
from . import check_in_list
|
from . import check_in_list
|
||||||
from . import check_re_match
|
from . import check_re_match
|
||||||
|
from . import check_len
|
||||||
|
|
||||||
from .basic import valid_number
|
from .basic import valid_number
|
||||||
|
|
||||||
@ -44,7 +45,8 @@ def valid_gpio_pin_optional(arg: Any) -> int:
|
|||||||
|
|
||||||
|
|
||||||
def valid_otg_gadget(arg: Any) -> str:
|
def valid_otg_gadget(arg: Any) -> str:
|
||||||
return check_re_match(arg, "OTG gadget name", r"^[a-z_][a-z0-9_-]*$")[:255]
|
name = "OTG gadget name"
|
||||||
|
return check_len(check_re_match(arg, name, r"^[a-z_][a-z0-9_-]*$"), name, 255)
|
||||||
|
|
||||||
|
|
||||||
def valid_otg_id(arg: Any) -> int:
|
def valid_otg_id(arg: Any) -> int:
|
||||||
|
|||||||
@ -28,6 +28,7 @@ from ..keyboard.mappings import KEYMAP
|
|||||||
from . import raise_error
|
from . import raise_error
|
||||||
from . import check_string_in_list
|
from . import check_string_in_list
|
||||||
from . import check_re_match
|
from . import check_re_match
|
||||||
|
from . import check_len
|
||||||
|
|
||||||
from .basic import valid_stripped_string_not_empty
|
from .basic import valid_stripped_string_not_empty
|
||||||
from .basic import valid_number
|
from .basic import valid_number
|
||||||
@ -92,7 +93,8 @@ def valid_hid_mouse_wheel(arg: Any) -> int:
|
|||||||
|
|
||||||
# =====
|
# =====
|
||||||
def valid_ugpio_driver(arg: Any) -> str:
|
def valid_ugpio_driver(arg: Any) -> str:
|
||||||
return check_re_match(arg, "GPIO driver", r"^[a-zA-Z_][a-zA-Z0-9_-]*$")[:255]
|
name = "GPIO driver"
|
||||||
|
return check_len(check_re_match(arg, name, r"^[a-zA-Z_][a-zA-Z0-9_-]*$"), name, 255)
|
||||||
|
|
||||||
|
|
||||||
def valid_ugpio_mode(arg: Any) -> str:
|
def valid_ugpio_mode(arg: Any) -> str:
|
||||||
@ -100,7 +102,8 @@ def valid_ugpio_mode(arg: Any) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def valid_ugpio_channel(arg: Any) -> str:
|
def valid_ugpio_channel(arg: Any) -> str:
|
||||||
return check_re_match(arg, "GPIO channel", r"^[a-zA-Z_][a-zA-Z0-9_-]*$")[:255]
|
name = "GPIO channel"
|
||||||
|
return check_len(check_re_match(arg, name, r"^[a-zA-Z_][a-zA-Z0-9_-]*$"), name, 255)
|
||||||
|
|
||||||
|
|
||||||
def valid_ugpio_view_table(arg: Any) -> List[List[str]]:
|
def valid_ugpio_view_table(arg: Any) -> List[List[str]]:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user