pyvaru |
|
pyvaru.rules |
Core API¶
-
class
pyvaru.
ValidationRule
(apply_to: object, label: str, error_message: str = None, stop_if_invalid: bool = False)¶ Base abstract rule class from which concrete ones must inherit from.
Parameters: - apply_to (object) – Value against which the rule is applied (can be any type).
- label (str) – Short string describing the field that will be validated (e.g. “phone number”, “user name”...). This string will be used as the key in the ValidationResult error dictionary.
- error_message (str) – Custom message that will be used instead of the “default_error_message”.
- stop_if_invalid (bool) – True to prevent Validator from processing the rest of the get_rules if the current one is not respected, False (default) to collect all the possible errors.
-
apply
() → bool¶ Abstract method that must be implemented by concrete get_rules in order to return a boolean indicating whether the rule is respected or not.
Returns: True if the rule is respected, False otherwise Return type: bool
-
default_error_message
= 'Data is invalid.'¶ Default error message for the rule (class attribute).
-
get_error_message
() → str¶ Returns the message that will be used by the validator if the rule is not respected. If a custom error message is provided during rule instantiation that one will be used, otherwise the default one.
Returns: Error message Return type: str
-
class
pyvaru.
ValidationResult
(errors: dict = None)¶ Represents a report of Validator’s validate() call.
Parameters: errors (dict) – Map containing errors descriptions (if one ore more get_rules are not respected) -
annotate_exception
(exception: Exception, rule: pyvaru.ValidationRule = None) → None¶ Takes note of an exception occurred during validation. (Typically caused by an invalid attribute/key access inside get_rules() method)
Parameters: - exception (Exception) – Exception catched during validate() phase.
- rule (ValidationRule) – Validation rule that has generated the exception.
Returns: None
-
annotate_rule_violation
(rule: pyvaru.ValidationRule) → None¶ Takes note of a rule validation failure by collecting its error message.
Parameters: rule (ValidationRule) – Rule that failed validation. Returns: None
-
is_successful
() → bool¶ Checks that the validation result does not contain errors.
Returns: True if the validation is successful, False otherwise. Return type: bool
-
-
exception
pyvaru.
ValidationException
(validation_result: pyvaru.ValidationResult, message: str = 'Data did not validate.')¶ Internal exception used by the library to represent a validation failure when using a Validator as a context processor.
Parameters: - validation_result (ValidationResult) – Validation result returned by validator.
- message (str) – Error message
-
with_traceback
()¶ Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
-
class
pyvaru.
Validator
(data: object)¶ Validate a data model against a list of ValidationRule(s). This class is abstract, concrete validators must inherit from Validator in order to provide a an actual implementation of get_rules().
Parameters: data (object) – Data model to validate (like a dict or a custom Python object instance). -
get_rules
() → list¶ Concrete validators must implement this abstract method in order to return a list of ValidationRule(s), that will be used to validate the model.
Returns: ValidationRule list Return type: list
-
validate
() → pyvaru.ValidationResult¶ Apply the configured ValidationRule(s) (in the given order) and return a ValidationResult object.
Returns: validation result Return type: ValidationResult
-
-
class
pyvaru.
JoinType
¶ An enumeration.
-
class
pyvaru.
RuleGroup
(apply_to: object, label: str, rules: list, error_message: str = None, stop_if_invalid: bool = False)¶ Allows the execution of multiple rules sequentially.
Example: >>> rules = [ >>> (TypeRule, {'valid_type': list}), >>> (MinLengthRule, {'min_length': 1}), >>> UniqueItemsRule >>> ] >>> group = RuleGroup(apply_to=countries, label='Countries', rules=rules)
Parameters: - apply_to (object) – Value against which the rule is applied (can be any type).
- label (str) – Short string describing the field that will be validated (e.g. “phone number”, “user name”...). This string will be used as the key in the ValidationResult error dictionary.
- rules (list) – List of rules to execute. The list can contain rule type (ie: FullStringRule, MinValueRule...) or tuples in the format: “(RuleClass, options)” (ie: “(MinLengthRule, {‘min_length’: 1})”)
- error_message (str) – Custom message that will be used instead of the “default_error_message”.
- stop_if_invalid (bool) – True to prevent Validator from processing the rest of the get_rules if the current one is not respected, False (default) to collect all the possible errors.
Generic validation rules¶
-
class
pyvaru.rules.
TypeRule
(apply_to: object, label: str, valid_type: type, error_message: str = None, stop_if_invalid: bool = False)¶ Ensure that the target value is an instance of the given type.
Parameters: - apply_to (object) – Value against which the rule is applied (can be any type).
- label (str) – Short string describing the field that will be validated (e.g. “phone number”, “user name”...). This string will be used as the key in the ValidationResult error dictionary.
- valid_type (type) – Valid class
- error_message (str) – Custom message that will be used instead of the “default_error_message”.
- stop_if_invalid (bool) – True to prevent Validator from processing the rest of the get_rules if the current one is not respected, False (default) to collect all the possible errors.
-
default_error_message
= 'Object is not an instance of the expected type.'¶ Default error message for the rule.
-
get_error_message
() → str¶ Returns the message that will be used by the validator if the rule is not respected. If a custom error message is provided during rule instantiation that one will be used, otherwise the default one.
Returns: Error message Return type: str
-
class
pyvaru.rules.
FullStringRule
(apply_to: object, label: str, error_message: str = None, stop_if_invalid: bool = False)¶ Ensure that the target value is a non empty string object.
Parameters: - apply_to (object) – Value against which the rule is applied (can be any type).
- label (str) – Short string describing the field that will be validated (e.g. “phone number”, “user name”...). This string will be used as the key in the ValidationResult error dictionary.
- error_message (str) – Custom message that will be used instead of the “default_error_message”.
- stop_if_invalid (bool) – True to prevent Validator from processing the rest of the get_rules if the current one is not respected, False (default) to collect all the possible errors.
-
default_error_message
= 'String is empty.'¶ Default error message for the rule.
-
get_error_message
() → str¶ Returns the message that will be used by the validator if the rule is not respected. If a custom error message is provided during rule instantiation that one will be used, otherwise the default one.
Returns: Error message Return type: str
-
class
pyvaru.rules.
ChoiceRule
(apply_to: object, label: str, choices: tuple, error_message: str = None, stop_if_invalid: bool = False)¶ Ensure that the target value is contained in a provided list of possible options.
Parameters: - apply_to (object) – Value against which the rule is applied (can be any type).
- label (str) – Short string describing the field that will be validated (e.g. “phone number”, “user name”...). This string will be used as the key in the ValidationResult error dictionary.
- choices (tuple) – Available options.
- error_message (str) – Custom message that will be used instead of the “default_error_message”.
- stop_if_invalid (bool) – True to prevent Validator from processing the rest of the get_rules if the current one is not respected, False (default) to collect all the possible errors.
-
default_error_message
= 'Value not found in available choices.'¶ Default error message for the rule.
-
get_error_message
() → str¶ Returns the message that will be used by the validator if the rule is not respected. If a custom error message is provided during rule instantiation that one will be used, otherwise the default one.
Returns: Error message Return type: str
-
class
pyvaru.rules.
MinValueRule
(apply_to: object, label: str, min_value: float, error_message: str = None, stop_if_invalid: bool = False)¶ Ensure that the target value is >= than the provided reference value.
Parameters: - apply_to (object) – Value against which the rule is applied (can be any type).
- label (str) – Short string describing the field that will be validated (e.g. “phone number”, “user name”...). This string will be used as the key in the ValidationResult error dictionary.
- min_value (float) – Minimum value allowed.
- error_message (str) – Custom message that will be used instead of the “default_error_message”.
- stop_if_invalid (bool) – True to prevent Validator from processing the rest of the get_rules if the current one is not respected, False (default) to collect all the possible errors.
-
default_error_message
= 'Value is smaller than expected one.'¶ Default error message for the rule.
-
get_error_message
() → str¶ Returns the message that will be used by the validator if the rule is not respected. If a custom error message is provided during rule instantiation that one will be used, otherwise the default one.
Returns: Error message Return type: str
-
class
pyvaru.rules.
MaxValueRule
(apply_to: object, label: str, max_value: float, error_message: str = None, stop_if_invalid: bool = False)¶ Ensure that the target value is <= than the provided reference value.
Parameters: - apply_to (object) – Value against which the rule is applied (can be any type).
- label (str) – Short string describing the field that will be validated (e.g. “phone number”, “user name”...). This string will be used as the key in the ValidationResult error dictionary.
- max_value (float) – Maximum value allowed.
- error_message (str) – Custom message that will be used instead of the “default_error_message”.
- stop_if_invalid (bool) – True to prevent Validator from processing the rest of the get_rules if the current one is not respected, False (default) to collect all the possible errors.
-
default_error_message
= 'Value is greater than expected one.'¶ Default error message for the rule.
-
get_error_message
() → str¶ Returns the message that will be used by the validator if the rule is not respected. If a custom error message is provided during rule instantiation that one will be used, otherwise the default one.
Returns: Error message Return type: str
-
class
pyvaru.rules.
MinLengthRule
(apply_to: object, label: str, min_length: int, error_message: str = None, stop_if_invalid: bool = False)¶ Ensure that the target value has a length >= than the provided reference value. This rule can be applied to all python objects supporting len() (strings, lists, tuples, sets, dicts... and even custom types).
Parameters: - apply_to (object) – Value against which the rule is applied (can be any type).
- label (str) – Short string describing the field that will be validated (e.g. “phone number”, “user name”...). This string will be used as the key in the ValidationResult error dictionary.
- min_length (int) – Minimum length allowed.
- error_message (str) – Custom message that will be used instead of the “default_error_message”.
- stop_if_invalid (bool) – True to prevent Validator from processing the rest of the get_rules if the current one is not respected, False (default) to collect all the possible errors.
-
default_error_message
= 'Length is smaller than expected one.'¶ Default error message for the rule.
-
get_error_message
() → str¶ Returns the message that will be used by the validator if the rule is not respected. If a custom error message is provided during rule instantiation that one will be used, otherwise the default one.
Returns: Error message Return type: str
-
class
pyvaru.rules.
MaxLengthRule
(apply_to: object, label: str, max_length: int, error_message: str = None, stop_if_invalid: bool = False)¶ Ensure that the target value has a length <= than the provided reference value. This rule can be applied to all python objects supporting len() (strings, lists, tuples, sets, dicts... and even custom types).
Parameters: - apply_to (object) – Value against which the rule is applied (can be any type).
- label (str) – Short string describing the field that will be validated (e.g. “phone number”, “user name”...). This string will be used as the key in the ValidationResult error dictionary.
- max_length (int) – Maximum length allowed.
- error_message (str) – Custom message that will be used instead of the “default_error_message”.
- stop_if_invalid (bool) – True to prevent Validator from processing the rest of the get_rules if the current one is not respected, False (default) to collect all the possible errors.
-
default_error_message
= 'Length is greater than expected one.'¶ Default error message for the rule.
-
get_error_message
() → str¶ Returns the message that will be used by the validator if the rule is not respected. If a custom error message is provided during rule instantiation that one will be used, otherwise the default one.
Returns: Error message Return type: str
-
class
pyvaru.rules.
RangeRule
(apply_to: object, label: str, valid_range: range, error_message: str = None, stop_if_invalid: bool = False)¶ Ensure that the target value is contained in the provided range.
IMPORTANT: this rule handles python range() objects (and its “step” configuration), so does not support floats as test value (testing for a float will always fail and even for an integer if it doesn’t match the step increment).
For a validation like “value BETWEEN x AND y” use IntervalRule instead!
Parameters: - apply_to (object) – Value against which the rule is applied (can be any type).
- label (str) – Short string describing the field that will be validated (e.g. “phone number”, “user name”...). This string will be used as the key in the ValidationResult error dictionary.
- valid_range (range) – Allowed range.
- error_message (str) – Custom message that will be used instead of the “default_error_message”.
- stop_if_invalid (bool) – True to prevent Validator from processing the rest of the get_rules if the current one is not respected, False (default) to collect all the possible errors.
-
default_error_message
= 'Value is out of range.'¶ Default error message for the rule.
-
get_error_message
() → str¶ Returns the message that will be used by the validator if the rule is not respected. If a custom error message is provided during rule instantiation that one will be used, otherwise the default one.
Returns: Error message Return type: str
-
class
pyvaru.rules.
IntervalRule
(apply_to: object, label: str, interval_from: float, interval_to: float, error_message: str = None, stop_if_invalid: bool = False)¶ Ensure that the target value is contained in the provided interval.
Parameters: - apply_to (object) – Value against which the rule is applied (can be any type).
- label (str) – Short string describing the field that will be validated (e.g. “phone number”, “user name”...). This string will be used as the key in the ValidationResult error dictionary.
- interval_from (float) – Minimum allowed value.
- interval_to (float) – Maximum allowed value.
- error_message (str) – Custom message that will be used instead of the “default_error_message”.
- stop_if_invalid (bool) – True to prevent Validator from processing the rest of the get_rules if the current one is not respected, False (default) to collect all the possible errors.
-
default_error_message
= 'Value is not in interval.'¶ Default error message for the rule.
-
get_error_message
() → str¶ Returns the message that will be used by the validator if the rule is not respected. If a custom error message is provided during rule instantiation that one will be used, otherwise the default one.
Returns: Error message Return type: str
-
class
pyvaru.rules.
PatternRule
(apply_to: object, label: str, pattern: str, flags: int = 0, error_message: str = None, stop_if_invalid: bool = False)¶ Ensure that the target string respects the given pattern.
Parameters: - apply_to (object) – Value against which the rule is applied (can be any type).
- pattern (str) – Regex used for pattern matching.
- flags (int) – Regex flags.
- label (str) – Short string describing the field that will be validated (e.g. “phone number”, “user name”...). This string will be used as the key in the ValidationResult error dictionary.
- error_message (str) – Custom message that will be used instead of the “default_error_message”.
- stop_if_invalid (bool) – True to prevent Validator from processing the rest of the get_rules if the current one is not respected, False (default) to collect all the possible errors.
-
default_error_message
= 'Value does not match expected pattern.'¶ Default error message for the rule.
-
get_error_message
() → str¶ Returns the message that will be used by the validator if the rule is not respected. If a custom error message is provided during rule instantiation that one will be used, otherwise the default one.
Returns: Error message Return type: str
-
class
pyvaru.rules.
PastDateRule
(apply_to: object, label: str, reference_date: datetime.datetime = None, error_message: str = None, stop_if_invalid: bool = False)¶ Ensure that the target value is a past date.
Parameters: - apply_to (object) – Value against which the rule is applied.
- label (str) – Short string describing the field that will be validated (e.g. “phone number”, “user name”...). This string will be used as the key in the ValidationResult error dictionary.
- reference_date (datetime) – Date used for time checking (default to datetime.now()).
- error_message (str) – Custom message that will be used instead of the “default_error_message”.
- stop_if_invalid (bool) – True to prevent Validator from processing the rest of the get_rules if the current one is not respected, False (default) to collect all the possible errors.
-
default_error_message
= 'Not a past date.'¶ Default error message for the rule.
-
get_error_message
() → str¶ Returns the message that will be used by the validator if the rule is not respected. If a custom error message is provided during rule instantiation that one will be used, otherwise the default one.
Returns: Error message Return type: str
-
class
pyvaru.rules.
FutureDateRule
(apply_to: object, label: str, reference_date: datetime.datetime = None, error_message: str = None, stop_if_invalid: bool = False)¶ Ensure that the target value is a future date.
Parameters: - apply_to (object) – Value against which the rule is applied.
- label (str) – Short string describing the field that will be validated (e.g. “phone number”, “user name”...). This string will be used as the key in the ValidationResult error dictionary.
- reference_date (datetime) – Date used for time checking (default to datetime.now()).
- error_message (str) – Custom message that will be used instead of the “default_error_message”.
- stop_if_invalid (bool) – True to prevent Validator from processing the rest of the get_rules if the current one is not respected, False (default) to collect all the possible errors.
-
default_error_message
= 'Not a future date.'¶ Default error message for the rule.
-
get_error_message
() → str¶ Returns the message that will be used by the validator if the rule is not respected. If a custom error message is provided during rule instantiation that one will be used, otherwise the default one.
Returns: Error message Return type: str
-
class
pyvaru.rules.
UniqueItemsRule
(apply_to: object, label: str, error_message: str = None, stop_if_invalid: bool = False)¶ Ensure that the target list (or iterable) does not contain duplicated items.
Parameters: - apply_to (object) – Value against which the rule is applied (can be any type).
- label (str) – Short string describing the field that will be validated (e.g. “phone number”, “user name”...). This string will be used as the key in the ValidationResult error dictionary.
- error_message (str) – Custom message that will be used instead of the “default_error_message”.
- stop_if_invalid (bool) – True to prevent Validator from processing the rest of the get_rules if the current one is not respected, False (default) to collect all the possible errors.
-
default_error_message
= 'List contains duplicated items.'¶ Default error message for the rule.
-
get_error_message
() → str¶ Returns the message that will be used by the validator if the rule is not respected. If a custom error message is provided during rule instantiation that one will be used, otherwise the default one.
Returns: Error message Return type: str