I'm extending a Python ORM with an InType
field, which is a validator.
In fact, I am creating a whole number of validators... in web2py DAL terms I am emulating:
Field(type=String, validators=[IS_EMAIL(), IS_IN(['foo@bar.com'])])
However doing it all at the custom field level. I've tried to create a test-case here:
from types import StringType, IntType
class StringField(object):
def validate(self, value):
assert type(value) is StringType
self.value = value
class IntField(object):
def validate(self, value):
assert type(value) is IntType
self.value = value
class InField(object):
"""
Confirms `value in enumerable` before putting
in `StringField` or `IntField`
"""
def validate(self, value, enumerable):
assert value not in enumerable
obj = (StringField if type(value) is StringType else IntField)()
obj.validate(value)
return obj
Rather than returning an instance object of a class figured out at runtime like above, I was thinking it'd be better to implement it using metaclassing, or some other technique.