Check and/or convert a value, throwing an exception on failure.
The core service method for konval, this can be used to check and convert
data. Note that this accepts a single value - if you want to sanitize a
whole list in the same way, use a list comprehension.
For example:
>>> sanitize (1, int)
1
>>> from konval import IsEqualOrMore, ToLength
>>> sanitize ('2', [float, IsEqualOrMore(1)])
2.0
>>> x = sanitize (['a', 'b'], [ToLength(), float, IsEqualOrMore(1)])
>>> x
2.0
>>> sanitize (['a', 'b'], [ToLength(), float, IsEqualOrMore(3)])
Traceback (most recent call last):
...
ValueError: 2.0 is lower than 3
- Parameters:
val - the original value to be validated and/or converted
validators - a validator or sequence of validators or suitable objects
- Returns:
- the converted value, or original one if only validation has occurred
|