jsonschema (version 0.1a) | index |
A complete, full-featured validator for JSON Schema
JSON Schema validation is based on the specifications of the the
JSON Schema Proposal Second Draft
(http://groups.google.com/group/json-schema/web/json-schema-proposal---second-draft).
jsonschema provides an API similar to simplejson in that validators can be
overridden to support special property support or extended functionality.
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, e:
... print e.message
...
Length of value 'simplejson' for field '_data' must be more than or equal to 15.000000
Running from the command line
% echo '{"type":"string"}' > schema.json
% echo '"mystring"' | python -mjsonschema schema.json
% echo '"mystring"' > data.json
% python -mjsonschema schema.json data.json
Package Contents | ||||||
|
Classes | ||||||||||
|
Functions | ||
|
Data | ||
__all__ = ['validate', 'JSONSchemaValidator'] __version__ = '0.1a' |