mirror of https://github.com/zulip/zulip.git
validator: Add generic check_or.
Added generic check_or function and tests. Fixes part of #17914. Co-authored-by: Gaurav Pandey <gauravguitarrocks@gmail.com>
This commit is contained in:
parent
33b5fa633a
commit
cd93d0967f
|
@ -582,3 +582,15 @@ def check_string_or_int(var_name: str, val: object) -> Union[str, int]:
|
||||||
return val
|
return val
|
||||||
|
|
||||||
raise ValidationError(_("{var_name} is not a string or integer").format(var_name=var_name))
|
raise ValidationError(_("{var_name} is not a string or integer").format(var_name=var_name))
|
||||||
|
|
||||||
|
|
||||||
|
def check_or(sub_validator1: Validator[Any], sub_validator2: Validator[Any]) -> Validator[Any]:
|
||||||
|
def f(var_name: str, val: object) -> Validator[Any]:
|
||||||
|
try:
|
||||||
|
return sub_validator1(var_name, val)
|
||||||
|
except ValidationError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return sub_validator2(var_name, val)
|
||||||
|
|
||||||
|
return f
|
||||||
|
|
|
@ -70,6 +70,7 @@ from zerver.lib.validator import (
|
||||||
check_int_in,
|
check_int_in,
|
||||||
check_list,
|
check_list,
|
||||||
check_none_or,
|
check_none_or,
|
||||||
|
check_or,
|
||||||
check_short_string,
|
check_short_string,
|
||||||
check_string,
|
check_string,
|
||||||
check_string_fixed_length,
|
check_string_fixed_length,
|
||||||
|
@ -991,6 +992,25 @@ class ValidatorTestCase(ZulipTestCase):
|
||||||
with self.assertRaisesRegex(ValidationError, r"x is not a string or integer"):
|
with self.assertRaisesRegex(ValidationError, r"x is not a string or integer"):
|
||||||
check_string_or_int("x", x)
|
check_string_or_int("x", x)
|
||||||
|
|
||||||
|
def test_check_or(self) -> None:
|
||||||
|
x: Any = "valid"
|
||||||
|
check_or(check_string_in(["valid"]), check_url)("x", x)
|
||||||
|
|
||||||
|
x = "http://zulip-bots.example.com/"
|
||||||
|
check_or(check_string_in(["valid"]), check_url)("x", x)
|
||||||
|
|
||||||
|
x = "invalid"
|
||||||
|
with self.assertRaisesRegex(ValidationError, r"x is not a URL"):
|
||||||
|
check_or(check_string_in(["valid"]), check_url)("x", x)
|
||||||
|
|
||||||
|
x = "http://127.0.0"
|
||||||
|
with self.assertRaisesRegex(ValidationError, r"x is not a URL"):
|
||||||
|
check_or(check_string_in(["valid"]), check_url)("x", x)
|
||||||
|
|
||||||
|
x = 1
|
||||||
|
with self.assertRaisesRegex(ValidationError, r"x is not a string"):
|
||||||
|
check_or(check_string_in(["valid"]), check_url)("x", x)
|
||||||
|
|
||||||
|
|
||||||
class DeactivatedRealmTest(ZulipTestCase):
|
class DeactivatedRealmTest(ZulipTestCase):
|
||||||
def test_send_deactivated_realm(self) -> None:
|
def test_send_deactivated_realm(self) -> None:
|
||||||
|
|
Loading…
Reference in New Issue