Skip to content

Commit c817aed

Browse files
authored
Fix validating min/maxLength with array types (#3805)
1 parent 88efb08 commit c817aed

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/cfnlint/rules/resources/properties/StringLength.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import regex as re
1111

12-
from cfnlint.helpers import FUNCTIONS
12+
from cfnlint.helpers import FUNCTIONS, ensure_list, is_function
1313
from cfnlint.jsonschema import ValidationError
1414
from cfnlint.rules import CloudFormationLintRule
1515

@@ -80,8 +80,8 @@ def maxLength(self, validator, mL, instance, schema):
8080
return
8181
# there are scenarios where Fn::Sub may not predictable so use
8282
# best judgement
83-
if validator.is_type(instance, "object") and len(instance) == 1:
84-
key = list(instance.keys())[0]
83+
key, value = is_function(instance)
84+
if key is not None:
8585
if key == "Fn::Sub":
8686
value = instance[key]
8787
if isinstance(value, str):
@@ -93,7 +93,7 @@ def maxLength(self, validator, mL, instance, schema):
9393
validator, mL, self._fix_sub_string(value[0]), schema
9494
)
9595
return
96-
if schema.get("type") == "object":
96+
if "object" in ensure_list(schema.get("type")):
9797
yield from self._non_string_max_length(instance, mL)
9898

9999
# pylint: disable=unused-argument, arguments-renamed
@@ -105,8 +105,8 @@ def minLength(self, validator, mL, instance, schema):
105105

106106
# there are scenarios where Fn::Sub may not predictable so use
107107
# best judgement
108-
if validator.is_type(instance, "object") and len(instance) == 1:
109-
key = list(instance.keys())[0]
108+
key, value = is_function(instance)
109+
if key is not None:
110110
if key == "Fn::Sub":
111111
value = instance[key]
112112
if isinstance(value, str):
@@ -118,5 +118,6 @@ def minLength(self, validator, mL, instance, schema):
118118
validator, mL, self._fix_sub_string(value[0]), schema
119119
)
120120
return
121-
if schema.get("type") == "object":
121+
122+
if "object" in ensure_list(schema.get("type")):
122123
yield from self._non_string_min_length(instance, mL)

test/unit/rules/resources/properties/test_string_length.py

+12
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ def rule():
3737
({"foo": 1, "bar": {"Fn::Sub": "2"}}, 20, {"type": "object"}, 1),
3838
({"foo": 1, "bar": {"Fn::Sub": ["2", {}]}}, 10, {"type": "object"}, 0),
3939
({"foo": 1, "bar": {"Fn::Sub": ["2", {}]}}, 20, {"type": "object"}, 1),
40+
(
41+
{"foo": 1, "bar": {"Fn::Sub": ["2", {}]}},
42+
20,
43+
{"type": ["string", "object"]},
44+
1,
45+
),
4046
],
4147
)
4248
def test_min_length(instance, mL, expected, rule, schema, validator):
@@ -65,6 +71,12 @@ def test_min_length(instance, mL, expected, rule, schema, validator):
6571
({"foo": 1, "bar": {"Fn::Sub": "2"}}, 4, {"type": "object"}, 1),
6672
({"foo": 1, "bar": {"Fn::Sub": ["2", {}]}}, 20, {"type": "object"}, 0),
6773
({"foo": 1, "bar": {"Fn::Sub": ["2", {}]}}, 4, {"type": "object"}, 1),
74+
(
75+
{"foo": 1, "bar": {"Fn::Sub": ["2", {}]}},
76+
4,
77+
{"type": ["string", "object"]},
78+
1,
79+
),
6880
],
6981
)
7082
def test_max_length(instance, mL, expected, rule, schema, validator):

0 commit comments

Comments
 (0)