Created
March 22, 2018 18:29
-
-
Save ssbozy/1ef63a9684271289fc773f1ec568ddd9 to your computer and use it in GitHub Desktop.
python json-schema testing
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
import jsonschema | |
import sys | |
# A sample schema, like what we'd get from json.load() | |
schema = { | |
"type" : "object", | |
"properties" : { | |
"price" : {"type" : "number"}, | |
"name" : {"type" : "string"}, | |
"details":{ | |
"type":"object", | |
"properties":{ | |
"company":{"type":"string", "description": "Name of supplying company",}, | |
"aisle":{"type":"string"} | |
}, | |
"required": ["company"] | |
} | |
}, | |
"required": ["name", "details"] | |
} | |
jsondata = {"name" : "Eggs", "price" : 34.99, "details":{"company":"New Egg", "aisle":"L21"}} | |
try: | |
jsonschema.validate(jsondata, schema) | |
except jsonschema.exceptions.ValidationError: | |
print sys.exc_info()[1] | |
print "Validation failed" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment