Skip to content

Commit a5672f0

Browse files
authored
add logic to handle substitutions (#3768)
* add logic to handle substitutions
1 parent 8e463fb commit a5672f0

File tree

2 files changed

+555
-333
lines changed

2 files changed

+555
-333
lines changed

src/cfnlint/rules/resources/stepfunctions/StateMachineDefinition.py

+49-24
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class StateMachineDefinition(CfnLintJsonSchema):
2929
def __init__(self):
3030
super().__init__(
3131
keywords=[
32-
"Resources/AWS::StepFunctions::StateMachine/Properties/Definition",
32+
"Resources/AWS::StepFunctions::StateMachine/Properties",
3333
# https://github.com/aws-cloudformation/cfn-lint/issues/3518
3434
# Resources/AWS::StepFunctions::StateMachine/Properties/DefinitionString
3535
],
@@ -55,32 +55,57 @@ def _fix_message(self, err: ValidationError) -> ValidationError:
5555
def validate(
5656
self, validator: Validator, keywords: Any, instance: Any, schema: dict[str, Any]
5757
) -> ValidationResult:
58+
59+
definition_keys = ["Definition"]
60+
if not validator.cfn.has_serverless_transform():
61+
definition_keys.append("DefinitionString")
62+
5863
# First time child rules are configured against the rule
5964
# so we can run this now
60-
add_path_to_message = False
61-
if validator.is_type(instance, "string"):
62-
try:
65+
for k in definition_keys:
66+
value = instance.get(k)
67+
if not value:
68+
continue
69+
70+
add_path_to_message = False
71+
if validator.is_type(value, "string"):
72+
try:
73+
step_validator = validator.evolve(
74+
context=validator.context.evolve(
75+
functions=[],
76+
),
77+
resolver=self.resolver,
78+
schema=self.schema,
79+
)
80+
value = json.loads(value)
81+
add_path_to_message = True
82+
except json.JSONDecodeError:
83+
return
84+
else:
6385
step_validator = validator.evolve(
64-
context=validator.context.evolve(
65-
functions=[],
66-
),
6786
resolver=self.resolver,
6887
schema=self.schema,
6988
)
70-
instance = json.loads(instance)
71-
add_path_to_message = True
72-
except json.JSONDecodeError:
73-
return
74-
else:
75-
step_validator = validator.evolve(
76-
resolver=self.resolver,
77-
schema=self.schema,
78-
)
79-
80-
for err in step_validator.iter_errors(instance):
81-
if add_path_to_message:
82-
err = self._fix_message(err)
83-
if not err.validator.startswith("fn_") and err.validator not in ["cfnLint"]:
84-
err.rule = self
85-
86-
yield self._clean_error(err)
89+
90+
substitutions = []
91+
props_substitutions = instance.get("DefinitionSubstitutions", {})
92+
if validator.is_type(props_substitutions, "object"):
93+
substitutions = list(props_substitutions.keys())
94+
95+
for err in step_validator.iter_errors(value):
96+
if validator.is_type(err.instance, "string"):
97+
if (
98+
err.instance.replace("${", "").replace("}", "").strip()
99+
in substitutions
100+
):
101+
continue
102+
if add_path_to_message:
103+
err = self._fix_message(err)
104+
105+
err.path.appendleft(k)
106+
if not err.validator.startswith("fn_") and err.validator not in [
107+
"cfnLint"
108+
]:
109+
err.rule = self
110+
111+
yield self._clean_error(err)

0 commit comments

Comments
 (0)