Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Background

The problem of sanitizing data (checking correctness and transforming to a useful form) is widespread throughout programming:

  • How do I verify user input is correct?

  • How do I munge data from a spreadsheet into dates and numbers?

  • How do I convert raw database fields into a programmatic object?

Ian Bicking came up with a sensible idiom for this problem, embodied in his Formencode library [formencode]: validation and conversion are one and the same thing, and can be handled by passing raw data through a chain of validators. Each validator checks and/or transforms the data and passes it on to the next.

In this spirit, konval is a package that provides:

  • a rich library of validation objects

  • base classes for easily producing custom validators

  • functions for easily using validators in a variety of ways

Status

konval is in an exploratory state, having been produced to support another package and to see if use can be got out of generalising conversion. As such. it is still an early release and the API may change. Comment is invited.

Installation

The simplest way to install konval is via easy_install [setuptools] or an equivalent program:

% easy_install konval

Alternatively the tarball can be downloaded [konval-pypi], unpacked and setup.py run:

% tar zxvf konval.tgz
% cd konval
% python set.py install

konval has no prerequisites and should work with just about any version of Python.

Using konval

A full API is included in the source distribution.

Examples

Most commonly, konval will be used to check or clean values. Failures result in exceptions being thrown:

# convert user input to a actual integer
>>> from konval import *
>>> sanitize ('1.0', ToInt())
1
>>> sanitize ('one', ToInt())
Traceback (most recent call last)
...
ValueError: can't convert 'one' to integer

A single validator or list can be passed to sanitize. Failure in any will result in any exception:

# check a list has no more than 3 members
>>> sanitize (['a', 'b', 'c'], [ToLength(), IsEqualOrLess(3)])
3
# check a password is long enough
>>> sanitize ('mypass', [ToLength(), IsEqualOrMore(8)])
Traceback (most recent call last)
...
ValueError: 6 is lower than 8

Any callable object that accepts and returns a single value can be used as a validator:

>>> from string import *
>>> sanitize (" my title ", [strip, capitalize])
'My title'

A rich library of prebuilt validators is supplied:

>>> sanitize ('abcde', IsNonblank())
'abcde'
>>> sanitize (5, IsInRange(1,6))
5
>>> sanitize ('foo', ToSynonym({'foo': 'bar', 'baz': 'quux'}))
'bar'

Custom validators can easily be subclassed from a supplied base class:

class IsFoo (BaseValidator):
        def validate_value (self, value):
                if value != 'foo':
                        self.raise_validation_error (value)
                return True

Limitations

konval is aimed at a one-way transformation of data, turning user input or stored data into Python objects. Certainly it could be used in the reverse direction, but this is not a primary use case. FormEncode is based around two-way (round trip) conversion of data, so that may be a useful alternative.

The name konval was chosen because:

  1. there’s already a Python library called “sanity”

  2. out of “valcon”, “valkon”, “conval” etc. it was the one with the fewest hits on Google

Credit

Thanks to Ian Bicking for inspiration in FormEncode. Thanks also to Egor Ryabkov.

References

History

v0.3, 20110802

  • Renamed a few validators to more consistent scheme. Names should be fixed now.

v0.2, 20110726

  • Fixed incorrect example

  • Added logic validators (Or, Default, Constant)

v0.1, 20110722

  • Initial release

Metadata

Release files for konval 0.3dev

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for konval 0.3dev
File Size Uploaded
konval-0.3dev.tar.gz 146.0 kB Details

Release files / konval-0.3dev.tar.gz

Download URL konval-0.3dev.tar.gz
Size 146.0 kB
Tags Source
SHA-256 checksum
How to use checksums
fa33d1a211b6aa4e0a387fd61c40df388a30a04a3056e4b0eb896ae83217757c
BLAKE2b-256 checksum
How to use checksums
018d404c88b69ed0664090c6b85481b61ec802d969c34e26e84dea34fbf5ef04
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3dev This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page