Module validation

Data validation.

New in version 0.9.0.

In this module a validator function is a callable that takes a value as its only argument and returns normally if the value is considered valid or raises a ValueError otherwise. It may raise a TypeError if the value is not of the right type.

Overview

chain_validator

Chain some validators.

float_validator

Create a function that checks whether a value is a valid float.

func2validator

Convert a function to a validator function.

in_validator

Create a function that checks for membership.

int_validator

Create a function that checks whether a value is a valid integer.

interval_validator

Create a function that checks whether a value is in an interval.

is_valid_ean13

Check whether a string is a valid EAN-13.

is_valid_iban

Check whether a string is a valid IBAN.

is_valid_isbn

Check whether a string is a valid ISBN.

is_valid_luhn

Check whether a string is valid according to the Luhn algorithm.

length_validator

Create a function that checks the length.

mapping_validator

Create a function that checks a mapping.

object_validator

Create a function that checks whether an object is valid.

pattern_validator

Create a function that checks a value with a pattern.

properties_validator

Create a function that checks the properties of an object.

sequence_validator

Create a function that checks a sequence.

set_validator

Create a function that checks a set.

salmagundi.validation.chain_validator(*validators)[source]

Chain some validators.

>>> str_vf = object_validator(value_type=str)
>>> len_vf = length_validator(min_len=5, max_len=10)
>>> # validator for values of type str and length 5-10
>>> vf = chain_validator(str_vf, len_vf)
>>> vf('abcde')
>>> vf('abc')
Traceback (most recent call last):
  ...
ValueError: length must in [5, 10], got 3
>>> vf(b'abcde')
Traceback (most recent call last):
  ...
TypeError: value must be of type 'str', got 'bytes'
Parameters

validators – validator functions

Returns

validator function

Raises

TypeError – if one of the validators is not callable

salmagundi.validation.float_validator(*, min_value=None, max_value=None, min_incl=True, max_incl=True, allow_nan=False, allow_inf=False, allow_ints=False)[source]

Create a function that checks whether a value is a valid float.

Parameters
Returns

validator function

Raises
salmagundi.validation.func2validator(func, err_result=False)[source]

Convert a function to a validator function.

The returned validator function raises a ValueError if func() returns err_result.

>>> vf = func2validator(str.isupper)
>>> vf('A')
>>> vf('a')
Traceback (most recent call last):
  ...
ValueError: invalid value: 'a'
Parameters
  • func – callable that takes a value as its only argument

  • err_result (bool) – the result, that will raise the ValueError

Returns

validator function

salmagundi.validation.in_validator(container, negate=False)[source]

Create a function that checks for membership.

Parameters
  • container – container object (must support the in operator)

  • negate (bool) – if True the value is valid if it is not in the container.

Returns

validator function

Raises

TypeError – if container does not support the in operator

salmagundi.validation.int_validator(*, min_value=None, max_value=None, allow_floats=False)[source]

Create a function that checks whether a value is a valid integer.

If allow_floats is True, a float value as the argument of the returned validator function will not raise a TypeError. Instead a value that represents an integer (such as 1.0) and is in the interval [min_value, max_value] will be considered valid. All other cases will raise a ValueError.

Parameters
  • min_value (int or None) – minimum value (inclusive, None means no limit)

  • max_value (int or None) – maximum value (inclusive, None means no limit)

  • allow_floats (bool) – see function description

Returns

validator function

Raises
salmagundi.validation.interval_validator(*, min_value=None, max_value=None, min_incl=True, max_incl=True)[source]

Create a function that checks whether a value is in an interval.

The type of the checked values must at least support the operators < (for *_incl=False) or <= (for *_incl=True).

Parameters
  • min_value – minimum value (None means no limit)

  • max_value – maximum value (None means no limit)

  • min_incl (bool) – if True min_value is included

  • max_incl (bool) – if True max_value is included

Returns

validator function

Raises

ValueError – if min_value > max_value

salmagundi.validation.is_valid_ean13(s)[source]

Check whether a string is a valid EAN-13.

EAN = European Article Number

The string must not contain any separators; only the characters 0-9 are allowed and the length of the string must be 13.

Parameters

s (str) – the string

Returns

True if the string is a valid EAN-13

Return type

bool

Raises

ValueError – if a character is not allowed or the length is wrong

salmagundi.validation.is_valid_iban(s)[source]

Check whether a string is a valid IBAN.

IBAN = International Bank Account Number

The string must not contain any separators; only the characters A-Z and 0-9 are allowed.

Parameters

s (str) – the string

Returns

True if the string is a valid IBAN

Return type

bool

Raises

ValueError – if a character is not allowed

salmagundi.validation.is_valid_isbn(s)[source]

