I have two JSON schemas - publisher and article, such that there could be multiple articles in a single publisher.
This is the article schema (in a.json):
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Article",
"type": "object",
"properties": {
"aid": {
"type": "integer"
},
"author": {
"type": "string"
},
"title": {
"type": "string"
}
},
"required": ["aid", "author", "title"]
}
And I'm trying to reference this in publisher schema as below (in p.json, in the same dir):
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Publisher",
"type": "object",
"properties": {
"pid": {
"type": "integer"
},
"name": {
"type": "integer"
},
"lang": {
"type": "string"
},
"articles": {
"type": "array",
"uniqueItems": true,
"items": {
"type": "object",
"$ref": "./a.json"
}
}
},
"required": ["pid", "articles"]
}
I would ideally want the data to raise error if any of the articles do not have the required fields as mentioned in a.json. But this does not seem to happen:
import json, jsonschema
schema = json.load(open("p.json"))
data = {
"pid": 1,
"articles": [
{
"aid": 100,
"title": "test",
}
]
}
jsonschema.validate(data, schema)
The last line should raise an exception, as article does not have the author key that is marked as a requierd field in a.json.
But somehow, it raises _WrappedReferencingError: Unresolvable: ./a.json exception.
How can I reference a.json here such that it raise the correct exception?
The way you outlined your schemas is correct. Your error is the trailing comma on
title. This is invalid JSON itself, not even JSON Schema.