Skip to content

Commit bf9cc1c

Browse files
authored
Fix format error messaging when no pattern (#3943)
* Fix format error messaging when no pattern
1 parent 02c6d24 commit bf9cc1c

File tree

6 files changed

+45
-19
lines changed

6 files changed

+45
-19
lines changed

src/cfnlint/rules/formats/Format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def format(
3434

3535
if format == rule.format_keyword: # type: ignore
3636
if not rule.format(validator, instance): # type: ignore
37-
if hasattr(rule, "pattern"):
37+
if hasattr(rule, "pattern") and rule.pattern:
3838
yield ValidationError(
3939
(
4040
f"{instance!r} is not a {format!r} with "

src/cfnlint/rules/formats/ImageId.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ class ImageId(FormatKeyword):
2121
source_url = "https://github.com/aws-cloudformation/cfn-lint/blob/main/docs/format_keyword.md#AWS::EC2::Image.Id"
2222

2323
def __init__(self):
24-
super().__init__(format="AWS::EC2::Image.Id")
24+
super().__init__(
25+
format="AWS::EC2::Image.Id", pattern=r"^ami-([0-9a-z]{8}|[0-9a-z]{17})$"
26+
)
2527

2628
def format(self, validator: Validator, instance: Any) -> bool:
2729
if not isinstance(instance, str):
@@ -34,7 +36,7 @@ def format(self, validator: Validator, instance: Any) -> bool:
3436
if instance.startswith("resolve:ssm"):
3537
return True
3638

37-
if re.match(r"^ami-([0-9a-z]{8}|[0-9a-z]{17})$", instance):
39+
if re.match(self.pattern, instance):
3840
return True
3941

4042
return False

test/fixtures/results/quickstart/nist_application.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@
419419
},
420420
{
421421
"Filename": "test/fixtures/templates/quickstart/nist_application.yaml",
422-
"Id": "1613319d-ac84-a032-c510-b54d3061c0b1",
422+
"Id": "e2b23fec-440d-6576-01e9-f0a09f5874d4",
423423
"Level": "Warning",
424424
"Location": {
425425
"End": {
@@ -438,7 +438,7 @@
438438
"LineNumber": 379
439439
}
440440
},
441-
"Message": "{'Ref': 'pAppAmi'} is not a 'AWS::EC2::Image.Id' with pattern '' when 'Ref' is resolved",
441+
"Message": "{'Ref': 'pAppAmi'} is not a 'AWS::EC2::Image.Id' with pattern '^ami-([0-9a-z]{8}|[0-9a-z]{17})$' when 'Ref' is resolved",
442442
"ParentId": null,
443443
"Rule": {
444444
"Description": "Resolve the Ref and then validate the values against the schema",
@@ -674,7 +674,7 @@
674674
},
675675
{
676676
"Filename": "test/fixtures/templates/quickstart/nist_application.yaml",
677-
"Id": "a5d3cdc6-0e20-ebf4-e842-6341804c7ec3",
677+
"Id": "499f19a1-f0ba-d118-c02a-b8519ce4dcd7",
678678
"Level": "Warning",
679679
"Location": {
680680
"End": {
@@ -693,7 +693,7 @@
693693
"LineNumber": 511
694694
}
695695
},
696-
"Message": "{'Ref': 'pWebServerAMI'} is not a 'AWS::EC2::Image.Id' with pattern '' when 'Ref' is resolved",
696+
"Message": "{'Ref': 'pWebServerAMI'} is not a 'AWS::EC2::Image.Id' with pattern '^ami-([0-9a-z]{8}|[0-9a-z]{17})$' when 'Ref' is resolved",
697697
"ParentId": null,
698698
"Rule": {
699699
"Description": "Resolve the Ref and then validate the values against the schema",
@@ -1732,7 +1732,7 @@
17321732
},
17331733
{
17341734
"Filename": "test/fixtures/templates/quickstart/nist_application.yaml",
1735-
"Id": "ba50a58d-877c-2610-cc0f-4418059c3d40",
1735+
"Id": "a793117d-34b7-967d-1a68-ef79479b9f52",
17361736
"Level": "Warning",
17371737
"Location": {
17381738
"End": {
@@ -1751,7 +1751,7 @@
17511751
"LineNumber": 801
17521752
}
17531753
},
1754-
"Message": "{'Ref': 'pWebServerAMI'} is not a 'AWS::EC2::Image.Id' with pattern '' when 'Ref' is resolved",
1754+
"Message": "{'Ref': 'pWebServerAMI'} is not a 'AWS::EC2::Image.Id' with pattern '^ami-([0-9a-z]{8}|[0-9a-z]{17})$' when 'Ref' is resolved",
17551755
"ParentId": null,
17561756
"Rule": {
17571757
"Description": "Resolve the Ref and then validate the values against the schema",

test/fixtures/results/quickstart/nist_vpc_management.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@
416416
},
417417
{
418418
"Filename": "test/fixtures/templates/quickstart/nist_vpc_management.yaml",
419-
"Id": "1c7a978c-0331-1e35-7ac6-6edc8d947e1c",
419+
"Id": "9584e85a-7daa-e6fa-e582-de25884d042c",
420420
"Level": "Warning",
421421
"Location": {
422422
"End": {
@@ -435,7 +435,7 @@
435435
"LineNumber": 513
436436
}
437437
},
438-
"Message": "{'Ref': 'pBastionAmi'} is not a 'AWS::EC2::Image.Id' with pattern {'Ref': 'pBastionAmi'} when 'Ref' is resolved",
438+
"Message": "{'Ref': 'pBastionAmi'} is not a 'AWS::EC2::Image.Id' with pattern '^ami-([0-9a-z]{8}|[0-9a-z]{17})$' when 'Ref' is resolved",
439439
"ParentId": null,
440440
"Rule": {
441441
"Description": "Resolve the Ref and then validate the values against the schema",

test/fixtures/results/quickstart/non_strict/nist_application.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@
419419
},
420420
{
421421
"Filename": "test/fixtures/templates/quickstart/nist_application.yaml",
422-
"Id": "1613319d-ac84-a032-c510-b54d3061c0b1",
422+
"Id": "e2b23fec-440d-6576-01e9-f0a09f5874d4",
423423
"Level": "Warning",
424424
"Location": {
425425
"End": {
@@ -438,7 +438,7 @@
438438
"LineNumber": 379
439439
}
440440
},
441-
"Message": "{'Ref': 'pAppAmi'} is not a 'AWS::EC2::Image.Id' with pattern '' when 'Ref' is resolved",
441+
"Message": "{'Ref': 'pAppAmi'} is not a 'AWS::EC2::Image.Id' with pattern '^ami-([0-9a-z]{8}|[0-9a-z]{17})$' when 'Ref' is resolved",
442442
"ParentId": null,
443443
"Rule": {
444444
"Description": "Resolve the Ref and then validate the values against the schema",
@@ -645,7 +645,7 @@
645645
},
646646
{
647647
"Filename": "test/fixtures/templates/quickstart/nist_application.yaml",
648-
"Id": "a5d3cdc6-0e20-ebf4-e842-6341804c7ec3",
648+
"Id": "499f19a1-f0ba-d118-c02a-b8519ce4dcd7",
649649
"Level": "Warning",
650650
"Location": {
651651
"End": {
@@ -664,7 +664,7 @@
664664
"LineNumber": 511
665665
}
666666
},
667-
"Message": "{'Ref': 'pWebServerAMI'} is not a 'AWS::EC2::Image.Id' with pattern '' when 'Ref' is resolved",
667+
"Message": "{'Ref': 'pWebServerAMI'} is not a 'AWS::EC2::Image.Id' with pattern '^ami-([0-9a-z]{8}|[0-9a-z]{17})$' when 'Ref' is resolved",
668668
"ParentId": null,
669669
"Rule": {
670670
"Description": "Resolve the Ref and then validate the values against the schema",
@@ -937,7 +937,7 @@
937937
},
938938
{
939939
"Filename": "test/fixtures/templates/quickstart/nist_application.yaml",
940-
"Id": "ba50a58d-877c-2610-cc0f-4418059c3d40",
940+
"Id": "a793117d-34b7-967d-1a68-ef79479b9f52",
941941
"Level": "Warning",
942942
"Location": {
943943
"End": {
@@ -956,7 +956,7 @@
956956
"LineNumber": 801
957957
}
958958
},
959-
"Message": "{'Ref': 'pWebServerAMI'} is not a 'AWS::EC2::Image.Id' with pattern '' when 'Ref' is resolved",
959+
"Message": "{'Ref': 'pWebServerAMI'} is not a 'AWS::EC2::Image.Id' with pattern '^ami-([0-9a-z]{8}|[0-9a-z]{17})$' when 'Ref' is resolved",
960960
"ParentId": null,
961961
"Rule": {
962962
"Description": "Resolve the Ref and then validate the values against the schema",

test/unit/rules/formats/test_format.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ def format(self, validator, instance):
3636
return False
3737

3838

39+
class _FailWithPattern(FormatKeyword):
40+
id = "BBBBB"
41+
42+
def __init__(self):
43+
super().__init__(format="test", pattern=r"^.*$")
44+
45+
def format(self, validator, instance):
46+
return False
47+
48+
3949
class _NoRuleId(FormatKeyword):
4050
def __init__(self):
4151
super().__init__(format="test")
@@ -101,7 +111,7 @@ def format(self, validator, instance):
101111
},
102112
[
103113
ValidationError(
104-
"'10.10.10.10' is not a 'test' with pattern ''",
114+
"'10.10.10.10' is not a valid 'test'",
105115
rule=_Fail(),
106116
)
107117
],
@@ -116,11 +126,25 @@ def format(self, validator, instance):
116126
},
117127
[
118128
ValidationError(
119-
"'10.10.10.10' is not a 'test' with pattern ''",
129+
"'10.10.10.10' is not a valid 'test'",
120130
rule=_Fail(),
121131
)
122132
],
123133
),
134+
(
135+
"Fail with pattern",
136+
"test",
137+
"10.10.10.10",
138+
{
139+
"AAAAA": _FailWithPattern(),
140+
},
141+
[
142+
ValidationError(
143+
"'10.10.10.10' is not a 'test' with pattern '^.*$'",
144+
rule=_FailWithPattern(),
145+
)
146+
],
147+
),
124148
],
125149
)
126150
def test_validate(name, format, instance, child_rules, expected, rule, validator):

0 commit comments

Comments
 (0)