組み合わせのバリデーション

formencode/compound.py に、AnyとAllというバリデータークラスが定義されています。これを使うと、複数のバリデーションの組み合わせが実現できる、みたいです。ちゃんと確認してないけど。

http://formencode.org/module-formencode.compound.html

>>> from formencode import validators, compound
>>> a=compound.Any(validators.Int(), validators.Email())
>>> a.to_python('10')
10
>>> a.to_python('foo@bar.com')
'foo@bar.com'
>>> a.to_python('sometext')
Traceback (most recent call last):
  ...
Invalid: Please enter an integer value
    
>>> a=compound.All(validators.Int(), validators.MaxLength(3))
>>> a.to_python('100')
100
>>> a.to_python('1000')
Traceback (most recent call last):
  ...
Invalid: Enter a value less than 3 characters long