Skip to content

Commit 2886207

Browse files
committed
Add test cases for error details hidden behind a $ref
These indeed can be improved, as mentioned in #817 (comment) but it's a bit less clear exactly how yet -- rather than putting $ref in the schema path, instead using relative_schema_path to only refer to the schema post-$ref lookup is a bit more consistent with the current norms, wherein what's in schema_path should be lookup-able via indexing. But for now, they're distinguishable via .schema, which shows only the $ref'ed schema for the second error.
1 parent 996437f commit 2886207

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

jsonschema/tests/test_validators.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,68 @@ def test_contains_none(self):
13251325
),
13261326
)
13271327

1328+
def test_ref_sibling(self):
1329+
schema = {
1330+
"$defs": {"foo": {"required": ["bar"]}},
1331+
"properties": {
1332+
"aprop": {
1333+
"$ref": "#/$defs/foo",
1334+
"required": ["baz"]
1335+
},
1336+
},
1337+
}
1338+
1339+
validator = validators.Draft202012Validator(schema)
1340+
e1, e2 = validator.iter_errors({"aprop": {}})
1341+
self.assertEqual(
1342+
(
1343+
e1.message,
1344+
e1.validator,
1345+
e1.validator_value,
1346+
e1.instance,
1347+
e1.absolute_path,
1348+
e1.schema,
1349+
e1.schema_path,
1350+
e1.relative_schema_path,
1351+
e1.json_path,
1352+
),
1353+
(
1354+
"'bar' is a required property",
1355+
"required",
1356+
["bar"],
1357+
{},
1358+
deque(["aprop"]),
1359+
{"required": ["bar"]},
1360+
deque(["properties", "aprop", "required"]),
1361+
deque(["properties", "aprop", "required"]),
1362+
"$.aprop",
1363+
),
1364+
)
1365+
self.assertEqual(
1366+
(
1367+
e2.message,
1368+
e2.validator,
1369+
e2.validator_value,
1370+
e2.instance,
1371+
e2.absolute_path,
1372+
e2.schema,
1373+
e2.schema_path,
1374+
e2.relative_schema_path,
1375+
e2.json_path,
1376+
),
1377+
(
1378+
"'baz' is a required property",
1379+
"required",
1380+
["baz"],
1381+
{},
1382+
deque(["aprop"]),
1383+
{"$ref": "#/$defs/foo", "required": ["baz"]},
1384+
deque(["properties", "aprop", "required"]),
1385+
deque(["properties", "aprop", "required"]),
1386+
"$.aprop",
1387+
),
1388+
)
1389+
13281390

13291391
class MetaSchemaTestsMixin(object):
13301392
# TODO: These all belong upstream

0 commit comments

Comments
 (0)