Skip to content

Commit 88c08b8

Browse files
authored
Merge pull request #62 from cwacek/feature/feature/check-schema-first
feature: Use jsonschema to validate the schema itself before load
2 parents 0b54c41 + d19a6ea commit 88c08b8

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

python_jsonschema_objects/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ def __init__(self, schema_uri, resolved={}):
4343
}
4444
)
4545

46+
meta_validator = Draft4Validator(Draft4Validator.META_SCHEMA)
47+
meta_validator.validate(self.schema)
4648
self.validator = Draft4Validator(self.schema,
4749
resolver=self.resolver)
4850

51+
4952
self._classes = None
5053
self._resolved = None
5154

test/test_pytest.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,38 @@
22

33
import json
44
import six
5-
import pkg_resources
5+
import jsonschema
66
import python_jsonschema_objects as pjs
77

88
import logging
99
logging.basicConfig(level=logging.DEBUG)
1010

1111

12+
def test_schema_validation():
13+
""" Test that the ObjectBuilder validates the schema itself.
14+
"""
15+
schema = {
16+
"$schema": "http://json-schema.org/schema#",
17+
"id": "test",
18+
"type": "object",
19+
"properties": {
20+
"name": "string",
21+
"email": {"oneOf": [{"type": "string"}, {"type": "integer"}]},
22+
},
23+
"required": ["email"]
24+
}
25+
with pytest.raises(jsonschema.ValidationError):
26+
pjs.ObjectBuilder(schema)
27+
28+
1229
def test_regression_9():
1330
schema = {
1431
"$schema": "http://json-schema.org/schema#",
1532
"id": "test",
1633
"type": "object",
1734
"properties": {
1835
"name": {"type": "string"},
19-
"email": {"oneOf": ["string", "integer"]},
36+
"email": {"oneOf": [{"type": "string"}, {"type": "integer"}]},
2037
},
2138
"required": ["email"]
2239
}

test/test_regression_17.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def test_class():
1212
"id": "claimed",
1313
"type": ["string", "integer", "null"],
1414
"description": "Robots Only. The human agent that has claimed this robot.",
15-
"required": False
1615
},
1716
}
1817
}

0 commit comments

Comments
 (0)