Enable max_length and min_length constraints in lists

Change-Id: Ieb7135c2f89c4217d97318d188b2ae7c23ce9122
Related-Bug: #1869350
This commit is contained in:
Miguel Caballer 2020-03-27 12:10:23 +01:00
parent 1abd37695c
commit c41495fc26
2 changed files with 20 additions and 4 deletions

View File

@ -503,7 +503,7 @@ class MinLength(Constraint):
valid_types = (int, )
valid_prop_types = (Schema.STRING, Schema.MAP)
valid_prop_types = (Schema.STRING, Schema.MAP, Schema.LIST)
def __init__(self, property_name, property_type, constraint):
super(MinLength, self).__init__(property_name, property_type,
@ -514,7 +514,7 @@ class MinLength(Constraint):
'expects an integer.')))
def _is_valid(self, value):
if ((isinstance(value, str) or isinstance(value, dict)) and
if (isinstance(value, (str, dict, list)) and
len(value) >= self.constraint_value):
return True
@ -538,7 +538,7 @@ class MaxLength(Constraint):
valid_types = (int, )
valid_prop_types = (Schema.STRING, Schema.MAP)
valid_prop_types = (Schema.STRING, Schema.MAP, Schema.LIST)
def __init__(self, property_name, property_type, constraint):
super(MaxLength, self).__init__(property_name, property_type,
@ -549,7 +549,7 @@ class MaxLength(Constraint):
'expects an integer.')))
def _is_valid(self, value):
if ((isinstance(value, str) or isinstance(value, dict)) and
if (isinstance(value, (str, dict, list)) and
len(value) <= self.constraint_value):
return True

View File

@ -371,3 +371,19 @@ class ConstraintTest(TestCase):
constraint.validate({"k": "v"})
except Exception as ex:
self.fail(ex)
def test_min_length_with_list(self):
schema = {'min_length': 1}
constraint = Constraint('prop', Schema.LIST, schema)
try:
constraint.validate(["1", "2"])
except Exception as ex:
self.fail(ex)
def test_max_length_with_list(self):
schema = {'max_length': 2}
constraint = Constraint('prop', Schema.LIST, schema)
try:
constraint.validate(["1", "2"])
except Exception as ex:
self.fail(ex)