Description
I would like to use a schema base.json
that defines basic properties together with a schema derived.json
which adds further validation to the base schema. The derived schema needs to reference the base schema using a relative path as discussed in #98. However, I cannot get both the relative reference and within-document references to work at the same time. Here's a minimum example to illustrate the problem.
base.json
{
"definitions": {
"string_alias": {
"type": "string"
}
},
"properties": {
"my_string": {
"$ref": "#/definitions/string_alias"
}
}
}
derived.json
{
"allOf": [
{
"$ref": "base.json"
},
{
"properties": {
"my_number": {
"type": "number"
}
}
}
]
}
data.json
{
"my_string": "Hello world!",
"my_number": 42
}
validate.py
import json
import os
import sys
import jsonschema
if len(sys.argv) > 1:
resolver = jsonschema.RefResolver('file://%s/' % os.path.abspath(os.path.dirname(__file__)), None)
else:
resolver = None
with open('base.json') as fp:
base = json.load(fp)
with open('derived.json') as fp:
derived = json.load(fp)
with open('data.json') as fp:
data = json.load(fp)
try:
jsonschema.validate(data, base, resolver=resolver)
print("Passed base schema.")
except Exception as ex:
print("Failed base schema: %s" % ex)
try:
jsonschema.validate(data, derived, resolver=resolver)
print("Passed derived schema.")
except Exception as ex:
print("Failed derived schema: %s" % ex)
If I use the resolver, the relative path gets resolved correctly but the within-document reference fails. If I don't use the resolver, the within-document reference succeeds but the relative path does not get resolved.
# With resolver
python validate.py resolver
Failed base schema: Unresolvable JSON pointer: 'definitions/string_alias'
Passed derived schema.
# Without resolver
python validate.py
Passed base schema.
Failed derived schema: unknown url type: 'base.json'
Any suggestions on how to get both to work?
Edit: Looks like this may be related to #306.
Edit: A temporary workaround is to have base.json
reference itself explicitly, i.e. use base.json#/definitions/string_alias
instead of #/definitiosn/string_alias
.