Skip to content

Validator

The validator module is designed as an extension of a Visitor pattern implementation, aptly named Visitor. It introduces a specialized class Validator that inherits from the Visitor base class. The primary purpose of this module is to provide a mechanism for validating objects or data structures, leveraging the visitor pattern to apply validation logic selectively based on the type of element being visited.

In the context of the Validator class, a notable feature is the visit_method_prefix which is set to 'validate'. This indicates that the methods responsible for performing the validation on the visited elements should have names prefixed with 'validate'. Doing so allows the Validator to dynamically determine the correct validation method to invoke based on the type of the element encountered during the traversal process.

The __get__ special method within the module is overridden to enhance descriptors for class methods. When instance is None, it redirects the call to the classmethod.__get__ thereby enabling the __get__ method to be utilized as a class method. Otherwise, it invokes the __func__.__get__ for the instance, facilitating the use of the descriptor protocol to properly bind functions to instances.

Overall, the validator module and its Validator class are instrumental in creating structured, type-specific validation logic in a manner that is consistent with the visitor design pattern. It is suitable for scenarios where diverse objects or data structures require a set of validation rules applied contextually, depending on their types.

Validator

Bases: Visitor

A class that represents a Validator which is a type of Visitor specifically designed for validation purposes. This class inherits from the base Visitor class and utilizes a specific method prefix 'validate' for its visit methods. The Validator class serves as a framework for creating concrete validator classes that can perform validations on different components or objects by implementing the respective validate prefixed methods.

Attributes:

Name Type Description
visit_method_prefix str

A class-level attribute that defines the prefix for the visit methods used in validation. The default prefix is set to 'validate'. This class does not have its own constructor and relies on the initialization process of the Visitor class. It is intended to be subclassed by other classes that implement the actual validation logic for specific objects or components. The Validator class itself does not define any methods. Subclasses are responsible for providing implementations for the various validate methods that correspond to the particular elements or constructs they are designed to validate. In a typical usage scenario, an instance of a subclass of Validator would be passed around in a context where various objects are to be validated. As each object is encountered, the corresponding validate_ method of the Validator subclass would be invoked to perform the necessary validation checks.

Source code in stateforward/model/validator.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Validator(Visitor, visit_method_prefix="validate"):
    """
    A class that represents a Validator which is a type of Visitor specifically designed for validation purposes.
    This class inherits from the base `Visitor` class and utilizes a specific method prefix 'validate' for its visit methods. The `Validator` class serves as a framework for creating concrete validator classes that can perform validations on different components or objects by implementing the respective `validate` prefixed methods.

    Attributes:
        visit_method_prefix (str):
             A class-level attribute that defines the prefix for the visit methods used in validation. The default prefix is set to 'validate'.
            This class does not have its own constructor and relies on the initialization process of the `Visitor` class. It is intended to be subclassed by other classes that implement the actual validation logic for specific objects or components.
            The `Validator` class itself does not define any methods. Subclasses are responsible for providing implementations for the various `validate` methods that correspond to the particular elements or constructs they are designed to validate.
            In a typical usage scenario, an instance of a subclass of `Validator` would be passed around in a context where various objects are to be validated. As each object is encountered, the corresponding `validate_` method of the `Validator` subclass would be invoked to perform the necessary validation checks.

    """

    pass