Closed aw32 closed 1 year ago
from jsonschema import validate
schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "test.schema.json",
"title": "Test schema",
"description": "Test schema description",
"oneOf": [
{
"$anchor": "rule",
"type": "object",
"properties": {
"name": {
"description": "The unique name for a rule",
"type": "string"
}
},
"required": ["name"]
},
{
"type": "array",
"items": {
"$ref": "#rule"
}
}
]
}
validate(
instance={ "name": "foobar" },
schema=schema,
)
This code produces no errors with either of your examples. Can you explain what behavior you're expecting? Those look valid under the schema you showed to me.
Oh, this is interesting. For me the example
instance=[{ "name": "foobar" },{"name":"asdf"}],
gives the forementioned error message.
Can you explain what behavior you're expecting?
I expect both examples to be valid according to the schema.
I don't see what code you're running -- though now I notice you're saying you're using check-jsonschema
-- if that's where you're seeing this behavior and you check you're on a recent version this likely would belong on that repo.
I confused the Python package location and used an outdated version of jsonschema. I updated to the newest version and now it works. Thanks for the very fast response. ❤️
I tried to create a schema using
oneOf
containing an array subschema and an object subschema. To avoid the redundant definition of the contained objects, I wanted to reference the object subschema inside of the array subschema:It seems, the subschema with the
$anchor
rule
is ignored, when evaluating the$ref
#rule
. The test JSON would be for the array:And for the object:
Using
check-jsonschema --schemafile test.schema.json a.json
with the array example gives the following error:Is this a bug in
python-jsonschema
or is my schema invalid?