diff options
author | Julian Berman <Julian@GrayVines.com> | 2015-09-17 07:56:34 -0400 |
---|---|---|
committer | Julian Berman <Julian@GrayVines.com> | 2015-09-17 07:56:34 -0400 |
commit | 94c60a45a52a89318c2149ca45267acee7688258 (patch) | |
tree | eaa1bec023671e2675d94b0df61e6083b2ba02a6 | |
parent | 96c5f476a1cb5152e61eef8d4771260328d1345b (diff) | |
parent | 7c4eefb881e8d366b21cffb492173f57f8e3ee22 (diff) | |
download | jsonschema-94c60a45a52a89318c2149ca45267acee7688258.tar.gz |
Merge remote-tracking branch 'playpauseandstop/master'
* playpauseandstop/master:
Provide test cases to previous fix to ensure proper error handling items errors.
Fix formatting error for unique items if instance is tuple.
-rw-r--r-- | jsonschema/_validators.py | 2 | ||||
-rw-r--r-- | jsonschema/tests/test_validators.py | 60 |
2 files changed, 61 insertions, 1 deletions
diff --git a/jsonschema/_validators.py b/jsonschema/_validators.py index f566f6d..40fe340 100644 --- a/jsonschema/_validators.py +++ b/jsonschema/_validators.py @@ -133,7 +133,7 @@ def uniqueItems(validator, uI, instance, schema): validator.is_type(instance, "array") and not _utils.uniq(instance) ): - yield ValidationError("%r has non-unique elements" % instance) + yield ValidationError("%r has non-unique elements" % (instance,)) def pattern(validator, patrn, instance, schema): diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py index e34efca..4e3fe61 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -899,6 +899,66 @@ class TestRefResolver(unittest.TestCase): self.assertIn("Failed to pop the scope", str(exc.exception)) +OVERRIDE_ARRAY_TYPES = Draft4Validator.DEFAULT_TYPES.copy() +OVERRIDE_ARRAY_TYPES['array'] = (list, tuple) + + +class ItemsErrorsMixin(object): + + array_type = list + schema = { + 'type': 'array', + 'items': { + 'type': 'number', + }, + 'minItems': 2, + 'maxItems': 3, + 'uniqueItems': True, + } + validator_class = None + + def check_error_message(self, instance): + self.assertIsNotNone(self.validator_class) + self.assertRaises(ValidationError, + validate, + instance, + self.schema, + self.validator_class) + + def test_min_items(self): + self.check_error_message(self.array_type([1])) + + def test_max_items(self): + self.check_error_message(self.array_type([1, 2, 3, 4])) + + def test_unique_items(self): + self.check_error_message(self.array_type([1, 1, 2])) + + def test_valid(self): + self.assertIsNotNone(self.validator_class) + validate(self.array_type([1, 2]), self.schema, self.validator_class) + + +class OverrideArrayTypeValidator(Draft4Validator): + + DEFAULT_TYPES = OVERRIDE_ARRAY_TYPES + + +class TestItemsErrorsDraft3(ItemsErrorsMixin, unittest.TestCase): + + validator_class = Draft3Validator + + +class TestItemsErrorsDraft4(ItemsErrorsMixin, unittest.TestCase): + + validator_class = Draft4Validator + + +class TestItemsErrorMessagesCustom(ItemsErrorsMixin, unittest.TestCase): + + validator_class = OverrideArrayTypeValidator + + def sorted_errors(errors): def key(error): return ( |