Skip to content

Commit

Permalink
validators: Add check_float validator.
Browse files Browse the repository at this point in the history
  • Loading branch information
timabbott committed Mar 24, 2017
1 parent 12a6913 commit d1760a1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 6 additions & 0 deletions zerver/lib/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ def check_int(var_name, val):
return _('%s is not an integer') % (var_name,)
return None

def check_float(var_name, val):
# type: (str, Any) -> Optional[str]
if not isinstance(val, float):
return _('%s is not a float') % (var_name,)
return None

def check_bool(var_name, val):
# type: (str, Any) -> Optional[str]
if not isinstance(val, bool):
Expand Down
13 changes: 12 additions & 1 deletion zerver/tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
return_success_on_head_request
)
from zerver.lib.validator import (
check_string, check_dict, check_bool, check_int, check_list, Validator,
check_string, check_dict, check_bool, check_float, check_int, check_list, Validator,
check_variable_type, equals, check_none_or,
)
from zerver.models import \
Expand Down Expand Up @@ -418,6 +418,17 @@ def test_check_int(self):
x = [{}]
self.assertEqual(check_int('x', x), 'x is not an integer')

def test_check_float(self):
# type: () -> None
x = 5.5 # type: Any
self.assertEqual(check_float('x', x), None)

x = 5
self.assertEqual(check_float('x', x), 'x is not a float')

x = [{}]
self.assertEqual(check_float('x', x), 'x is not a float')

def test_check_list(self):
# type: () -> None
x = 999 # type: Any
Expand Down

0 comments on commit d1760a1

Please sign in to comment.