formencode.compound – Validate with multiple validators

Validators for applying validations in sequence.

Module Contents

class formencode.compound.All(*args, **kw)

Check that specified validators are valid.

This class is like an ‘and’ operator for validators. All validators must work, and the results are passed in turn through all validators for conversion in the order of evaluation. All is the same as Pipe but operates in the reverse order.

The order of evaluation differs depending on if you are validating to Python or from Python as follows:

The validators are evaluated right to left when validating to Python.

The validators are evaluated left to right when validating from Python.

Pipe is more intuitive when predominantly validating to Python.

Examples:

>>> from formencode.validators import DictConverter
>>> av = All(validators=[DictConverter({2: 1}),
... DictConverter({3: 2}), DictConverter({4: 3})])
>>> av.to_python(4)
1
>>> av.from_python(1)
4

Messages

badType:

The input must be a string (not a %(type)s: %(value)r)

empty:

Please enter a value

noneType:

The input must be a string (not None)

class formencode.compound.Any(*args, **kw)

Check if any of the specified validators is valid.

This class is like an ‘or’ operator for validators. The first validator/converter in the order of evaluation that validates the value will be used.

The order of evaluation differs depending on if you are validating to Python or from Python as follows:

The validators are evaluated right to left when validating to Python.

The validators are evaluated left to right when validating from Python.

Examples:

>>> from formencode.validators import DictConverter
>>> av = Any(validators=[DictConverter({2: 1}),
... DictConverter({3: 2}), DictConverter({4: 3})])
>>> av.to_python(3)
2
>>> av.from_python(2)
3

Messages

badType:

The input must be a string (not a %(type)s: %(value)r)

empty:

Please enter a value

noneType:

The input must be a string (not None)

class formencode.compound.Pipe(*args, **kw)

Pipe value through all specified validators.

This class works like All but the order of evaluation is opposite. All validators must work, and the results are passed in turn through each validator for conversion in the order of evaluation. A behaviour known to Unix and GNU users as ‘pipe’.

The order of evaluation differs depending on if you are validating to Python or from Python as follows:

The validators are evaluated left to right when validating to Python.

The validators are evaluated right to left when validating from Python.

Examples:

>>> from formencode.validators import DictConverter
>>> pv = Pipe(validators=[DictConverter({1: 2}),
... DictConverter({2: 3}), DictConverter({3: 4})])
>>> pv.to_python(1)
4
>>> pv.from_python(4)
1

Messages

badType:

The input must be a string (not a %(type)s: %(value)r)

empty:

Please enter a value

noneType:

The input must be a string (not None)