From c41495fc26efd25b2f2d548dfe1001d6b7b94004 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Fri, 27 Mar 2020 12:10:23 +0100 Subject: [PATCH] Enable max_length and min_length constraints in lists Change-Id: Ieb7135c2f89c4217d97318d188b2ae7c23ce9122 Related-Bug: #1869350 --- toscaparser/elements/constraints.py | 8 ++++---- toscaparser/tests/test_constraints.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/toscaparser/elements/constraints.py b/toscaparser/elements/constraints.py index b78a6e47..d20c87f6 100644 --- a/toscaparser/elements/constraints.py +++ b/toscaparser/elements/constraints.py @@ -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 diff --git a/toscaparser/tests/test_constraints.py b/toscaparser/tests/test_constraints.py index 07cb910f..c6fdd9d9 100644 --- a/toscaparser/tests/test_constraints.py +++ b/toscaparser/tests/test_constraints.py @@ -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)