JSON Schema Validation

JSON schema validator for python

Note

Only a subset of schema features are currently supported. Unsupported features are detected and raise a NotImplementedError when you call Validator.validate()

Warning

This implementation was based on the second draft of the specification A third draft was published on the 22nd Nov 2010. This draft introduced several important changes that are not yet implemented.

See also

http://json-schema.org/ for details about the schema

class linaro_json.schema.Schema(json_obj)

JSON schema object

enum

Enumeration of possible correct values.

Must be either None or a non-empty list of valid objects. The list must not contain duplicate elements.

pattern

Note

JSON schema specifications says that this value SHOULD follow the EMCA 262/Perl 5 format. We cannot support this so we support python regular expressions instead. This is still valid but should be noted for clarity.

:returns None or compiled regular expression

properties

The properties property contains schema for each property in a dictionary.

The dictionary name is the property name. The dictionary value is the schema object itself.

type

Return the ‘type’ property of this schema.

The return value is always a list of correct JSON types. Correct JSON types are one of the pre-defined simple types or another schema object.

List of built-in simple types: * ‘string’ * ‘number’ * ‘integer’ * ‘boolean’ * ‘object’ * ‘array’ * ‘any’ (default)

exception linaro_json.schema.SchemaError

A bug in the schema prevents the program from working

exception linaro_json.schema.ValidationError(message, new_message=None, object_expr=None, schema_expr=None)

A bug in the validated object prevents the program from working.

The error instance has several interesting properties:

message:Old and verbose message that contains less helpful message and lots of JSON data (deprecated).
new_message:short and concise message about the problem
object_expr:A JavaScript expression that evaluates to the object that failed to validate. The expression always starts with a root object called 'object'.
schema_expr:A JavaScript expression that evaluates to the schema that was checked at the time validation failed. The expression always starts with a root object called 'schema'.
class linaro_json.schema.Validator

JSON Schema validator.

Can be used to validate any JSON document against a Schema.

classmethod validate(schema, obj)

Validate specified JSON object obj with specified Schema instance schema.

Parameters:
  • schema (Schema) – Schema to validate against
  • obj – JSON Document to validate
Return type:

bool

Returns:

True on success

Raises:
  • ValidationError – if the object does not match schema.
  • SchemaError – if the schema itself is wrong.
linaro_json.schema.validate(schema_text, data_text)

Validate specified JSON text (data_text) with specified schema (schema text). Both are converted to JSON objects with simplesjon.loads().

Previous topic

Code Documentation

Next topic

Version History

This Page