I'm trying to recursively validate a custom JSON schema against a template JSON Schema using the jsonschema
module in Python 3.
The custom JSON looks like this:
{
"endpoint": "rfc",
"filter_by": ["change_ref", "change_i"],
"expression": [
{
"field": "first_name",
"operator": "EQ",
"value": "O'Neil"
},
"AND",
[
{
"field": "last_name",
"operator": "NEQ",
"value": "Smith"
},
"OR",
{
"field": "middle_name",
"operator": "EQ",
"value": "Sam"
}
]
],
"limit_results_to": "2"
}
The above can be generalized further by adding multiple AND
s and OR
s => my question related to recursivity.
The template that I'm trying to validate this schema against of is in the following piece of code:
import json
import jsonschema
def get_data(file):
with open(file) as data_file:
return json.load(data_file)
def json_schema_is_valid():
data = get_data("other.json")
valid_schema = {
"type": "object",
"required": ["endpoint", "filter_by", "expression", "limit_results_to"],
"properties": {
"endpoint": {
"type": "string",
"additionalProperties": False
},
"filter_by": {
"type": ["string", "array"],
"additionalProperties": False
},
"limit_results_to": {
"type": "string",
"additionalProperties": False
},
"expression": {
"type": "array",
"properties": {
"field": {
"type": "string",
"additionalProperties": False
},
"operator": {
"type": "string",
"additionalProperties": False
},
"value": {
"type": "string",
"additionalProperties": False
}
},
"required": ["field", "operator", "value"]
}
}
}
return jsonschema.validate(data, valid_schema)
if __name__ == '__main__':
print(json_schema_is_valid())
Now, something seems wrong because when I run the above code, I get None
which might (not) be okay. When I'm trying to modify the type
of a property
in something that isn't allowed, I don't get any excception. Is there something wrong in my template? Here, it looks like the expression properties are not parsed. More, I read here that I can make my template to recursively validate my custom JSON schema using '$ref': '#'
but I didn't quite understand how to use it. Could someone give me some hints?