Skip to content

Instantly share code, notes, and snippets.

@zhangcheng
Created October 16, 2013 06:31
Show Gist options
  • Save zhangcheng/7003452 to your computer and use it in GitHub Desktop.
Save zhangcheng/7003452 to your computer and use it in GitHub Desktop.
import json
from jsonschema import validate
str_schema = '''
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product set",
"type": "array",
"items": {
"title": "Product",
"type": "object",
"properties": {
"id": {
"description": "The unique identifier for a product",
"type": "number"
},
"name": {
"type": "string"
}
},
"required": ["id", "name"]
}
}
'''
str_p1 = '''
[
{
"id": 2,
"name": "An ice sculpture"
}
]
'''
str_p2 = '''
[
{
"id": 1,
"name": "An ice"
},
{
"id": 2,
"name": "An ice sculpture"
}
]
'''
str_p3 = '''
[
{
"id": 2,
"name2": "An ice sculpture"
}
]
'''
schema = json.loads(str_schema)
p1 = json.loads(str_p1)
validate(p1, schema)
p2 = json.loads(str_p2)
validate(p2, schema)
p3 = json.loads(str_p3)
validate(p3, schema)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment