dispel.data.validators module#
A module containing value validators to ensure conformity of values.
The following validators are implemented:
RangeValidator
to ensure values are within a given rangeSetValidator
to ensure values are one of a set of allowed values
As some validators are very common, hence there are constants for convenience:
GREATER_THAN_ZERO
to allow values greater or equal than zero
- dispel.data.validators.BETWEEN_MINUS_ONE_AND_ONE = <RangeValidator: [-1, 1]>#
A validator that ensures values are between minus one and one
- Parameters:
value (Any) –
- dispel.data.validators.BETWEEN_ZERO_AND_ONE = <RangeValidator: [0, 1]>#
A validator that ensures values are between zero and one
- Parameters:
value (Any) –
- dispel.data.validators.GREATER_THAN_ZERO = <RangeValidator: [0, ∞]>#
A validator that ensures values are greater or equal than zero
- Parameters:
value (Any) –
- class dispel.data.validators.RangeValidator[source]#
Bases:
object
A range validator.
The range validator can be used to ensure a measure value is within a given range. It is specified via the
validator
argument when creatingValueDefinition
s.Examples
To create a range validator that allows all values between
-4
and7
one can use it the following way:validator >>> validator = RangeValidator(-4, 7) >>> validator <RangeValidator: [-4, 7]> >>> validator(5)
When called the range validator will raise an except only if the value is outside its range:
validator >>> validator(10) Traceback (most recent call last): ... dispel.data.validators.ValidationException: Value violated upper bound [-4, 7]: 10
The range validator can also be used with just one side by specifying only the lower or upper bound:
validator >>> RangeValidator(lower_bound=-4) <RangeValidator: [-4, ∞]> >>> RangeValidator(upper_bound=7) <RangeValidator: [-∞, 7]>
To exclude the boundaries in the range, one can set
include_lower
orinclude_upper
toFalse
:validator >>> validator = RangeValidator(lower_bound=0, include_lower=False) >>> validator <RangeValidator: (0, ∞]> >>> validator(0) Traceback (most recent call last): ... dispel.data.validators.ValidationException: Value violated lower bound (0, ∞]: 0
- lower_bound#
The lower bound of the range validator.
- upper_bound#
The upper bound of the range validator.
- include_lower#
Include the lower boundary in the range check.
- include_upper#
Include the upper boundary in the range check.
- class dispel.data.validators.SetValidator[source]#
Bases:
object
A set validator.
The set validator can be used to ensure that a value is within a particular set of values. It is specified via the
validator
argument when creatingValueDefinition
s.Examples
The most common application of the
SetValidator
is to validate survey responses and to provide additional labels for numerical responses. Assuming you have a survey that has possible responses ranging from1
to4
and you want to provide the respective labels to those responses you can achieve this in the following way:validators >>> validator = SetValidator({ ... 1: 'Not at all', ... 2: 'A little', ... 3: 'Moderately', ... 4: 'Extremely' ... }) >>> validator <SetValidator: {1: 'Not at all', 2: 'A little', 3: 'Moderately', ...}> >>> validator(1)
When calling the validator with a value not being part of the
allowed_values
the validator will raise an exception:validator >>> validator(0) Traceback (most recent call last): ... dispel.data.validators.ValidationException: Value must be one of: {1, ...}
If there are no labels for values one can simply pass a list of unique values to the validator:
validator >>> validator = SetValidator([1, 2, 3, 4]) >>> validator <SetValidator: {1, 2, 3, 4}>
- allowed_values#
The allowed values by the validator. Any value within this set will pass the validator.
- labels#
The labels for the allowed values. To get a label for a specific value consider using
get_label()
. Labels for values are specified by providing a dictionary with allowed values as keys and labels as values, e.g.>>> from dispel.data.validators import SetValidator >>> validator = SetValidator({1: 'label for 1', 2: 'label for two'})