Skip to main content

Minimal validation framework

Project description

Minimal Validator

minimal_validator is a very simple validation framework for python.

Installation

Installation into a virtualenv is recommended. Also see pipenv.

  • Install from pipy as external dependency: $ pip install minimal-validator
  • Install source locally: $ pip install -e .
  • Run unit tests: $ python setup.py test

Overview

Minimal validator is a very simple validation framework. The framework expects to run through a list of validation functions. Each validation function must have at least two parameters: attribute and data. The attribute is a string representing the attribute in the data object to be validated. Each validation function returns a Result object. Several validation functions for the same attribute can be chained together with the combine_validators function. The combine_validation_results function is used to perform all of the validations against a given data object.

Each Result has a keep_checking boolean that defaults to True. For a given attribute, if keep_checking is True, the validate_sequentially function of combine_validators will continue gathering results. If it's False, validate_sequentially will not run any subsequent validations in the list. The idea is that some errors make it impossible to continue validating. For example, if an attribute is not set, then no further validation logic can be applied to it. On the other hand, for something like a password, a number of validations such as "minimum length" and "presence of special characters", etc. can be applied indepdendently. In that case the final result of the validation will include all such errors rather than stopping at the first error encountered.

Examples

The following is an example validate_username_and_password function (along with a few helper functions) taken from the unit tests:

def value_has_min_length_6(attribute, data):
    return value_has_min_length(attribute, data, 6) 

def value_has_max_length_12(attribute, data):
    return value_has_max_length(attribute, data, 12) 

def value_has_at_least_one_uppercase_char(attribute, data):
    return value_matches_at_least(attribute, data, 
        list(string.ascii_uppercase), 1)

def value_has_at_least_one_special_symbol(attribute, data):
    return value_matches_at_least(attribute, data,
        list('!@#$%^&*'), 1)

@pytest.fixture
def validate_username_and_password():
    def validate(data):
        username_validators = combine_validators('username', 
            data, [attribute_exists, 
                value_is_set, 
                value_is_valid_email])        

        password_validators = combine_validators('password',
            data, [attribute_exists,
                value_is_set,
                value_has_min_length_6,
                value_has_max_length_12,
                value_has_at_least_one_uppercase_char,
                value_has_at_least_one_special_symbol])

        results = combine_validation_results(
            username_validators, 
            password_validators)

        return [result.to_dict() for result in results]            

    return validate

While the framework includes some validation functions, it will accept any function that takes attribute and data parameters and returns a valid Result object (or None if the validation passes and no validation messages are needed).

If you just need a single validation function for a given attribute, you can just wrap that function in a lambda instead of using combine_validators.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

minimal_validator-0.1.9.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

minimal_validator-0.1.9-py3-none-any.whl (4.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page