I have the following JSON schema (testschema.json
) that I am testing in Python3.
I keep getting a validation error for property "alpha"
and I have tried declaring the property in various ways to no avail.
{
"id": "testschema",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "test schema",
"type": "object",
"properties": {
"traffic_parameters": {
"properties": {
"test_type": {
"type": "string",
"enum": ["AA", "BB"]
},
"capacity": {
"oneOf": [
{
"properties": {
"min_percentage": {
"type": "integer",
"minimum" : 1,
"maximum" : 150,
"additionalProperties": false
},
"max_percentage": {
"type": "integer",
"minimum" : 1,
"maximum" : 150,
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"properties": {
"percentage_range": {
"type": "array",
"minItems": 1,
"maxItems": 10,
"items": {
"type": "integer"
},
"additionalProperties": false
}
},
"additionalProperties": false
}
]
}
},
"additionalProperties": false
},
"alpha": {
"properties": {
"beta": {
"oneOf": [
{
"properties": {
"AA": {
"a": [90, 95],
"b": [4, 8],
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"properties": {
"BB": {
"a": [100],
"b": [0],
"c": [0],
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"properties": {
"CC": {
"a": [50],
"b": [50],
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"properties": {
"DD": {
"a": [0],
"b": [0],
"c": [100],
"additionalProperties": false
}
},
"additionalProperties": false
}
]
}
},
"additionalProperties": false
}
},
"required": ["traffic_parameters", "alpha"]
}
Here is the test JSON (test.json
) that uses this schema.
{
"traffic_parameters": {
"test_type": "BB",
"capacity": {
"percentage_range": [1,2,3,4,5,6]
}
},
"alpha": {
"beta": "AA"
}
}
When I validate this using the Python3 jsonschema
module
with open("testschema.json") as schema_file:
test_schema = json.load(schema_file)
schema_file.close()
with open("test.json") as json_file:
test_json = json.load(json_file)
json_file.close()
validate(test_json, test_schema)
I get an error on the alpha
property which is similar in structure to traffic_parameters
which doesn't error.
jsonschema.exceptions.ValidationError: 'AA' is valid under each of {'properties': {'BB': ...
Here is the output
E
======================================================================
ERROR: test_valid__JSON_against_schema (__main__.SchemaTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line 35, in test_valid__JSON_against_schema
validate(test_json, test_schema)
File "/local/tools/PACKAGES/pyhton3/lib/python3.6/site-packages/jsonschema/validators.py", line 541, in validate
cls(schema, *args, **kwargs).validate(instance)
File "/local/tools/PACKAGES/pyhton3/lib/python3.6/site-packages/jsonschema/validators.py", line 130, in validate
raise error
jsonschema.exceptions.ValidationError: 'AA' is valid under each of {'properties': {'BB': {'a': [100], 'b': [0], 'c': [0], 'additionalProperties': False}}, 'additionalProperties': False}, {'properties': {'CC': {'a': [50], 'b': [50], 'additionalProperties': False}}, 'additionalProperties': False}, {'properties': {'DD': {'a': [0], 'b': [0], 'c': [100], 'additionalProperties': False}}, 'additionalProperties': False}, {'properties': {'AA': {'a': [90, 95], 'b': [4, 8], 'additionalProperties': False}}, 'additionalProperties': False}
Failed validating 'oneOf' in schema['properties']['alpha']['properties']['beta']:
{'oneOf': [{'additionalProperties': False,
'properties': {'AA': {'a': [90, 95],
'additionalProperties': False,
'b': [4, 8]}}},
{'additionalProperties': False,
'properties': {'BB': {'a': [100],
'additionalProperties': False,
'b': [0],
'c': [0]}}},
{'additionalProperties': False,
'properties': {'CC': {'a': [50],
'additionalProperties': False,
'b': [50]}}},
{'additionalProperties': False,
'properties': {'DD': {'a': [0],
'additionalProperties': False,
'b': [0],
'c': [100]}}}]}
On instance['alpha']['beta']:
'AA'
----------------------------------------------------------------------
Ran 1 test in 0.007s
FAILED (errors=1)
Does anyone know why this is and how to fix it?
Thanks.