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:
SerializableBase 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:
JSONSchemaObjectValue is a constant.
- __init__(const, **kws)[source]¶
Initialize.
- Parameters:
args – see
JSONSchemaObjectconst – constant value
- Params kws:
see
JSONSchemaObject
- class Boolean[source]¶
Bases:
JSONSchemaObjectBoolean 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:
JSONSchemaObjectText/string data type.
- __init__(*, minLength: int = None, maxLength: int = None, pattern: str = None, format: str = None, **kws)[source]¶
Initialize.
- Parameters:
args – see
JSONSchemaObjectminLength – min string size
maxLength – max string size
pattern – regex pattern
format – string format (not working?)
- Params kws:
see
JSONSchemaObject
- class GUID[source]¶
Bases:
StringUUID string alias.
- __init__(*, minLength: int = None, maxLength: int = None, pattern: str = None, format: str = None, **kws)¶
Initialize.
- Parameters:
args – see
JSONSchemaObjectminLength – min string size
maxLength – max string size
pattern – regex pattern
format – string format (not working?)
- Params kws:
see
JSONSchemaObject
- class Date[source]¶
Bases:
StringDate string alias.
- __init__(*, minLength: int = None, maxLength: int = None, pattern: str = None, format: str = None, **kws)¶
Initialize.
- Parameters:
args – see
JSONSchemaObjectminLength – min string size
maxLength – max string size
pattern – regex pattern
format – string format (not working?)
- Params kws:
see
JSONSchemaObject
- class DateTime[source]¶
Bases:
StringDatetime string alias.
- __init__(*, minLength: int = None, maxLength: int = None, pattern: str = None, format: str = None, **kws)¶
Initialize.
- Parameters:
args – see
JSONSchemaObjectminLength – min string size
maxLength – max string size
pattern – regex pattern
format – string format (not working?)
- Params kws:
see
JSONSchemaObject
- class Number[source]¶
Bases:
JSONSchemaObjectNumeric 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
JSONSchemaObjectmultipleOf – 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:
NumberInteger type.
- __init__(*, multipleOf: float = None, minimum: float = None, maximum: float = None, exclusiveMinimum: float = None, exclusiveMaximum: float = None, **kws)¶
Initialize.
- Parameters:
args – see
JSONSchemaObjectmultipleOf – 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:
JSONSchemaObjectJSON 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:
JSONSchemaObjectNull value only.
- __new__(**kwargs)¶
- class Array[source]¶
Bases:
JSONSchemaObjectArray, 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:
JSONSchemaKeywordGiven data must be valid against any (one or more) of the given sub-schemas.
- __init__(*items: JSONSchemaObject)¶
Initialize.
- class AllOf[source]¶
Bases:
JSONSchemaKeywordGiven data must be valid against all of the given sub-schemas.
- __init__(*items: JSONSchemaObject)¶
Initialize.
- class OneOf[source]¶
Bases:
JSONSchemaKeywordGiven data must be valid against exactly one of the given sub-schemas.
- __init__(*items: JSONSchemaObject)¶
Initialize.
- class Not[source]¶
Bases:
JSONSchemaObjectReverse the condition.
- __init__(item: JSONSchemaObject, /)[source]¶
Initialize.
- Parameters:
item – schema to create negative condition from