Skip to content

Commit a1d4cb3

Browse files
committed
Special-case the error messages for min/maxItems' special values.
minItems 1 really means "non-empty", and maxItems 0 is a long way of writing {"const": []} but means "empty".
1 parent 294ecd6 commit a1d4cb3

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

jsonschema/_keywords.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,14 @@ def multipleOf(validator, dB, instance, schema):
192192

193193
def minItems(validator, mI, instance, schema):
194194
if validator.is_type(instance, "array") and len(instance) < mI:
195-
yield ValidationError(f"{instance!r} is too short")
195+
message = "should be non-empty" if mI == 1 else "is too short"
196+
yield ValidationError(f"{instance!r} {message}")
196197

197198

198199
def maxItems(validator, mI, instance, schema):
199200
if validator.is_type(instance, "array") and len(instance) > mI:
200-
yield ValidationError(f"{instance!r} is too long")
201+
message = "is expected to be empty" if mI == 0 else "is too long"
202+
yield ValidationError(f"{instance!r} {message}")
201203

202204

203205
def uniqueItems(validator, uI, instance, schema):

jsonschema/tests/test_validators.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,14 @@ def test_maxItems(self):
509509
message = self.message_for(instance=[1, 2, 3], schema={"maxItems": 2})
510510
self.assertEqual(message, "[1, 2, 3] is too long")
511511

512+
def test_minItems_1(self):
513+
message = self.message_for(instance=[], schema={"minItems": 1})
514+
self.assertEqual(message, "[] should be non-empty")
515+
516+
def test_maxItems_0(self):
517+
message = self.message_for(instance=[1, 2, 3], schema={"maxItems": 0})
518+
self.assertEqual(message, "[1, 2, 3] is expected to be empty")
519+
512520
def test_prefixItems_with_items(self):
513521
message = self.message_for(
514522
instance=[1, 2, "foo"],

0 commit comments

Comments
 (0)