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

Class WrappingValidator

source code

   object --+    
            |    
BaseValidator --+
                |
               WrappingValidator

Wraps functions and possible error messages in a validator.

This allows converting and validating functions to be easily encapsulated in a validator. Given the design of konval (any function that accepts & returns a value can be used as a validator), this is only slightly useful. However it does allow useful error messages to be incorporated.

For example:

>>> from string import *
>>> v = WrappingValidator (conv_fn=upper, conv_msg='not a string')
>>> v('abc')
'ABC'
>>> v(1)
Traceback (most recent call last):
...
ValueError: not a string
>>> v = WrappingValidator (val_fn=lambda x: len(x) < 4)
>>> v('abc')
'abc'
>>> v(1)
Traceback (most recent call last):
...
ValueError: can't validate '1'

Idea flinched from FormEncode.

Instance Methods [hide private]
 
__init__(self, conv_fn=None, conv_msg=None, val_fn=None, val_msg=None)
C'tor accepting functors for validation & conversion.
source code
 
convert_value(self, value)
Transform a value to the desired form.
source code
 
make_conversion_error_msg(self, bad_val, err)
Generate an error message for a conversion problem.
source code
 
validate_value(self, value)
Check a value is of the desired form.
source code
 
make_validation_error_msg(self, bad_val, err)
Generate an error message for a validation problem.
source code

Inherited from BaseValidator: __call__, convert, raise_conversion_error, raise_validation_error, validate

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

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, conv_fn=None, conv_msg=None, val_fn=None, val_msg=None)
(Constructor)

source code 

C'tor accepting functors for validation & conversion.

conv_fn and val_fn can be any callable object (e.g. a class with __call__, lambda). Note that validation function should return not the value but validation success, or just raise an exception. Error message strings can include the keyword substitutions 'bad_val' and 'err' for the value that raised the exception and the exception itself.

Parameters:
  • conv_fn (callable) - performs conversion, should accept value and return transformed
  • conv_msg (str) - string for error message in event of conversion failure
  • val_fn (callable) - performs validation, should accept value and return success
  • val_msg (str) - string for error message in event of validation failure
Overrides: object.__init__

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
Overrides: BaseValidator.convert_value
(inherited documentation)

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.

Overrides: BaseValidator.make_conversion_error_msg
(inherited documentation)

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.
Overrides: BaseValidator.validate_value
(inherited documentation)

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.

Overrides: BaseValidator.make_validation_error_msg
(inherited documentation)