jsonschema - python object validation

This module implements most of jsonschema draft 7 specification for validating python json-like objects (dictionaries, lists and other simple data types).

You can use python classes to describe a validator schema and compile this schema into a validation function which accepts a json-like object and returns its validated and normalized copy.

import jsonschema as j

obj = j.Object({
    'my_string': j.String(minLength=1, default='default_string')
}, required=['my_string'], additionalProperties=False)

validator = j.compile_schema(obj)
obj = validator({'my_string': 'abc'})  # returns a new validated and normalized object
compile_schema(validator: JSONSchemaObject | dict, /) Callable[source]

Compile JSONSchema object into a validator function.

class JSONSchemaObject[source]

Bases: Serializable

Base JSONSchema object.

__init__(*, title: str = None, description: str = None, default=..., examples: list = None, enum: list = None, nullable: bool = None)[source]

Initialize.

Parameters:
  • title – short description

  • description – long description

  • default – default value

  • examples – value examples

  • enum – accepted list of values

  • nullable – not used

class Constant[source]

Bases: JSONSchemaObject

Value is a constant.

__init__(const, **kws)[source]

Initialize.

Parameters:
Params kws:

see JSONSchemaObject

class Boolean[source]

Bases: JSONSchemaObject

Boolean True or False.

__init__(*, title: str = None, description: str = None, default=..., examples: list = None, enum: list = None, nullable: bool = None)

Initialize.

Parameters:
  • title – short description

  • description – long description

  • default – default value

  • examples – value examples

  • enum – accepted list of values

  • nullable – not used

class String[source]

Bases: JSONSchemaObject

Text/string data type.

__init__(*, minLength: int = None, maxLength: int = None, pattern: str = None, format: str = None, **kws)[source]

Initialize.

Parameters:
  • args – see JSONSchemaObject

  • minLength – min string size

  • maxLength – max string size

  • pattern – regex pattern

  • format – string format (not working?)

Params kws:

see JSONSchemaObject

class GUID[source]

Bases: String

UUID string alias.

__init__(*, minLength: int = None, maxLength: int = None, pattern: str = None, format: str = None, **kws)

Initialize.

Parameters:
  • args – see JSONSchemaObject

  • minLength – min string size

  • maxLength – max string size

  • pattern – regex pattern

  • format – string format (not working?)

Params kws:

see JSONSchemaObject

class Date[source]

Bases: String

Date string alias.

__init__(*, minLength: int = None, maxLength: int = None, pattern: str = None, format: str = None, **kws)

Initialize.

Parameters:
  • args – see JSONSchemaObject

  • minLength – min string size

  • maxLength – max string size

  • pattern – regex pattern

  • format – string format (not working?)

Params kws:

see JSONSchemaObject

class DateTime[source]

Bases: String

Datetime string alias.

__init__(*, minLength: int = None, maxLength: int = None, pattern: str = None, format: str = None, **kws)

Initialize.

Parameters:
  • args – see JSONSchemaObject

  • minLength – min string size

  • maxLength – max string size

  • pattern – regex pattern

  • format – string format (not working?)

Params kws:

see JSONSchemaObject

class Number[source]

Bases: JSONSchemaObject

Numeric data type (use it for both float or integer params).

__init__(*, multipleOf: float = None, minimum: float = None, maximum: float = None, exclusiveMinimum: float = None, exclusiveMaximum: float = None, **kws)[source]

Initialize.

Parameters:
  • args – see JSONSchemaObject

  • multipleOf – value should be a multiplier of

  • minimum – min allowed value

  • maximum – max allowed value

  • exclusiveMinimum – min allowed value (excl)

  • exclusiveMaximum – max allowed value (excl)

Params kws:

see JSONSchemaObject

class Integer[source]

Bases: Number

Integer type.

__init__(*, multipleOf: float = None, minimum: float = None, maximum: float = None, exclusiveMinimum: float = None, exclusiveMaximum: float = None, **kws)

Initialize.

Parameters:
  • args – see JSONSchemaObject

  • multipleOf – value should be a multiplier of

  • minimum – min allowed value

  • maximum – max allowed value

  • exclusiveMinimum – min allowed value (excl)

  • exclusiveMaximum – max allowed value (excl)

Params kws:

see JSONSchemaObject

class Object[source]

Bases: JSONSchemaObject

JSON object (dictionary) definition.

__init__(properties: dict[str, kaiju_tools.jsonschema.JSONSchemaObject] = None, *, patternProperties: dict[str, kaiju_tools.jsonschema.JSONSchemaObject] = None, propertyNames: dict = None, additionalProperties: bool = None, minProperties: int = None, maxProperties: int = None, required: list[str] = None, **kws)[source]

Initialize.

Params kws:

see JSONSchemaObject

Parameters:
  • properties – object attributes schema

  • patternProperties – object attribute patterns schema

  • propertyNames – allowed property names

  • additionalProperties – allow additional attributes

  • minProperties – min number of properties

  • maxProperties – max number of properties

  • required – list of required keys

class Null[source]

Bases: JSONSchemaObject

Null value only.

__new__(**kwargs)
class Array[source]

Bases: JSONSchemaObject

Array, list, set or tuple definition (depends on params).

__init__(items: JSONSchemaObject = None, *, prefixItems: Collection[JSONSchemaObject] = None, contains: JSONSchemaObject = None, additionalItems: bool = None, uniqueItems: bool = None, minItems: int = None, maxItems: int = None, **kws)[source]

Initialize.

Params kws:

see JSONSchemaObject

Parameters:
  • items – item type

  • prefixItems – use this to create a tuple like structure with different types of items

  • contains – a list should contain this type of objects

  • additionalItems – allow additional items in the tuple

  • uniqueItems – unique items - set

  • minItems – min number of items

  • maxItems – max number of items

class AnyOf[source]

Bases: JSONSchemaKeyword

Given data must be valid against any (one or more) of the given sub-schemas.

__init__(*items: JSONSchemaObject)

Initialize.

class AllOf[source]

Bases: JSONSchemaKeyword

Given data must be valid against all of the given sub-schemas.

__init__(*items: JSONSchemaObject)

Initialize.

class OneOf[source]

Bases: JSONSchemaKeyword

Given data must be valid against exactly one of the given sub-schemas.

__init__(*items: JSONSchemaObject)

Initialize.

class Not[source]

Bases: JSONSchemaObject

Reverse the condition.

__init__(item: JSONSchemaObject, /)[source]

Initialize.

Parameters:

item – schema to create negative condition from