Skip to content

Commit 95ffe2d

Browse files
committed
Don't include if in the schema_path of errors.
Closes: #696 This is definitely not the right general solution though. That was obvious before given that $ref needed special casing, but it's even more obvious now. As-is, downstream implementers of new validator keywords have no way to get this kind of behavior. It's additional complexity that's been introduced in modern specs, but it seems likely that a generic mechanism now needs adding for allowing keywords to more cleanly ignore themselves in schema paths, or otherwise do more complex manipulation of the schema path for an error.
1 parent 64619d3 commit 95ffe2d

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

jsonschema/tests/test_validators.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ def test_if_then(self):
10621062
self.assertEqual(error.message, "13 was expected")
10631063
self.assertEqual(error.path, deque([]))
10641064
self.assertEqual(error.json_path, '$')
1065-
self.assertEqual(error.schema_path, deque(["if", "then", "const"]))
1065+
self.assertEqual(error.schema_path, deque(["then", "const"]))
10661066

10671067
def test_if_else(self):
10681068
schema = {
@@ -1077,7 +1077,7 @@ def test_if_else(self):
10771077
self.assertEqual(error.message, "13 was expected")
10781078
self.assertEqual(error.path, deque([]))
10791079
self.assertEqual(error.json_path, '$')
1080-
self.assertEqual(error.schema_path, deque(["if", "else", "const"]))
1080+
self.assertEqual(error.schema_path, deque(["else", "const"]))
10811081

10821082
def test_boolean_schema_False(self):
10831083
validator = validators.Draft7Validator(False)

jsonschema/validators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def iter_errors(self, instance, _schema=None):
333333
instance=instance,
334334
schema=_schema,
335335
)
336-
if k != u"$ref":
336+
if k not in {u"if", u"$ref"}:
337337
error.schema_path.appendleft(k)
338338
yield error
339339
finally:

0 commit comments

Comments
 (0)