mirror of
https://github.com/zulip/zulip.git
synced 2025-11-16 03:41:58 +00:00
validators: Add check_int_in validator.
This is a useful helper for our enum format fields where we want to only allow a fixed list of integer values.
This commit is contained in:
@@ -30,7 +30,7 @@ import ujson
|
|||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.core.validators import validate_email, URLValidator
|
from django.core.validators import validate_email, URLValidator
|
||||||
from typing import Iterable, Optional, Tuple, cast
|
from typing import Iterable, Optional, Tuple, cast, List
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from zerver.lib.request import JsonableError
|
from zerver.lib.request import JsonableError
|
||||||
@@ -92,6 +92,17 @@ def check_int(var_name: str, val: object) -> Optional[str]:
|
|||||||
return _('%s is not an integer') % (var_name,)
|
return _('%s is not an integer') % (var_name,)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def check_int_in(possible_values: List[int]) -> Validator:
|
||||||
|
def validator(var_name: str, val: object) -> Optional[str]:
|
||||||
|
not_int = check_int(var_name, val)
|
||||||
|
if not_int is not None:
|
||||||
|
return not_int
|
||||||
|
if val not in possible_values:
|
||||||
|
return _("Invalid %s") % (var_name,)
|
||||||
|
return None
|
||||||
|
|
||||||
|
return validator
|
||||||
|
|
||||||
def check_float(var_name: str, val: object) -> Optional[str]:
|
def check_float(var_name: str, val: object) -> Optional[str]:
|
||||||
if not isinstance(val, float):
|
if not isinstance(val, float):
|
||||||
return _('%s is not a float') % (var_name,)
|
return _('%s is not a float') % (var_name,)
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ from zerver.lib.validator import (
|
|||||||
check_string, check_dict, check_dict_only, check_bool, check_float, check_int, check_list, Validator,
|
check_string, check_dict, check_dict_only, check_bool, check_float, check_int, check_list, Validator,
|
||||||
check_variable_type, equals, check_none_or, check_url, check_short_string,
|
check_variable_type, equals, check_none_or, check_url, check_short_string,
|
||||||
check_string_fixed_length, check_capped_string, check_color, to_non_negative_int,
|
check_string_fixed_length, check_capped_string, check_color, to_non_negative_int,
|
||||||
check_string_or_int_list, check_string_or_int
|
check_string_or_int_list, check_string_or_int, check_int_in
|
||||||
)
|
)
|
||||||
from zerver.models import \
|
from zerver.models import \
|
||||||
get_realm, get_user, UserProfile, Realm
|
get_realm, get_user, UserProfile, Realm
|
||||||
@@ -758,6 +758,11 @@ class ValidatorTestCase(TestCase):
|
|||||||
x = "hi"
|
x = "hi"
|
||||||
self.assertEqual(check_capped_string(5)('x', x), None)
|
self.assertEqual(check_capped_string(5)('x', x), None)
|
||||||
|
|
||||||
|
def test_check_int_in(self) -> None:
|
||||||
|
self.assertEqual(check_int_in([1])("Test", 1), None)
|
||||||
|
self.assertEqual(check_int_in([1])("Test", 2), "Invalid Test")
|
||||||
|
self.assertEqual(check_int_in([1])("Test", "t"), "Test is not an integer")
|
||||||
|
|
||||||
def test_check_short_string(self) -> None:
|
def test_check_short_string(self) -> None:
|
||||||
x = "hello" # type: Any
|
x = "hello" # type: Any
|
||||||
self.assertEqual(check_short_string('x', x), None)
|
self.assertEqual(check_short_string('x', x), None)
|
||||||
|
|||||||
Reference in New Issue
Block a user