Check whether a string is a valid ISBN.

ISBN = International Standard Book Number

The string must not contain any separators; only the characters 0-9 plus X for ISBN-10 are allowed and the length of the string must be either 10 or 13.

Parameters

s (str) – the string

Returns

True if the string is a valid ISBN

Return type

bool

Raises

ValueError – if a character is not allowed or the length is wrong

salmagundi.validation.is_valid_luhn(s)[source]

Check whether a string is valid according to the Luhn algorithm.

The Luhn algorithm is used to validate a variety of identification numbers, e.g. credit card numbers.

The string must not contain any separators; only the characters 0-9 are allowed.

Parameters

s (str) – the string

Returns

True if the string is valid

Return type

bool

Raises

ValueError – if a character is not allowed

salmagundi.validation.length_validator(min_len=0, max_len=None)[source]

Create a function that checks the length.

The type of the checked values must support the len() function.

Parameters
  • min_len (int) – the minimum length

  • max_len (int or None) – the maximum length (None means no limit)

Raises
  • ValueError – if min_len or max_len are negative integers or min_len > max_len

  • TypeError – if min_len or max_len are not of type int

salmagundi.validation.mapping_validator(validator, what='values')[source]

Create a function that checks a mapping.

The check is done by applying the validator to each key (if what='keys'), value (if what='values') or (key, value)-tuple (if what='items').

Parameters
Returns

validator function

Raises

TypeError – if validator is not callable

salmagundi.validation.object_validator(*, validator=None, value_type=None, strict_type=False, allow_none=False)[source]

Create a function that checks whether an object is valid.

>>> # validator for values of type str and length 5-10
>>> len_vf = length_validator(min_len=5, max_len=10)
>>> vf = object_validator(validator=len_vf, value_type=str)
>>> vf('abcde')
>>> vf('abc')
Traceback (most recent call last):
  ...
ValueError: invalid object: 'abc' (length must be in [5, 10], got 3)
>>> vf(b'abcde')
Traceback (most recent call last):
  ...
TypeError: value must be of type 'str', got 'bytes'
Parameters
  • validator (callable or None) – validator function

  • value_type (type or None) – the type of the value (not checked if None)

  • strict_type (bool) – if True instances of subclasses of value_type are not allowed

  • allow_none (bool) – if True None values are valid even when value_type is set

Returns

validator function

Raises

TypeError – if validator is not callable or value_type is not a type-object

salmagundi.validation.pattern_validator(pattern)[source]

Create a function that checks a value with a pattern.

The type of the argument for the returned validator function can be either str or bytes. It must be the same type that is used for the pattern.

The check is done by using re.Pattern.search().

>>> # validator for values of type str and length 5-10
>>> vf = pattern_validator(r'^.{5,10}$')
>>> vf('abcde')
>>> vf('abc')
Traceback (most recent call last):
  ...
ValueError: invalid value: 'abc'
>>> vf(b'abcde')
Traceback (most recent call last):
  ...
TypeError: the type of 'value' must be 'str', got 'bytes'
Parameters

pattern (bytes or str or compiled pattern) – regular expression pattern (see: module re)

Raises

TypeError – if pattern has the wrong type

salmagundi.validation.properties_validator(validators, mapping=False)[source]

Create a function that checks the properties of an object.

The validators argument must be a mapping from a property name to a validator function or None if only the existence of a property should be checked. If the argument value for the returned validator function is missing a property, the value is considered invalid (no AttributeError will be raised).

If mapping is True the value must be a mapping with string keys that are used as properties.

>>> validators = dict(a=func2validator(str.isupper), b=None)
>>> vf = properties_validator(validators, True)
>>> vf({'a': 'ABC', 'b': 1})
>>> vf({'a': 'ABC'})
Traceback (most recent call last):
  ...
ValueError: missing property 'b'
>>> vf({'a': 'abc', 'b': 1})
Traceback (most recent call last):
  ...
ValueError: invalid property 'a': invalid value: 'abc'
Parameters
Raises

TypeError – if validators is not a mapping or keys are not strings or values are not callable or None

salmagundi.validation.sequence_validator(validator)[source]

Create a function that checks a sequence.

The check is done by applying the validator to each item in the sequence.

>>> vf = sequence_validator(func2validator(str.isupper))
>>> vf('ABC')
>>> vf('AbC')
Traceback (most recent call last):
  ...
ValueError: error at sequence index 1: invalid value: 'b'
Parameters

validatorvalidator function

Returns

validator function

Raises

TypeError – if validator is not callable

salmagundi.validation.set_validator(validator)[source]

Create a function that checks a set.

The check is done by applying the validator to each element in the set.

Parameters

validatorvalidator function

Returns

validator function

Raises

TypeError – if validator is not callable