From 6f573feaeb120c6115d75ee5cdbb7401946277c3 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Wed, 18 Dec 2013 11:59:02 -0500 Subject: [PATCH] Add "length" option to check_list validator. (imported from commit 4f8e203f964d1c936fe548b2f77a2e4aae745ae9) --- zerver/lib/validator.py | 4 +++- zerver/tests.py | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/zerver/lib/validator.py b/zerver/lib/validator.py index 7013649dbb..8312ee2738 100644 --- a/zerver/lib/validator.py +++ b/zerver/lib/validator.py @@ -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) diff --git a/zerver/tests.py b/zerver/tests.py index d88448f8af..7fad4d9646 100644 --- a/zerver/tests.py +++ b/zerver/tests.py @@ -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)),