Skip to content

Commit 6d95dd6

Browse files
authored
Merge pull request #84 from EdoPut/i-will-find-your-errors
Travel all the errors in report
2 parents 8c26cd6 + 325b511 commit 6d95dd6

2 files changed

Lines changed: 131 additions & 1 deletion

File tree

netjsonconfig/exceptions.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1+
from functools import reduce
2+
3+
4+
def list_error_in_subschema(jsonschema_error):
5+
sub_errors = []
6+
for validator_value, error in zip(jsonschema_error.validator_value, jsonschema_error.context):
7+
sub_errors.append((validator_value, error.message))
8+
if error.context:
9+
sub_errors += list_error_in_subschema(error)
10+
return sub_errors
11+
12+
113
class NetJsonConfigException(Exception):
214
"""
315
Root netjsonconfig exception
416
"""
517
def __str__(self):
6-
return "%s %s %s" % (self.__class__.__name__, self.message, self.details)
18+
suberrors = list_error_in_subschema(self.details)
19+
20+
default_message = "%s %s\n" % (self.__class__.__name__, self.details,)
21+
suberror_fmt = '\nAgainst schema %s\n%s\n'
22+
suberror_message = reduce(lambda x, y: x + suberror_fmt % y, suberrors, '')
23+
24+
return default_message + suberror_message
725

826

927
class ValidationError(NetJsonConfigException):

tests/test_jsonschema_error.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import unittest
2+
3+
from jsonschema import ValidationError, validate
4+
5+
from netjsonconfig.exceptions import list_error_in_subschema
6+
7+
schema = {
8+
'$schema': 'http://json-schema.org/draft-04/schema#',
9+
'type': 'object',
10+
'additionalProperties': True,
11+
'definitions': {
12+
'spam_object': {
13+
'additionalProperties': True,
14+
'required': [
15+
'spam',
16+
],
17+
'properties': {
18+
'spam': {
19+
'type': 'string',
20+
},
21+
},
22+
},
23+
'eggs_object': {
24+
'additionalProperties': True,
25+
'required': [
26+
'eggs',
27+
],
28+
'properties': {
29+
'eggs': {
30+
'type': 'boolean',
31+
},
32+
},
33+
},
34+
},
35+
'properties': {
36+
'test_object': {
37+
'type': 'object',
38+
'oneOf': [
39+
{'$ref': '#/definitions/spam_object'},
40+
{'$ref': '#/definitions/eggs_object'},
41+
],
42+
}
43+
},
44+
}
45+
46+
47+
class TestJsonSchema(unittest.TestCase):
48+
"""
49+
tests ValidationError helpers
50+
"""
51+
52+
def test_spam_object(self):
53+
test_i = {
54+
'test_object': {
55+
'spam': 'lots of',
56+
},
57+
}
58+
59+
validate(test_i, schema)
60+
61+
def test_eggs_object(self):
62+
test_i = {
63+
'test_object': {
64+
'eggs': True,
65+
},
66+
}
67+
68+
validate(test_i, schema)
69+
70+
def test_burrito_object(self):
71+
test_i = {
72+
'test_object': {
73+
'burrito': 'yes',
74+
},
75+
}
76+
77+
self.assertRaises(ValidationError, validate, test_i, schema)
78+
79+
def test_burrito_error_message(self):
80+
test_i = {
81+
'test_object': {
82+
'burrito': 'yes',
83+
},
84+
}
85+
86+
with self.assertRaises(ValidationError) as error_container:
87+
validate(test_i, schema)
88+
89+
message_list = [
90+
"'spam' is a required property",
91+
"'eggs' is a required property",
92+
]
93+
94+
self.assertEqual([e.message for e in error_container.exception.context], message_list)
95+
96+
def test_list_error_in_subschema(self):
97+
test_i = {
98+
'test_object': {
99+
'burrito': 'yes',
100+
},
101+
}
102+
103+
with self.assertRaises(ValidationError) as error_container:
104+
validate(test_i, schema)
105+
106+
suberror_list = [
107+
({'$ref': '#/definitions/spam_object'}, "'spam' is a required property"),
108+
109+
({'$ref': '#/definitions/eggs_object'}, "'eggs' is a required property"),
110+
]
111+
112+
self.assertEqual(list_error_in_subschema(error_container.exception), suberror_list)

0 commit comments

Comments
 (0)