Package konval :: Module containerval :: Class CheckLength
[hide private]
[frames] | no frames]

Class CheckLength

source code

                 object --+    
                          |    
basevalidator.BaseValidator --+
                              |
                             CheckLength
Known Subclasses:

Only allow values of a certain sizes.

Length limitations are expressed as (inclusive) minimum and maximum sizes. This is most useful for strings, but could be used for lists.

For example:

>>> v = CheckLength(min=2, max=4)
>>> v("abc")
'abc'
>>> v("abcde") #doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: 'abcde' is longer than 4
>>> v("a")
Traceback (most recent call last):
...
ValueError: 'a' is shorter than 2
>>> v = CheckLength(max=4)
>>> v("abc")
'abc'
>>> v("abcde")
Traceback (most recent call last):
...
ValueError: 'abcde' is longer than 4
>>> v("a")
'a'
>>> v = CheckLength(min=2)
>>> v("abc")
'abc'
>>> v("abcde")
'abcde'
>>> v("a")
Traceback (most recent call last):
...
ValueError: 'a' is shorter than 2
Instance Methods [hide private]
 
__init__(self, min=None, max=None)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
make_validation_error_msg(self, bad_val, err)
Generate an meaningful error message for a length problem.
source code
 
validate_value(self, value)
Check a value is of the desired form.
source code

Inherited from basevalidator.BaseValidator: __call__, convert, convert_value, make_conversion_error_msg, 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, min=None, max=None)
(Constructor)

source code 
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Overrides: object.__init__
(inherited documentation)

make_validation_error_msg(self, bad_val, err)

source code 
Generate an meaningful error message for a length problem.
Overrides: basevalidator.BaseValidator.make_validation_error_msg

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