From 288620719b2c395f554cff9f56834107de11d4b3 Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Thu, 26 Aug 2021 09:48:32 +0100 Subject: [PATCH] Add test cases for error details hidden behind a $ref These indeed can be improved, as mentioned in https://github.com/Julian/jsonschema/pull/817#issuecomment-881550313 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. --- jsonschema/tests/test_validators.py | 62 +++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py index 915758fef..30f07af42 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -1325,6 +1325,68 @@ def test_contains_none(self): ), ) + def test_ref_sibling(self): + schema = { + "$defs": {"foo": {"required": ["bar"]}}, + "properties": { + "aprop": { + "$ref": "#/$defs/foo", + "required": ["baz"] + }, + }, + } + + validator = validators.Draft202012Validator(schema) + e1, e2 = validator.iter_errors({"aprop": {}}) + self.assertEqual( + ( + e1.message, + e1.validator, + e1.validator_value, + e1.instance, + e1.absolute_path, + e1.schema, + e1.schema_path, + e1.relative_schema_path, + e1.json_path, + ), + ( + "'bar' is a required property", + "required", + ["bar"], + {}, + deque(["aprop"]), + {"required": ["bar"]}, + deque(["properties", "aprop", "required"]), + deque(["properties", "aprop", "required"]), + "$.aprop", + ), + ) + self.assertEqual( + ( + e2.message, + e2.validator, + e2.validator_value, + e2.instance, + e2.absolute_path, + e2.schema, + e2.schema_path, + e2.relative_schema_path, + e2.json_path, + ), + ( + "'baz' is a required property", + "required", + ["baz"], + {}, + deque(["aprop"]), + {"$ref": "#/$defs/foo", "required": ["baz"]}, + deque(["properties", "aprop", "required"]), + deque(["properties", "aprop", "required"]), + "$.aprop", + ), + ) + class MetaSchemaTestsMixin(object): # TODO: These all belong upstream