Basic Usage

All imports are from:

from json_schema_toolkit.document import ...

JSON Document

Extends json_document Document (and hence DocumentFragment) in order to build the schema automatically through introspection. The schema is generated by each field present in the Document. Fields of type ‘object’ and ‘array’ recursively generate sub-schemas.

The schema parameters (such as ‘title’) for the Document are provided in a ‘Meta’ class; the base Document always describes a JSON type ‘object’. If no fields are specified, the resulting Document’s type is ‘any’.

Example empty JSONDocument:

class EmptyDocument(JSONDocument):

    class Meta(object):
        title = u'a title'
        description = u'a description'

d1 = EmptyDocument({})

JSON Document Field

Extensible fields of various kinds (including non-native JSON types which can be converted to a standard string-based representation such as date and time). Fields can be added to a JSONDocument as well as the content of other fields which expect them (JSONObjectField and JSONListField). The constructor arguments for fields are used to parametrize the schema they each generate.

Example of a simple ‘integer’ field:

class SimpleDocument(JSONDocument):

    answer = JSONIntegerField(title = u'the answer',
        description = u'answer mutually exclusive with question in each given universe')

    class Meta(object):
        title = u'oracle'
        description = u'a repository of truth'

d1 = SimpleDocument({ 'answer' : 42 })

Example of an ‘array’ type wich contains ‘string’ elements:

class ListDocument(JSONDocument):

    events = JSONListField(title = u'events',
        description = u'important historical events', content = [
            JSONStringField(title = u'event',
                description = u'important historical event'),
        ])

    class Meta(object):
        title = u'history'
        description = u'a collection of historical events'

d1 = ListDocument({ 'events' : [
    u'Sinking of Atlantis',
    u'Discovery of Atlantis',
    u'Colonization of Atlantis',
] })

Example of an ‘object’ type which contains a combination of other fields:

class ObjectDocument(JSONDocument):

    events = JSONListField(title = u'events',
        description = u'important historical events', content = [
            JSONObjectField(title = u'event',
                description = u'important historical event', content = {
                    'title' : JSONStringField(title = u'event title'),
                    'importance' : JSONIntegerField(title = u'event importance'),
                }),
        ])

    class Meta(object):
        title = u'history'
        description = u'a collection of historical events'

d1 = ObjectDocument({ 'events' : [
    { 'title' : u'Sinking of Atlantis', 'importance' : 3 },
    { 'title' : u'Discovery of Atlantis', 'importance' : 7 },
    { 'title' : u'Colonization of Atlantis', 'importance' : 12 },
] })

Fragment Proxy

Provides a Pythonic API to composite fields, so members can be accessed through the dot notation. The relevant fragment can be obtained through the ‘_fragment’ property.