Package konval :: Module basevalidator :: Class BaseValidator
[hide private]
[frames] | no frames]

Class BaseValidator

source code

object --+
         |
        BaseValidator
Known Subclasses:

A base class for custom validators.

The aim to make construction of new validators easy and quick. Ideally, with this as a base class, most subclasses should only need to override one method (convert_value or validate_value) and perhaps supply a c'tor if some state needs to be stored.

Instance Methods [hide private]
 
__call__(self, value)
Converts and validates user input.
source code
 
convert(self, value)
Transform a value to the desired form.
source code
 
convert_value(self, value)
Transform a value to the desired form.
source code
 
raise_conversion_error(self, bad_val, err)
Raise an error for a conversion problem.
source code
 
make_conversion_error_msg(self, bad_val, err)
Generate an error message for a conversion problem.
source code
 
validate(self, value)
Is this value correct or of the correct form?
source code
 
validate_value(self, value)
Check a value is of the desired form.
source code
 
raise_validation_error(self, bad_val, err)
Generate an error message for a validation problem.
source code
 
make_validation_error_msg(self, bad_val, err)
Generate an error message for a validation problem.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __init__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__call__(self, value)
(Call operator)

source code 

Converts and validates user input.

This converts the passed value (via convert) and then validates it (via validate). It should throw an error if any problems. This is the primary entry-point for validator objects and could be overridden in a subclass if required. However, it would probably be easier done in other methods called by this.

Parameters:
  • value - value to be checked or transformed
Returns:
the transformed or validated value

convert(self, value)

source code 

Transform a value to the desired form.

This attempts to convert the passed value (via convert_value) and if successful returns the new value. If any exception is thrown by conversion, raise_conversion_error is called with the bad value and error (if any). Behaviour could be customised by overriding this in a subclass, but convert_value may be a better target.

Parameters:
  • value - value to be transformed
Returns:
the transformed value

convert_value(self, value)

source code 

Transform a value to the desired form.

This is the workhorse method that is called by convert to transform passed values. As such, errors are signalled by throwing a meaningful exception. This is one of the obvious and easiest places to customize behaviour by overriding in a subclass.

Parameters:
  • value - value to be transformed
Returns:
the transformed value

raise_conversion_error(self, bad_val, err)

source code 

Raise an error for a conversion problem.

Override in subclass if need be, for specific exception types and messages.

Parameters:
  • bad_val - The value that failed to convert.
  • err (Exception, None) - The exception caught during conversion.

make_conversion_error_msg(self, bad_val, err)

source code 

Generate an error message for a conversion problem.

Parameters as per raise_conversion_error. Override in subclass if need be, for more specific and meaningful messages.

validate(self, value)

source code 

Is this value correct or of the correct form?

This checks the passed value (via validate_value) and if successful returns the new value. If any exception is thrown by validation, raise_validation_error is called with the bad value and error (if any). Behaviour could be customised by overriding this in a subclass, but validate_value may be a better target.

Parameters:
  • value - value to be checked

validate_value(self, value)

source code 

Check a value is of the desired form.

This is the workhorse method that is called by validate to check passed values. As such, errors are signalled by either by throwing a meaningful exception or by returning false. This is one of the obvious and easiest places to customize behaviour by overriding in a subclass.

Parameters:
  • value - value to be checked
Returns:
success of validation.

raise_validation_error(self, bad_val, err)

source code 

Generate an error message for a validation problem.

Override in subclass if need be, for specific exception types and messages.

Parameters:
  • bad_val - The value that failed to validate.
  • err (Exception, None) - The exception caught during validation.

make_validation_error_msg(self, bad_val, err)

source code 

Generate an error message for a validation problem.

Parameters as per raise_validation_error. Override in subclass if need be, for more specific and meaningful messages.