Skip to content

Instantly share code, notes, and snippets.

@ssbozy
Created March 22, 2018 18:29
Show Gist options
  • Save ssbozy/1ef63a9684271289fc773f1ec568ddd9 to your computer and use it in GitHub Desktop.
Save ssbozy/1ef63a9684271289fc773f1ec568ddd9 to your computer and use it in GitHub Desktop.
python json-schema testing
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