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
       
tests (package)
validator

 
Classes
       
jsonschema.validator.JSONSchemaValidator

 
class JSONSchemaValidator
    Implementation of the json-schema validator that adheres to the 
JSON Schema Proposal 2nd Draft.
 
  Methods defined here:
__init__(self, interactive_mode=True)
validate(self, data, schema)
Validates a piece of json data against the provided json-schema.
validate_additionalProperties(self, x, fieldname, schema, additionalProperties=None)
Validates additional properties of a JSON object that were not
specifically defined by the properties property
validate_default(self, x, fieldname, schema, default=None)
Adds default data to the original json document if the document is
not readonly
validate_description(self, x, fieldname, schema, description=None)
validate_disallow(self, x, fieldname, schema, disallow=None)
Validates that the value of the given field does not match the
disallowed type.
validate_enum(self, x, fieldname, schema, options=None)
Validates that the value of the field is equal to one of the
specified option values
validate_extends(self, x, fieldname, schema, extends=None)
validate_format(self, x, fieldname, schema, format=None)
Validates that the value of the field matches the predifined format
specified.
validate_hidden(self, x, fieldname, schema, hidden=False)
validate_id(self, x, fieldname, schema, ID=None)
Validates a schema id and adds it to the schema reference map
validate_identity(self, x, fieldname, schema, unique=False)
validate_items(self, x, fieldname, schema, items=None)
Validates that all items in the list for the given field match the
given schema
validate_maxDecimal(self, x, fieldname, schema, maxdecimal=None)
Validates that the value of the given field has less than or equal
to the maximum number of decimal places given
validate_maxItems(self, x, fieldname, schema, maxitems=None)
Validates that the number of items in the given field is equal to or
less than the maximum amount.
validate_maxLength(self, x, fieldname, schema, length=None)
Validates that the value of the given field is shorter than or equal
to the specified length if a string
validate_maximum(self, x, fieldname, schema, maximum=None)
Validates that the field is shorter than or equal to the maximum
length if specified.
validate_minItems(self, x, fieldname, schema, minitems=None)
Validates that the number of items in the given field is equal to or
more than the minimum amount.
validate_minLength(self, x, fieldname, schema, length=None)
Validates that the value of the given field is longer than or equal
to the specified length if a string
validate_minimum(self, x, fieldname, schema, minimum=None)
Validates that the field is longer than or equal to the minimum
length if specified
validate_optional(self, x, fieldname, schema, optional=False)
Validates that the given field is present if optional is false
validate_options(self, x, fieldname, schema, options=None)
validate_pattern(self, x, fieldname, schema, pattern=None)
Validates that the given field, if a string, matches the given
regular expression.
validate_properties(self, x, fieldname, schema, properties=None)
Validates properties of a JSON object by processing the object's
schema recursively
validate_readonly(self, x, fieldname, schema, readonly=False)
validate_requires(self, x, fieldname, schema, requires=None)
validate_title(self, x, fieldname, schema, title=None)
validate_transient(self, x, fieldname, schema, transient=False)
validate_type(self, x, fieldname, schema, fieldtype=None)
Validates that the fieldtype specified is correct for the given
data

 
Functions
       
validate(data, schema, validator_cls=None, interactive_mode=True)
Validates a parsed json document against the provided schema. If an
error is found a ValueError is raised.
 
``data`` is a python dictionary object of parsed json data.
 
``schema`` is a python dictionary object of the parsed json schema.
 
If ``validator_cls`` is provided that class will be used to validate
the given ``schema`` against the given ``data``. The given class should
be a subclass of the JSONSchemaValidator class.
 
``interactive_mode`` is a boolean value specifying if the data should
be validated in interactive mode. Validating in interactive mode will
allow the validator to make changes to the given json ``data`` object
to put in place default values specified in the given ``schema``
object.

 
Data
        __all__ = ['validate', 'JSONSchemaValidator']
__version__ = '0.1a'