Skip to content

Fix properties #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 7, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

PY3 = sys.version_info[0] >= 3

if PY3:
from raise_exception_3 import raise_exception_from
else:
from raise_exception_2 import raise_exception_from

if PY3:
basestring = unicode = str
iteritems = operator.methodcaller("items")
Expand Down Expand Up @@ -74,7 +79,7 @@ def _uniq(container):
},
"properties" : {
"type" : "object",
"additionalProperties" : {"$ref" : "#"},
"additionalProperties" : {"$ref" : "#", "type": "object"},
"default" : {}
},
"patternProperties" : {
Expand Down Expand Up @@ -152,6 +157,9 @@ class SchemaError(Exception):

"""

def __init__(self, *args, **kwargs):
self.errors = kwargs.pop("errors", [])
super(SchemaError, self).__init__(*args, **kwargs)

class ValidationError(Exception):
"""
Expand Down Expand Up @@ -361,7 +369,7 @@ def validate(self, instance, schema):
try:
self._meta_validator.validate(schema, self._version)
except ValidationError as e:
raise SchemaError(str(e))
raise_exception_from(SchemaError(str(e), errors=e.errors), e)

self._errors = []
self._validate(instance, schema)
Expand Down
5 changes: 5 additions & 0 deletions raise_exception_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Raise an exception from another exception 2.x.
import sys
def raise_exception_from(exception, original_exception):
tb = sys.exc_info()[2]
raise exception, None, tb
3 changes: 3 additions & 0 deletions raise_exception_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Raise an exception from another exception 3.x.
def raise_exception_from(exception, original_exception):
raise exception from original_exception
10 changes: 10 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,9 @@ def test_unknown_property_skip(self):
type="number")
)

def test_invalid_properties(self):
self.assertRaises(SchemaError, validate, {}, {"properties": {"test": True}})

# TODO: we're in need of more meta schema tests
def test_minItems_invalid_string(self):
with self.assertRaises(SchemaError):
Expand All @@ -639,6 +642,13 @@ def test_string_types_deprecated(self):
validate("foo", {"type" : "string"}, string_types=(unicode,))
self.assertEqual(len(w), 1)

def test_schema_error_errors(self):
try:
validate({}, { "properties": { "test": False } }, stop_on_error=False)
except SchemaError as e:
self.assertGreater(len(e.errors), 0)
else:
raise AssertionError("Expected SchemaError, got nothing")

class TestIgnorePropertiesForIrrelevantTypes(unittest.TestCase):
def test_minimum(self):
Expand Down