class Validator(object):
def validate(self, value):
pass
def clean(self, value):
self.validate(value)
return value
class IntegerCleaner(Validator):
def clean(self, value):
try:
value = int(value)
except ValueError:
raise ValidationError(...)
return value
class FunctionValidator(Validator):
def __init__(self, func):
self.func = func
def validate(self, value):
func(value)
Not 100% sure about the split between clean and validate but there is a difference between the two. Perhaps instead we have cleaners and validators, one of which changes and one of which doesn't, but they're chainable together.
Related but not necessarily belonging in this tree is idea of a shaper which is a multivalued cleaner which returns different data shape. Then again perhaps these should be in the validator tree so subsequent validators can use the changed shape. Shaping is also about input/output though so these lines are blurry.
Not 100% sure about the split between
cleanandvalidatebut there is a difference between the two. Perhaps instead we havecleanersandvalidators, one of which changes and one of which doesn't, but they're chainable together.Related but not necessarily belonging in this tree is idea of a
shaperwhich is a multivalued cleaner which returns different data shape. Then again perhaps these should be in the validator tree so subsequent validators can use the changed shape. Shaping is also about input/output though so these lines are blurry.