-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.txt
77 lines (58 loc) · 1.94 KB
/
README.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
INTRO
jsonschema is a full featured validator for the JSON Schema specification.
It conforms to the JSON Schema Proposal Second Draft which can be found at the
following url:
http://groups.google.com/group/json-schema/web/json-schema-proposal---second-draft
INSTALL
jsonschema uses setup tools so it can be installed normally using:
python setup.py install
Furthermore, the test suite can be run by using the following command:
python setup.py test
USAGE
JSON documents and schema must first be loaded into a python dictionary type
before it can be validated. This can be done with the JSON parser of your choice
but I will use simplejson (just because).
Parsing a simple JSON document
>>> import jsonschema
>>> jsonschema.validate("simplejson", {"type":"string"})
Parsing a more complex JSON document.
>>> import simplejson
>>> import jsonschema
>>>
>>> data = simplejson.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
>>> schema = {
... "type":"array",
... "items":[
... {"type":"string"},
... {"type":"object",
... "properties":{
... "bar":{
... "items":[
... {"type":"string"},
... {"type":"any"},
... {"type":"number"},
... {"type":"integer"}
... ]
... }
... }
... }
... ]
... }
>>> jsonschema.validate(data,schema)
Handling validation errors
ValueErrors are thrown when validation errors occur.
>>> import jsonschema
>>> try:
... jsonschema.validate("simplejson", {"type":"string","minLength":15})
... except ValueError as e:
... print(e)
...
Length of 'simplejson' must be more than 15.000000
EXTENDING JSONSCHEMA
jsonschema provides an API similar to simplejson in that validators can be
overridden to support special property support or extended functionality.
Samples of how jsonschema can be extended can be found in the examples
directory.
LIMITATIONS
References are currently not supported.
The unique property is currently not validated.