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 |
Source code in stateforward/model/validator.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |