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 some validators. | |
| Create a function that checks whether a value is a valid float. | |
| Convert a function to a validator function. | |
| Create a function that checks for membership. | |
| Create a function that checks whether a value is a valid integer. | |
| Create a function that checks whether a value is in an interval. | |
| Check whether a string is a valid EAN-13. | |
| Check whether a string is a valid IBAN. | |
| Check whether a string is a valid ISBN. | |
| Check whether a string is valid according to the Luhn algorithm. | |
| Create a function that checks the length. | |
| Create a function that checks a mapping. | |
| Create a function that checks whether an object is valid. | |
| Create a function that checks a value with a pattern. | |
| Create a function that checks the properties of an object. | |
| Create a function that checks a sequence. | |
| 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
- Raises
- TypeError – if one of the - validatorsis 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
- min_value (float or None) – minimum value ( - Nonemeans no limit)
- max_value (float or None) – maximum value ( - Nonemeans no limit)
- min_incl (bool) – if - True- min_valueis included
- max_incl (bool) – if - True- max_valueis included
- allow_nan (bool) – if - True- math.nanis allowed as the argument of the returned validator function
- allow_inf (bool) – if - True- math.infis allowed as the argument of the returned validator function
- allow_ints (bool) – if - Truean integer value as the argument of the returned validator function will not raise a- TypeError.
 
- Returns
- Raises
- ValueError – if - min_value > max_value
 
 
- 
salmagundi.validation.func2validator(func, err_result=False)[source]¶
- Convert a function to a validator function. - The returned validator function raises a - ValueErrorif- 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
 
- 
salmagundi.validation.in_validator(container, negate=False)[source]¶
- Create a function that checks for membership. 
- 
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_floatsis- 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
- Returns
- Raises
- ValueError – if - min_value > max_value
 
 
- 
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
- Returns
- 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-9are allowed and the length of the string must be 13.- Parameters
- s (str) – the string 
- Returns
- Trueif the string is a valid EAN-13
- Return type
- 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-Zand- 0-9are allowed.- Parameters
- s (str) – the string 
- Returns
- Trueif the string is a valid IBAN
- Return type
- 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-9plus- Xfor ISBN-10 are allowed and the length of the string must be either 10 or 13.- Parameters
- s (str) – the string 
- Returns
- Trueif the string is a valid ISBN
- Return type
- 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-9are allowed.- Parameters
- s (str) – the string 
- Returns
- Trueif the string is valid
- Return type
- 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.
- 
salmagundi.validation.mapping_validator(validator, what='values')[source]¶
- Create a function that checks a mapping. - The check is done by applying the - validatorto each key (if- what='keys'), value (if- what='values') or (key, value)-tuple (if- what='items').- Parameters
- validator – validator function 
- what (str) – see function description 
 
- Returns
- Raises
- TypeError – if - validatoris 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
- Returns
- Raises
- TypeError – if - validatoris not callable or- value_typeis 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 - stror- 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' 
- 
salmagundi.validation.properties_validator(validators, mapping=False)[source]¶
- Create a function that checks the properties of an object. - The - validatorsargument must be a mapping from a property name to a validator function or- Noneif 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- AttributeErrorwill be raised).- If - mappingis- Truethe 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
- validators (dict) – validator function for each property 
- mapping (bool) – if - Truethe value must be a mapping
 
- Raises
- TypeError – if - validatorsis 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 - validatorto 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
- validator – validator function 
- Returns
- Raises
- TypeError – if - validatoris not callable
 
- 
salmagundi.validation.set_validator(validator)[source]¶
- Create a function that checks a set. - The check is done by applying the - validatorto each element in the set.- Parameters
- validator – validator function 
- Returns
- Raises
- TypeError – if - validatoris not callable