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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file minimal_validator-0.1.9.tar.gz.

File metadata

  • Download URL: minimal_validator-0.1.9.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.5

File hashes

Hashes for minimal_validator-0.1.9.tar.gz
Algorithm Hash digest
SHA256 02d615a5ca745e4560fa627491eee26459b97d434b0644cee9d493ee4f00c097
MD5 5867755574b1e9b177b9cc6d195c2df1
BLAKE2b-256 9215da5f743ca214578fd87299466cd4040853f781b37bf2dd876c08b14b1ae9

See more details on using hashes here.

File details

Details for the file minimal_validator-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: minimal_validator-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.5

File hashes

Hashes for minimal_validator-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 42aec20c8c5bf22aad678df5b12ce5ed6f833a2a6c1b944a27c9339172e903c0
MD5 7ab72dd82dc083c5618000e3ba488b63
BLAKE2b-256 48a8c9ce551dc244624dcaef3d791aac5c93311716475da855f618267979a6b8

See more details on using hashes here.

Supported by

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