Skip to content

vedro-universe/vedro-valera-validator

Repository files navigation

Vedro Valera Validator

Codecov PyPI PyPI - Downloads Python Version

vedro-valera-validator is a plugin for the Vedro framework that utilizes the valera validator, a package designed for data validation based on d42 (district42) schemas. Valera validator provides a simple yet powerful approach to ensure your data aligns perfectly with your expectations.

Installation

Quick

For a quick installation, you can use a plugin manager as follows:

$ vedro plugin install vedro-valera-validator

Manual

To install manually, follow these steps:

  1. Install the package using pip:
$ pip3 install vedro-valera-validator
  1. Next, activate the plugin in your vedro.cfg.py configuration file:
# ./vedro.cfg.py
import vedro
import vedro_valera_validator

class Config(vedro.Config):

    class Plugins(vedro.Config.Plugins):

        class ValeraValidator(vedro_valera_validator.ValeraValidator):
            enabled = True

Usage

Here is an example scenario demonstrating how to decode a base64 encoded string:

# ./scenarios/decode_base64_encoded_string.py
import vedro
from base64 import b64decode
from d42 import schema

class Scenario(vedro.Scenario):
    subject = "decode base64 encoded string"

    def given(self):
        self.encoded = "Y3VjdW1iZXI="

    def when(self):
        self.result = {
            "result": b64decode(self.encoded)
        }

    def then(self):
        assert self.result == schema.dict({
            "result": schema.bytes(b"banana")
        })

Run the test using the command:

$ vedro run

If the expected and actual results don't match, a ValidationException will be raised, as illustrated below:

ValidationException:
 - Value <class 'bytes'> at _['result'] must be equal to b'banana', but b'cucumber' given

Additional Resources

For a detailed guide and further information, check out the documentation.