Skip to content

Commit 0acac1e

Browse files
authored
update error messagings from json schema (#3798)
1 parent d5c3da9 commit 0acac1e

17 files changed

+48
-63
lines changed

src/cfnlint/jsonschema/_keywords.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,9 @@ def maxItems(
323323
validator: Validator, mI: Any, instance: Any, schema: dict[str, Any]
324324
) -> ValidationResult: # pylint: disable=arguments-renamed
325325
if validator.is_type(instance, "array") and len(instance) > mI:
326-
yield ValidationError(f"{instance!r} is too long ({mI})")
326+
yield ValidationError(
327+
f"expected maximum item count: {mI}, found: {len(instance)}"
328+
)
327329

328330

329331
def maxLength(
@@ -333,7 +335,7 @@ def maxLength(
333335
return
334336

335337
if len(instance) > mL:
336-
yield ValidationError(f"{instance!r} is longer than {mL}")
338+
yield ValidationError(f"expected maximum length: {mL}, found: {len(instance)}")
337339

338340

339341
def maxProperties(
@@ -342,7 +344,9 @@ def maxProperties(
342344
if not validator.is_type(instance, "object"):
343345
return
344346
if validator.is_type(instance, "object") and len(instance) > mP:
345-
yield ValidationError(f"{instance!r} has too many properties")
347+
yield ValidationError(
348+
f"expected maximum property count: {mP}, found: {len(instance)}"
349+
)
346350

347351

348352
def maximum(
@@ -366,7 +370,9 @@ def minItems(
366370
validator: Validator, mI: Any, instance: Any, schema: dict[str, Any]
367371
) -> ValidationResult:
368372
if validator.is_type(instance, "array") and len(instance) < mI:
369-
yield ValidationError(f"{instance!r} is too short ({mI})")
373+
yield ValidationError(
374+
f"expected minimum item count: {mI}, found: {len(instance)}"
375+
)
370376

371377

372378
def minLength(
@@ -376,14 +382,16 @@ def minLength(
376382
return
377383

378384
if len(instance) < mL:
379-
yield ValidationError(f"{instance!r} is shorter than {mL}")
385+
yield ValidationError(f"expected minimum length: {mL}, found: {len(instance)}")
380386

381387

382388
def minProperties(
383389
validator: Validator, mP: Any, instance: Any, schema: dict[str, Any]
384390
) -> ValidationResult:
385391
if validator.is_type(instance, "object") and len(instance) < mP:
386-
yield ValidationError(f"{instance!r} does not have enough properties")
392+
yield ValidationError(
393+
f"expected minimum property count: {mP}, found: {len(instance)}"
394+
)
387395

388396

389397
def minimum(
@@ -590,14 +598,14 @@ def uniqueItems(
590598
validator: Validator, uI: Any, instance: Any, schema: dict[str, Any]
591599
) -> ValidationResult:
592600
if uI and validator.is_type(instance, "array") and not uniq(instance):
593-
yield ValidationError(f"{instance!r} has non-unique elements")
601+
yield ValidationError("array items are not unique")
594602

595603

596604
def uniqueKeys(
597605
validator: Validator, uKs: Any, instance: Any, schema: dict[str, Any]
598606
) -> ValidationResult:
599607
if uKs and validator.is_type(instance, "array") and not uniq_keys(instance, uKs):
600-
yield ValidationError(f"{instance!r} has non-unique elements for keys {uKs!r}")
608+
yield ValidationError(f"array items are not unique for keys {uKs!r}")
601609

602610

603611
def type(

src/cfnlint/rules/functions/_BaseFn.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def resolve(
8787
all_errs = []
8888
for value, v, resolve_err in validator.resolve_value(instance):
8989
if resolve_err:
90-
yield resolve_err
90+
yield from self.fix_errors(iter([resolve_err]))
9191
continue
9292
errs = list(
9393
self.fix_errors(

test/integration/jsonschema/test_validator_cfn.py

+3-15
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,7 @@ def test_unique_items(self):
189189
schema_patches=schema_patch,
190190
expected_errs=[
191191
ValidationError(
192-
message=(
193-
"[{'Key': 'Name', 'Value': 'Value'}, "
194-
"{'Key': 'Name', 'Value': 'Value'}] has "
195-
"non-unique elements"
196-
),
192+
message=("array items are not unique"),
197193
path=deque(["Tags"]),
198194
validator="uniqueItems",
199195
validator_value=True,
@@ -220,11 +216,7 @@ def test_unique_items(self):
220216
schema_patches=schema_patch,
221217
expected_errs=[
222218
ValidationError(
223-
message=(
224-
"[{'Key': 'Name', 'Value': 'Value'}, "
225-
"{'Key': 'Name', 'Value': 'Value'}] has "
226-
"non-unique elements"
227-
),
219+
message=("array items are not unique"),
228220
path=deque(["Tags"]),
229221
validator="uniqueItems",
230222
validator_value=True,
@@ -289,11 +281,7 @@ def test_unique_items(self):
289281
schema_patches=schema_patch,
290282
expected_errs=[
291283
ValidationError(
292-
message=(
293-
"[{'Key': 'Name', 'Value': 'Value'}, "
294-
"{'Key': 'Name', 'Value': 'Value'}] has "
295-
"non-unique elements"
296-
),
284+
message=("array items are not unique"),
297285
path=deque(["Tags"]),
298286
validator="uniqueItems",
299287
validator_value=True,

test/unit/module/jsonschema/test_validator.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ def test_validator(name, schema, instance, expected, validator):
532532
[],
533533
[
534534
ValidationError(
535-
"[] is too short (2)",
535+
"expected minimum item count: 2, found: 0",
536536
)
537537
],
538538
),
@@ -552,11 +552,7 @@ def test_validator(name, schema, instance, expected, validator):
552552
"maxItems",
553553
{"maxItems": 0},
554554
["foo"],
555-
[
556-
ValidationError(
557-
"['foo'] is too long (0)",
558-
)
559-
],
555+
[ValidationError("expected maximum item count: 0, found: 1")],
560556
),
561557
(
562558
"valid minLength",
@@ -576,7 +572,7 @@ def test_validator(name, schema, instance, expected, validator):
576572
"",
577573
[
578574
ValidationError(
579-
"'' is shorter than 2",
575+
"expected minimum length: 2, found: 0",
580576
)
581577
],
582578
),
@@ -598,7 +594,7 @@ def test_validator(name, schema, instance, expected, validator):
598594
"foo",
599595
[
600596
ValidationError(
601-
"'foo' is longer than 0",
597+
"expected maximum length: 0, found: 3",
602598
)
603599
],
604600
),
@@ -768,7 +764,7 @@ def test_validator(name, schema, instance, expected, validator):
768764
{},
769765
[
770766
ValidationError(
771-
"{} does not have enough properties",
767+
"expected minimum property count: 1, found: 0",
772768
)
773769
],
774770
),
@@ -790,7 +786,7 @@ def test_validator(name, schema, instance, expected, validator):
790786
{"foo": {}, "bar": {}},
791787
[
792788
ValidationError(
793-
"{'foo': {}, 'bar': {}} has too many properties",
789+
"expected maximum property count: 1, found: 2",
794790
)
795791
],
796792
),
@@ -943,7 +939,7 @@ def test_validator(name, schema, instance, expected, validator):
943939
[1, 2, "1"],
944940
[
945941
ValidationError(
946-
"[1, 2, '1'] has non-unique elements",
942+
"array items are not unique",
947943
)
948944
],
949945
),
@@ -1035,8 +1031,7 @@ def test_validator(name, schema, instance, expected, validator):
10351031
],
10361032
[
10371033
ValidationError(
1038-
"[{'Name': 'foo'}, {'Name': 'foo'}] has non-unique "
1039-
"elements for keys ['Name']",
1034+
"array items are not unique " "for keys ['Name']",
10401035
)
10411036
],
10421037
),

test/unit/rules/functions/test_dynamic_reference.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def context(cfn):
110110
},
111111
[
112112
ValidationError(
113-
"['resolve', 'ssm'] is too short (3)",
113+
"expected minimum item count: 3, found: 2",
114114
validator="minItems",
115115
rule=DynamicReference(),
116116
)
@@ -182,10 +182,7 @@ def context(cfn):
182182
},
183183
[
184184
ValidationError(
185-
"['resolve', 'secretsmanager', 'arn', 'aws', "
186-
"'secretsmanager', 'us-east-1', '012345678901', "
187-
"'secret', 'my-secret', 'SecretString', "
188-
"'', '', '', ''] is too long (13)",
185+
"expected maximum item count: 13, found: 14",
189186
validator="maxItems",
190187
rule=DynamicReference(),
191188
)

test/unit/rules/functions/test_find_in_map.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def context(cfn):
6363
None,
6464
[
6565
ValidationError(
66-
"['foo', 'bar', 'key', 'key2'] is too long (3)",
66+
"expected maximum item count: 3, found: 4",
6767
path=deque(["Fn::FindInMap"]),
6868
schema_path=deque(["maxItems"]),
6969
validator="fn_findinmap",
@@ -164,6 +164,7 @@ def context(cfn):
164164
"'C' is not one of ['B'] for mapping 'A'",
165165
path=deque(["Fn::FindInMap", 1]),
166166
schema_path=deque([]),
167+
validator="fn_findinmap",
167168
),
168169
],
169170
),

test/unit/rules/functions/test_if.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def validator(cfn, context):
6363
{"type": "string"},
6464
[
6565
ValidationError(
66-
"['IsUsEast1', 'foo', 'bar', 'key'] is too long (3)",
66+
"expected maximum item count: 3, found: 4",
6767
path=deque(["Fn::If"]),
6868
schema_path=deque(["maxItems"]),
6969
validator="fn_if",

test/unit/rules/functions/test_sub.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def context(cfn):
168168
{"type": "string"},
169169
[
170170
ValidationError(
171-
"['${foo}', {'foo': 'bar'}, {}] is too long (2)",
171+
"expected maximum item count: 2, found: 3",
172172
path=deque(["Fn::Sub"]),
173173
schema_path=deque(["maxItems"]),
174174
validator="fn_sub",

test/unit/rules/functions/test_tojsonstring.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def context(cfn):
7878
{"transforms": Transforms(["AWS::LanguageExtensions"])},
7979
[
8080
ValidationError(
81-
"{} does not have enough properties",
81+
"expected minimum property count: 1, found: 0",
8282
path=deque(["Fn::ToJsonString"]),
8383
schema_path=deque(["minProperties"]),
8484
validator="fn_tojsonstring",
@@ -93,7 +93,7 @@ def context(cfn):
9393
{"transforms": Transforms(["AWS::LanguageExtensions"])},
9494
[
9595
ValidationError(
96-
"[] is too short (1)",
96+
"expected minimum item count: 1, found: 0",
9797
path=deque(["Fn::ToJsonString"]),
9898
schema_path=deque(["minItems"]),
9999
validator="fn_tojsonstring",

test/unit/rules/mappings/test_configuration.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def rule():
125125
{"a" * 256: {"Key": {"Value": "Foo"}}},
126126
[
127127
ValidationError(
128-
f"{'a'*256!r} is longer than 255",
128+
"expected maximum length: 255, found: 256",
129129
validator="maxLength",
130130
schema_path=deque(["propertyNames", "maxLength"]),
131131
rule=None, # empty because we didn't load child rules

test/unit/rules/outputs/test_configuration.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def context(cfn):
119119
},
120120
[
121121
ValidationError(
122-
f"{'a'*256!r} is longer than 255",
122+
"expected maximum length: 255, found: 256",
123123
validator="maxLength",
124124
schema_path=deque(["propertyNames", "maxLength"]),
125125
rule=None, # none becuase we don't load child rule

test/unit/rules/parameters/test_configuration.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def context(cfn):
172172
},
173173
[
174174
ValidationError(
175-
f"{'a'*256!r} is longer than 255",
175+
"expected maximum length: 255, found: 256",
176176
validator="maxLength",
177177
schema_path=deque(["propertyNames", "maxLength"]),
178178
rule=None, # none becuase we don't load child rule

test/unit/rules/resources/ecs/test_service_health_check_grace_period_seconds.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def template():
4747
{"HealthCheckGracePeriodSeconds": "Foo", "LoadBalancers": []},
4848
[
4949
ValidationError(
50-
"[] is too short (1)",
50+
"expected minimum item count: 1, found: 0",
5151
rule=ServiceHealthCheckGracePeriodSeconds(),
5252
path=deque(["LoadBalancers"]),
5353
validator="minItems",
@@ -80,7 +80,7 @@ def template():
8080
},
8181
[
8282
ValidationError(
83-
"[] is too short (1)",
83+
"expected minimum item count: 1, found: 0",
8484
rule=ServiceHealthCheckGracePeriodSeconds(),
8585
path=deque(["LoadBalancers"]),
8686
validator="minItems",

test/unit/rules/resources/elbv2/test_loadbalancer_application_subnets.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def rule():
4040
{"Subnets": ["SubnetA"]},
4141
[
4242
ValidationError(
43-
("['SubnetA'] is too short (2)"),
43+
"expected minimum item count: 2, found: 1",
4444
rule=LoadBalancerApplicationSubnets(),
4545
path=deque(["Subnets"]),
4646
validator="minItems",
@@ -55,7 +55,7 @@ def rule():
5555
},
5656
[
5757
ValidationError(
58-
("['SubnetA'] is too short (2)"),
58+
"expected minimum item count: 2, found: 1",
5959
rule=LoadBalancerApplicationSubnets(),
6060
path=deque(["Subnets"]),
6161
validator="minItems",

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ def rule():
3838
{"taggable": True},
3939
[
4040
ValidationError(
41-
(
42-
"[{'Key': 'Foo', 'Value': 'Bar'}, "
43-
"{'Key': 'Foo', 'Value': 'Bar'}] "
44-
"has non-unique elements for keys ['Key']"
45-
),
41+
("array items are not unique for keys ['Key']"),
4642
path=deque(["Tags"]),
4743
schema_path=deque(["properties", "Tags", "uniqueKeys"]),
4844
validator="tagging",
@@ -59,7 +55,7 @@ def rule():
5955
{"taggable": True},
6056
[
6157
ValidationError(
62-
f"{'a'*257!r} is longer than 256",
58+
"expected maximum length: 256, found: 257",
6359
path=deque(["Tags", 0, "Value"]),
6460
schema_path=deque(
6561
[
@@ -83,7 +79,7 @@ def rule():
8379
{"taggable": True},
8480
[
8581
ValidationError(
86-
f"{'a'*257!r} is longer than 256",
82+
"expected maximum length: 256, found: 257",
8783
path=deque(["Tags", "Foo"]),
8884
schema_path=deque(
8985
[

test/unit/rules/resources/route53/test_recordsets.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ def rule():
383383
),
384384
),
385385
ValidationError(
386-
"['cname1.example.com', 'foo√bar'] is too long (1)",
386+
"expected maximum item count: 1, found: 2",
387387
rule=RecordSet(),
388388
path=deque(["ResourceRecords"]),
389389
validator="maxItems",

test/unit/rules/templates/test_limitsize_description.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def rule():
4141
None,
4242
[
4343
ValidationError(
44-
("'FooBar' is longer than 3"),
44+
("expected maximum length: 3, found: 6"),
4545
)
4646
],
4747
),

0 commit comments

Comments
 (0)