Created
October 16, 2013 06:31
-
-
Save zhangcheng/7003452 to your computer and use it in GitHub Desktop.
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 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