Created
June 24, 2014 23:58
-
-
Save wshayes/7bb4118cfe21b962cdc1 to your computer and use it in GitHub Desktop.
python jsonschema validation error for top-level oneOf construct
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
肤错过