Skip to content

Commit 8875c2e

Browse files
committed
Also improve min{Length,Properties}'s error message.
1 parent 74dfd48 commit 8875c2e

File tree

2 files changed

+61
-32
lines changed

2 files changed

+61
-32
lines changed

Diff for: jsonschema/_keywords.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,14 @@ def format(validator, format, instance, schema):
228228

229229
def minLength(validator, mL, instance, schema):
230230
if validator.is_type(instance, "string") and len(instance) < mL:
231-
yield ValidationError(f"{instance!r} is too short")
231+
message = "should be non-empty" if mL == 1 else "is too short"
232+
yield ValidationError(f"{instance!r} {message}")
232233

233234

234235
def maxLength(validator, mL, instance, schema):
235236
if validator.is_type(instance, "string") and len(instance) > mL:
236-
yield ValidationError(f"{instance!r} is too long")
237+
message = "is expected to be empty" if mL == 0 else "is too long"
238+
yield ValidationError(f"{instance!r} {message}")
237239

238240

239241
def dependentRequired(validator, dependentRequired, instance, schema):
@@ -307,14 +309,22 @@ def required(validator, required, instance, schema):
307309

308310
def minProperties(validator, mP, instance, schema):
309311
if validator.is_type(instance, "object") and len(instance) < mP:
310-
yield ValidationError(f"{instance!r} does not have enough properties")
312+
message = (
313+
"should be non-empty" if mP == 1
314+
else "does not have enough properties"
315+
)
316+
yield ValidationError(f"{instance!r} {message}")
311317

312318

313319
def maxProperties(validator, mP, instance, schema):
314320
if not validator.is_type(instance, "object"):
315321
return
316322
if validator.is_type(instance, "object") and len(instance) > mP:
317-
yield ValidationError(f"{instance!r} has too many properties")
323+
message = (
324+
"is expected to be empty" if mP == 0
325+
else "has too many properties"
326+
)
327+
yield ValidationError(f"{instance!r} {message}")
318328

319329

320330
def allOf(validator, allOf, instance, schema):

Diff for: jsonschema/tests/test_validators.py

+47-28
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,53 @@ def test_maxItems_0(self):
517517
message = self.message_for(instance=[1, 2, 3], schema={"maxItems": 0})
518518
self.assertEqual(message, "[1, 2, 3] is expected to be empty")
519519

520+
def test_minLength(self):
521+
message = self.message_for(
522+
instance="",
523+
schema={"minLength": 2},
524+
)
525+
self.assertEqual(message, "'' is too short")
526+
527+
def test_maxLength(self):
528+
message = self.message_for(
529+
instance="abc",
530+
schema={"maxLength": 2},
531+
)
532+
self.assertEqual(message, "'abc' is too long")
533+
534+
def test_minLength_1(self):
535+
message = self.message_for(instance="", schema={"minLength": 1})
536+
self.assertEqual(message, "'' should be non-empty")
537+
538+
def test_maxLength_0(self):
539+
message = self.message_for(instance="abc", schema={"maxLength": 0})
540+
self.assertEqual(message, "'abc' is expected to be empty")
541+
542+
def test_minProperties(self):
543+
message = self.message_for(instance={}, schema={"minProperties": 2})
544+
self.assertEqual(message, "{} does not have enough properties")
545+
546+
def test_maxProperties(self):
547+
message = self.message_for(
548+
instance={"a": {}, "b": {}, "c": {}},
549+
schema={"maxProperties": 2},
550+
)
551+
self.assertEqual(
552+
message,
553+
"{'a': {}, 'b': {}, 'c': {}} has too many properties",
554+
)
555+
556+
def test_minProperties_1(self):
557+
message = self.message_for(instance={}, schema={"minProperties": 1})
558+
self.assertEqual(message, "{} should be non-empty")
559+
560+
def test_maxProperties_0(self):
561+
message = self.message_for(
562+
instance={1: 2},
563+
schema={"maxProperties": 0},
564+
)
565+
self.assertEqual(message, "{1: 2} is expected to be empty")
566+
520567
def test_prefixItems_with_items(self):
521568
message = self.message_for(
522569
instance=[1, 2, "foo"],
@@ -537,20 +584,6 @@ def test_prefixItems_with_multiple_extra_items(self):
537584
"Expected at most 2 items but found 2 extra: ['foo', 5]",
538585
)
539586

540-
def test_minLength(self):
541-
message = self.message_for(
542-
instance="",
543-
schema={"minLength": 2},
544-
)
545-
self.assertEqual(message, "'' is too short")
546-
547-
def test_maxLength(self):
548-
message = self.message_for(
549-
instance="abc",
550-
schema={"maxLength": 2},
551-
)
552-
self.assertEqual(message, "'abc' is too long")
553-
554587
def test_pattern(self):
555588
message = self.message_for(
556589
instance="bbb",
@@ -646,20 +679,6 @@ def test_dependentRequired(self):
646679
)
647680
self.assertEqual(message, "'bar' is a dependency of 'foo'")
648681

649-
def test_minProperties(self):
650-
message = self.message_for(instance={}, schema={"minProperties": 2})
651-
self.assertEqual(message, "{} does not have enough properties")
652-
653-
def test_maxProperties(self):
654-
message = self.message_for(
655-
instance={"a": {}, "b": {}, "c": {}},
656-
schema={"maxProperties": 2},
657-
)
658-
self.assertEqual(
659-
message,
660-
"{'a': {}, 'b': {}, 'c': {}} has too many properties",
661-
)
662-
663682
def test_oneOf_matches_none(self):
664683
message = self.message_for(instance={}, schema={"oneOf": [False]})
665684
self.assertEqual(

0 commit comments

Comments
 (0)