Skip to main content

A library for runtime type checking and validation of python values

Project description

Build Status Coverage

A python library for runtime type checking and validation of python values.

Intended as a stepping stone towards static typing.

Developed at Joivy Ltd and open-sourced with permission. Development temporarily moved from JOIVY/validation to bwhmather/python-validation in order to enable continuous integration.

Installation

Recommended method is to use the version from pypi

$ pip install validation

Please note that this library only supports python version 2.7, and versions 3.4 and later.

Usage

A toy example demonstrating typical usage:

from validation import (
    validate_int, validate_float,
    validate_structure,
    validate_text,
)


def function(int_arg, dict_arg, unicode_arg=None):
    """
    A normal function that expects to be called in a particular way.

    :param int int_arg:
        A non-optional integer.  Must be between one and ten.
    :param dict dict_arg:
        A dictionary containing an integer id, and a floating point amount.
    :param str unicode_arg:
        An optional string.
    """
    # Validate arguments.
    validate_int(int_arg, min_value=0, max_value=10)
    validate_structure(dict_arg, schema={
        'id': validate_int(min_value=0)
        'amount': validate_float(),
    })
    validate_text(unicode_argument, required=False)

    # Do something.
    ...

The validation functions are used to check arguments passed to a public function. Exceptions raised by the validation functions are allowed to propagate through. No logic is run until validation is complete.

License

The project is made available under the terms of the Apache 2.0 license. See LICENSE for details.

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

validation-0.3.0.zip (28.9 kB view hashes)

Uploaded Source

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