schemadict package

Submodules

schemadict.schemadict module

Schema dictionaries

exception schemadict.schemadict.SchemaError

Bases: Exception

Raised if the schema dictionary is ill-defined

class schemadict.schemadict.SpecialValidators

Bases: object

Collection of special validator functions

Special validator functions must accept three arguments in the order listed below. The actual variable names may differ depending on context.

Args:
sd_key

special key from the schemadict

sd_value

special value from the schemadict

sd_instance

instance of the schemadict from which tests are called

static check_req_keys_in_dict(sd_key, req_keys, sd_instance)

Check that required keys are in a test dictionary

class schemadict.schemadict.ValidatorDict

Bases: collections.OrderedDict

Use to map ‘type’ (=key) and validator functions

Raise ‘SchemaError’ if meta schema for ‘type’ is not defined.

register_type(new_type, add_val={})

Register a new type

Args:
new_type

any type/class

add_val

additional validators

class schemadict.schemadict.Validators

Bases: object

Collection of validator functions for test dictionary values

All validator functions must accept four arguments in the order listed below. The actual variable names may differ depending on context.

Args:
key

related dictionary key (used in error message)

test_value

value to be checked

exp_value

expected value or comparison object

sd_instance

instance of the schemadict from which tests are called

Note:
  • In most cases the last argument (the schemadict instance) is not needed, and the test function may ignore the argument. However, in case the schemadict is instantiated with custom validator functions, these must be propagated when recursively checking nested data structures

FOR_TYPE = {'type': <function Validators.is_type>}
static allowed_items(key, values, allowed_items, _)
static check_item_schema(key, iterable, item_schema, sd_instance)
classmethod check_item_schemadict(key, iterable, item_schema, sd_instance)
static check_item_types(key, iterable, exp_item_type, _)
static check_regex_match(key, string, pattern, _)
static check_schemadict(key, testdict, schema, sd_instance)
static has_max_len(key, value, max_len, _)
static has_min_len(key, value, min_len, _)
static is_ge(key, value, comp_value, _)
static is_gt(key, value, comp_value, _)
static is_le(key, value, comp_value, _)
static is_lt(key, value, comp_value, _)
static is_type(key, value, exp_type, _)
static one_of(key, value, allowed_values, _)
class schemadict.schemadict.schemadict(*args, validators=ValidatorDict([('$required_keys', <function SpecialValidators.check_req_keys_in_dict>), ('$allowed_keys', Ellipsis), (<class 'numbers.Number'>, {'type': <function Validators.is_type>, 'one_of': <function Validators.one_of>, '>': <function Validators.is_gt>, '<': <function Validators.is_lt>, '>=': <function Validators.is_ge>, '<=': <function Validators.is_le>}), (<class 'bool'>, {'type': <function Validators.is_type>}), (<class 'dict'>, {'type': <function Validators.is_type>, 'schema': <function Validators.check_schemadict>}), (<class 'float'>, {'type': <function Validators.is_type>, 'one_of': <function Validators.one_of>, '>': <function Validators.is_gt>, '<': <function Validators.is_lt>, '>=': <function Validators.is_ge>, '<=': <function Validators.is_le>}), (<class 'int'>, {'type': <function Validators.is_type>, 'one_of': <function Validators.one_of>, '>': <function Validators.is_gt>, '<': <function Validators.is_lt>, '>=': <function Validators.is_ge>, '<=': <function Validators.is_le>}), (<class 'list'>, {'type': <function Validators.is_type>, 'min_len': <function Validators.has_min_len>, 'max_len': <function Validators.has_max_len>, 'item_types': <function Validators.check_item_types>, 'item_schema': <function Validators.check_item_schema>, 'item_schemadict': <bound method Validators.check_item_schemadict of <class 'schemadict.schemadict.Validators'>>, 'allowed_items': <function Validators.allowed_items>}), (<class 'str'>, {'type': <function Validators.is_type>, 'min_len': <function Validators.has_min_len>, 'max_len': <function Validators.has_max_len>, 'one_of': <function Validators.one_of>, 'regex': <function Validators.check_regex_match>}), (<class 'tuple'>, {'type': <function Validators.is_type>, 'min_len': <function Validators.has_min_len>, 'max_len': <function Validators.has_max_len>, 'item_types': <function Validators.check_item_types>, 'item_schema': <function Validators.check_item_schema>, 'item_schemadict': <bound method Validators.check_item_schemadict of <class 'schemadict.schemadict.Validators'>>, 'allowed_items': <function Validators.allowed_items>})]), **kwargs)

Bases: collections.abc.MutableMapping

A schemadict is a dictionary that specifies the type and format of values for some given key. To check if a test dictionary is conform with the expected schema, schemadict provides the validate() method. If the test dictionary is ill-defined, an error will be thrown, otherwise None is returned.

validate(testdict)

Check that a dictionary conforms to a schema dictionary. This function will raise an error if the ‘testdict’ is not in agreement with the schema.

Args:
testdict

(dict) dictionary to test against the schema

Raises:
KeyError

if test dictionary does not have a required key

SchemaError

if the schema itself is ill-defined

TypeError

if test dictionary has a value of wrong type

ValueError

if test dictionary has a value of wrong ‘size’

Module contents