GwValidatorsPattern

This pattern allows plugins to register validators, which can be used to hash and validate python objects.

A validator can be configured to use a specific hash algorithm and hash specific attributes of an given object only. This maybe necessary, if unhashable python object types are used inside given object.

Note

GwValidatorsPattern uses the pickle function of Python to build a hashable, binary-based representation of your data. There are some data types, which can not be pickeled. In this case the validator must be configured to ignore these specific attributes of your data.

Register a new validator

To register a new validator, a plugin must inherit from GwValidatorsPattern and use register() for registration:

from groundwork_validation.patterns import GwValidatorsPattern

 class My_Plugin(GwValidatorsPattern):
     def __init__(self, app, **kwargs):
         self.name = "My_Plugin"
         super(My_Plugin, self).__init__(app, **kwargs)

     def activate(self):
         self.validator = self.validators.register("my_validator", "test validator")

Creating a hash

Hashes can be build for nearly each python object by using hash():

class My_Plugin(GwValidatorsPattern):
    ...

    def get_hash(self):
         data = "test this"
         self.my_hash = self.validator.hash(data)

Validate an object by given hash

To validate an object, all you need is the hash and the function validate():

class My_Plugin(GwValidatorsPattern):
    ...

    def validate_hash(self):
        data = "test this"
        if self.validator.validate(data, self.my_hash) is True:
            print("Data is valid")
        else:
            print("Data is invalid. We stop here!")
            sys.exit(1)

Note

The plugin developer is responsible for safely storing hashes (e.g. inside a database).