API

JSON schema builder.

schemabuilder.Str, schemabuilder.Bool, schemabuilder.Number, schemabuilder.Int, schemabuilder.Object and schemabuilder.Array should be used to define schema.

schemabuilder.Schema should be used to collect schema and validate against them.

Primitives

Read this guide to schema type to learn about JSON schema types and their properties.

class schemabuilder.primitives.Generic(**kw)

Base schema class

Defines generic object with the default attribute most other json object accept, including description, title, default or enum.

Parameters:
  • id – schema id.
  • desc – schema description.
  • title – schema title.
  • default – default value (ignored by most validator).
  • enum – list of valid value.
  • required – marks the schema as required.
  • format – value format (like uri or email).
  • type – schema type.
  • one_of – list of schema. The value must validate against one of them.
  • any_of – list of schema. The value must validate against one or many of the schema.
  • all_of – list of schema. The value must validate against all of them.
to_dict()

Return the schema as a dict, ready to be serialized by json.

class schemabuilder.Str(**kw)

Bases: schemabuilder.primitives.Generic

a String type with its optional min/max length and pattern attributes.

Parameters:
  • min – minimum length of the string.
  • max – maximum length of the string.
  • pattern – regex pattern the string should validate against.
class schemabuilder.Bool(**kw)

Bases: schemabuilder.primitives.Generic

Boolean type

class schemabuilder.Number(**kw)

Bases: schemabuilder.primitives.Generic

A number type with its multipleOf and range attributes.

Parameters:
  • min – minimum valid value.
  • max – maximum valid value.
  • exclusive_min – should the minimum valid be exclusive.
  • exclusive_max – should the maximum valid be exclusive.
  • multiple_of – a value the number should be a multiple of.
class schemabuilder.Int(**kw)

Bases: schemabuilder.primitives.Number

An integer type with the same attributes than Number.

class schemabuilder.Array(**kw)

Bases: schemabuilder.primitives.Generic

Array type.

class schemabuilder.Object(**kw)

Bases: schemabuilder.primitives.Generic

An object type.

Parameters:
  • properties – dict of properties, key -> property type.
  • pattern_properties – like properties, but the keys are defines by a regex pattern.
  • additional_properties – should there be any additional properties?
  • min – minimum number of properties.
  • max – maximum number of properties.
  • required – list of properties names that are required.

Schema

class schemabuilder.Schema(id=None, desc=None)

Collects schema definitions

define(id, schema)

Add a schema to the list of definition

Parameters:
  • id – id of the schema.
  • schema – the schema as a dict or a :class:schemabuilder.primitives.Generic
Returns:

reference to schema.

Return type:

schemabuilder.schema.Ref

to_dict()

Return the schema as a dict ready to be serialized.

validator(id)

Create a validator for the current state of the schema.

Note: a validator (the resolver it’s using) is not thread safe.

Parameters:id – id of the schema in the list of definition.
Returns:a validator.
Return type:jsonschema.Draft4Validator
class schemabuilder.schema.Ref(id, schema, **kw)

Reference to a schema inside a schema collection.

Can be used to reference that schema in an other schema of the some collection.

Parameters:
  • id – name of the schema
  • schema – schema collection the schema resides.

Warning

The schema is saved as a weak reference. It will only be able to validate data while a reference of that schema live somewhere else.

validate(data)

Validate the data against the schema.