Skip to content

Commit 7c4eefb

Browse files
Provide test cases to previous fix to ensure proper error handling items errors.
1 parent bfc06bd commit 7c4eefb

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

jsonschema/tests/test_validators.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,66 @@ def test_helpful_error_message_on_failed_pop_scope(self):
899899
self.assertIn("Failed to pop the scope", str(exc.exception))
900900

901901

902+
OVERRIDE_ARRAY_TYPES = Draft4Validator.DEFAULT_TYPES.copy()
903+
OVERRIDE_ARRAY_TYPES['array'] = (list, tuple)
904+
905+
906+
class ItemsErrorsMixin(object):
907+
908+
array_type = list
909+
schema = {
910+
'type': 'array',
911+
'items': {
912+
'type': 'number',
913+
},
914+
'minItems': 2,
915+
'maxItems': 3,
916+
'uniqueItems': True,
917+
}
918+
validator_class = None
919+
920+
def check_error_message(self, instance):
921+
self.assertIsNotNone(self.validator_class)
922+
self.assertRaises(ValidationError,
923+
validate,
924+
instance,
925+
self.schema,
926+
self.validator_class)
927+
928+
def test_min_items(self):
929+
self.check_error_message(self.array_type([1]))
930+
931+
def test_max_items(self):
932+
self.check_error_message(self.array_type([1, 2, 3, 4]))
933+
934+
def test_unique_items(self):
935+
self.check_error_message(self.array_type([1, 1, 2]))
936+
937+
def test_valid(self):
938+
self.assertIsNotNone(self.validator_class)
939+
validate(self.array_type([1, 2]), self.schema, self.validator_class)
940+
941+
942+
class OverrideArrayTypeValidator(Draft4Validator):
943+
944+
DEFAULT_TYPES = OVERRIDE_ARRAY_TYPES
945+
946+
947+
class TestItemsErrorsDraft3(ItemsErrorsMixin, unittest.TestCase):
948+
949+
validator_class = Draft3Validator
950+
951+
952+
class TestItemsErrorsDraft4(ItemsErrorsMixin, unittest.TestCase):
953+
954+
validator_class = Draft4Validator
955+
956+
957+
class TestItemsErrorMessagesCustom(ItemsErrorsMixin, unittest.TestCase):
958+
959+
validator_class = OverrideArrayTypeValidator
960+
961+
902962
def sorted_errors(errors):
903963
def key(error):
904964
return (

0 commit comments

Comments
 (0)