mirror of https://github.com/zulip/zulip.git
Add "length" option to check_list validator.
(imported from commit 4f8e203f964d1c936fe548b2f77a2e4aae745ae9)
This commit is contained in:
parent
b2bffc26f9
commit
6f573feaeb
|
@ -41,11 +41,13 @@ def check_bool(var_name, val):
|
|||
return '%s is not a boolean' % (var_name,)
|
||||
return None
|
||||
|
||||
def check_list(sub_validator):
|
||||
def check_list(sub_validator, length=None):
|
||||
def f(var_name, val):
|
||||
if not isinstance(val, list):
|
||||
return '%s is not a list' % (var_name,)
|
||||
|
||||
if length is not None and length != len(val):
|
||||
return '%s should have exactly %d items' % (var_name, length)
|
||||
for i, item in enumerate(val):
|
||||
vname = '%s[%d]' % (var_name, i)
|
||||
error = sub_validator(vname, item)
|
||||
|
|
|
@ -290,6 +290,10 @@ class ValidatorTestCase(TestCase):
|
|||
error = check_list(check_list(check_string))('x', x)
|
||||
self.assertEqual(error, 'x[1][2] is not a string')
|
||||
|
||||
x = ["hello", "goodbye", "hello again"]
|
||||
error = check_list(check_string, length=2)('x', x)
|
||||
self.assertEqual(error, 'x should have exactly 2 items')
|
||||
|
||||
def test_check_dict(self):
|
||||
keys = [
|
||||
('names', check_list(check_string)),
|
||||
|
|
Loading…
Reference in New Issue