Skip to content

Commit

Permalink
Fix validating min/maxLength with array types (#3805)
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong authored Nov 1, 2024
1 parent 88efb08 commit c817aed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/cfnlint/rules/resources/properties/StringLength.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import regex as re

from cfnlint.helpers import FUNCTIONS
from cfnlint.helpers import FUNCTIONS, ensure_list, is_function
from cfnlint.jsonschema import ValidationError
from cfnlint.rules import CloudFormationLintRule

Expand Down Expand Up @@ -80,8 +80,8 @@ def maxLength(self, validator, mL, instance, schema):
return
# there are scenarios where Fn::Sub may not predictable so use
# best judgement
if validator.is_type(instance, "object") and len(instance) == 1:
key = list(instance.keys())[0]
key, value = is_function(instance)
if key is not None:
if key == "Fn::Sub":
value = instance[key]
if isinstance(value, str):
Expand All @@ -93,7 +93,7 @@ def maxLength(self, validator, mL, instance, schema):
validator, mL, self._fix_sub_string(value[0]), schema
)
return
if schema.get("type") == "object":
if "object" in ensure_list(schema.get("type")):
yield from self._non_string_max_length(instance, mL)

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

# there are scenarios where Fn::Sub may not predictable so use
# best judgement
if validator.is_type(instance, "object") and len(instance) == 1:
key = list(instance.keys())[0]
key, value = is_function(instance)
if key is not None:
if key == "Fn::Sub":
value = instance[key]
if isinstance(value, str):
Expand All @@ -118,5 +118,6 @@ def minLength(self, validator, mL, instance, schema):
validator, mL, self._fix_sub_string(value[0]), schema
)
return
if schema.get("type") == "object":

if "object" in ensure_list(schema.get("type")):
yield from self._non_string_min_length(instance, mL)
12 changes: 12 additions & 0 deletions test/unit/rules/resources/properties/test_string_length.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def rule():
({"foo": 1, "bar": {"Fn::Sub": "2"}}, 20, {"type": "object"}, 1),
({"foo": 1, "bar": {"Fn::Sub": ["2", {}]}}, 10, {"type": "object"}, 0),
({"foo": 1, "bar": {"Fn::Sub": ["2", {}]}}, 20, {"type": "object"}, 1),
(
{"foo": 1, "bar": {"Fn::Sub": ["2", {}]}},
20,
{"type": ["string", "object"]},
1,
),
],
)
def test_min_length(instance, mL, expected, rule, schema, validator):
Expand Down Expand Up @@ -65,6 +71,12 @@ def test_min_length(instance, mL, expected, rule, schema, validator):
({"foo": 1, "bar": {"Fn::Sub": "2"}}, 4, {"type": "object"}, 1),
({"foo": 1, "bar": {"Fn::Sub": ["2", {}]}}, 20, {"type": "object"}, 0),
({"foo": 1, "bar": {"Fn::Sub": ["2", {}]}}, 4, {"type": "object"}, 1),
(
{"foo": 1, "bar": {"Fn::Sub": ["2", {}]}},
4,
{"type": ["string", "object"]},
1,
),
],
)
def test_max_length(instance, mL, expected, rule, schema, validator):
Expand Down

0 comments on commit c817aed

Please sign in to comment.