# This file contains the base class for Validator/Converters

# Define a passive exception type for when validation fails
class InvalidField(Exception):
    pass

class ValidatorConverter:

    def _attemptConvert(self, value):
        # core logic: validate the input (value)
        #   if validation passes, run the value through convert and return it
        #   else, raise an error with whatever message the validator returns
        v = self._validate(value)
        if v:
            raise InvalidField, v
        else:
            return self._convert(value)
            
    def _convert(self, value):
        """By default, all validators do no conversion... they just return what they are given"""
        # overwrite this method in a subclass to create a converter
        return value
    
    def _validate(self, value):
        """Return an error message, or None if valid"""
        # overwrite this method in a subclass to create a validator
        return None

## This is taken pretty much verbatim from FunFormKit by Ian Bicking -- Thanks, Ian
class FormValidator:
    """
    This is just a skeleton for what an actual form validator should
    do.

    The important method is .validate(), of course.  It gets passed a
    dictionary of the (processed) values from the form.  If you have
    .validatePartialForm() return True, it will be called regardless
    of whether any of the fields have validation errors on thier own.

    Raise an error (as below) if you don't like something about the field
    values.

    Return None on no errors.
    """

    def validatePartialForm(self):
        return None
    def validate(self, fieldDict):
        raise InvalidField, "This FormValidator hasn't been written, yet!"