doncatnip/kanone — Issue #7: An idea, using convert for Boolean
Author: nikitinsm
Created: 2012-02-01 12:05:40+00:00
State: open

```python
@messages\
( type="Invalid type (%(value.type)s), must be a bool"
)
class HumanBoolean( TypeValidator):

def on_value(self, context, value):
if isinstance(value, int )\
and not(value<0 or value>1):
value = bool(value)

if not (isinstance( value, bool )):
if self._convert:
if str(value).lower() in ('1', 'true', 'yes', 'on'):
return True
elif str(value).lower() in ('0', 'false', 'no', 'off'):
return False
raise Invalid( value, self, 'type' )
return value
```