Skip to content

Instantly share code, notes, and snippets.

@wshayes
Created June 24, 2014 23:58
Show Gist options
  • Select an option

  • Save wshayes/7bb4118cfe21b962cdc1 to your computer and use it in GitHub Desktop.

Select an option

Save wshayes/7bb4118cfe21b962cdc1 to your computer and use it in GitHub Desktop.
python jsonschema validation error for top-level oneOf construct
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import jsonschema
schema_json = '''
{
"$schema": "http://json-schema.org/draft-04/schema",
"oneOf": [{
"type": "object",
"properties": {
"graph": {
"$ref": "#/definitions/graph"
}
},
"additionalProperties": false,
"required": ["graph"]
}, {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"graphs": {
"type": "array",
"items": {
"$ref": "#/definitions/graph"
}
}
},
"additionalProperties": false
}],
"definitions": {
"graph": {
"type": "object",
"additionalProperties": false,
"required": ["id"],
"properties": {
"id": {
"type": "string"
},
"label": {
"type": "string"
}
}
}
}
}
'''
working_network_json = '''
{
"graph": {
"id": "testid",
"label": "testlabel"
}
}
'''
bad_network_json = '''
{
"graph": {
"label": "testlabel"
}
}
'''
def main():
working_network = json.loads(working_network_json)
bad_network = json.loads(bad_network_json)
schema = json.loads(schema_json)
print "Validating working network"
jsonschema.validate(working_network, schema)
print "Validating bad network"
jsonschema.validate(bad_network, schema)
if __name__ == '__main__':
main()
@yjdzh
Copy link

yjdzh commented Jun 25, 2014

肤错过

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment