Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix validating min/maxLength with array types #3805

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading