JSON Schema Builder

Build JSON schemas easily in Python.

Check out the source code on Github.

License Development Status Latest release Supported Python versions Supported Python implementations Download format Build status Code test coverage Code Health

Installation

pip install jsonschema-builder

Usage

import jsonschema
import jsb

schema = jsb.typed(
    jsb.schema(id='/schemas/myschema#', meta=jsb.draft04()),
    type='object',
    properties={
        'foo': jsb.typed(jsb.schema(), type='string'),
        'bar': jsb.typed(jsb.schema(), type='integer')
    }
)
data = {
    'foo': 'bar',
    'bar': 42
}

jsonschema.validate(data, schema)

Schema construction

jsb.schema.define(parent, id, schema)

Adds a schema definition to a schema.

Parameters:
  • parent (dict) – Schema that will store the new definition
  • id (str) – Definition identifier
  • schema (dict) – Schema of new definition
Return type:

dict

jsb.schema.draft04()

Return URL for the Draft 4 meta schema.

jsb.schema.ref(id)

Return a new JSON reference to a definition in a schema.

Parameters:id (str) – Definition identifier
Return type:dict
jsb.schema.schema(id=None, title=None, description=None, meta=None, scope_id='id')

Create a new schema.

Parameters:
  • id (str) – Schema identifier
  • title (str) – Schema title
  • description (str) – Schema’s description
  • meta (str) – URL to the meta schema (see draft04())
  • scope_id (str) – Key to use to store schema identifier (default: id)
Return type:

dict

Schema composition

jsb.generic.allOf(parent, *schemas)

Add a clause to validate all of the supplied schemas.

Parameters:
  • parent (dict) – Schema to add the clause to
  • schemas (List[dict]) – List of schemas
Return type:

dict

jsb.generic.anyOf(parent, *schemas)

Add a clause to validate at least one of the supplied schemas.

Parameters:
  • parent (dict) – Schema to add the clause to
  • schemas (List[dict]) – List of schemas
Return type:

dict

jsb.generic.default(parent, value)

Add a clause to use a default value if none is supplied by the data.

Parameters:
  • parent (dict) – Schema to add the clause to
  • value (Any) – Default value to use
Return type:

dict

jsb.generic.dependencies(parent, **deps)

Add a clause to make properties dependent.

Parameters:
  • parent (dict) – Schema to add the clause to
  • deps (Mapping[str, Union[dict, List[str]]]) – Dependencies definition
Return type:

dict

jsb.generic.enum(parent, *values)

Add a clause to validate that the data takes only one of the supplied values.

Parameters:
  • parent (dict) – Schema to add the clause to
  • values (List[Any]) – Authorized values for data
Return type:

dict

jsb.generic.not_(parent, schema)

Add a clause to invalidate if the supplied schemas validates.

Parameters:
  • parent (dict) – Schema to add the clause to
  • schema (dict) – Schema that must not validate the data
Return type:

dict

jsb.generic.oneOf(parent, *schemas)

Add a clause to validate only one of the supplied schemas.

Parameters:
  • parent (dict) – Schema to add the clause to
  • schemas (List[dict]) – List of schemas
Return type:

dict

jsb.generic.require(parent, *keys)

Require properties from object.

Parameters:
  • parent (dict) – Schema containing the properties
  • keys (List[str]) – Properties to require
Return type:

dict

jsb.generic.typed(parent, type, **kwargs)

Adds a type to a schema and construct it (see Primitives)

Parameters:
  • parent (dict) – Schema to type
  • type (str) – JSON primitive type name
Return type:

dict

Primitives

Primitives are called internally by the typed() function. All of their arguments are also passed from this function. You do not need to call those functions directly.

NB: If a function for a type doesn’t exist, the typed() will just add the type to the schema.

jsb.primitives.array(parent, items=None, additionalItems=None, minItems=None, maxItems=None, uniqueItems=None)
Parameters:
  • items (Union[List[dict], dict]) – Array item schema
  • additionalItems – (Dis)allow additional items on data
  • minItems (int) – Minimum number of items on data
  • maxItems – Maximum number of items on data
  • uniqueItems (bool) – Specify if all items must be unique
jsb.primitives.integer(parent, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None)
Parameters:
  • minimum (float) – Minimum value authorized
  • maximum (float) – Maximum value authorized
  • exclusiveMinimum (bool) – Is the minimum value authorized?
  • exclusiveMaximum (bool) – Is the maximum value authorized?
  • multipleOf (float) – Number that must be a divisor of the supplied data
jsb.primitives.number(parent, minimum=None, maximum=None, exclusiveMinimum=None, exclusiveMaximum=None, multipleOf=None)
Parameters:
  • minimum (float) – Minimum value authorized
  • maximum (float) – Maximum value authorized
  • exclusiveMinimum (bool) – Is the minimum value authorized?
  • exclusiveMaximum (bool) – Is the maximum value authorized?
  • multipleOf (float) – Number that must be a divisor of the supplied data
jsb.primitives.object(parent, properties=None, additionalProperties=None, patternProperties=None, minProperties=None, maxProperties=None)
Parameters:
  • properties (Mapping[str, dict]) – Object properties
  • additionalProperties (Union[bool, dict]) – (Dis)allow additional properties on data
  • patternProperties (Mapping[str, dict]) – Object properties that match a regular expression
  • minProperties (int) – Minimum number of properties on data
  • maxProperties – Maximum number of properties on data
jsb.primitives.string(parent, minLength=None, maxLength=None, pattern=None, format=None)
Parameters:
  • minLength (int) – Minimum length of string
  • maxLength (int) – Maximum length of string
  • pattern (str) – Regular expression the string must validate
  • format (str) – String format (see JSON schema specification)