Skip to main content

A slim validation library with a very elegant API designed to afford the least amount of typing.

Project description

# Valhalla
[![Build Status](](
[![Coverage Status](](

The API is designed to afford the programmer the least amount of typing for each
use case, with the option to be more verbose when necessary.

There are 2 ways to build a schema:

- Declarative
- Dict-based

### Declarative Schemas


from valhalla import Schema

s = Schema(match=['password', 'password_confirm'])
s.password.text(min_len=8, max_len=20)


Note: The field names are added dynamically, so long as you don't collide with any of the built-in ```Schema ``` attributes. Calling the field name is not necessary. Calling the filter functions IS necessary, as shown above.

The built-in ``` Schema ``` attributes are: [errors, valid, results, add_filter_chain, validate, reset] so collision should not be an issue.


test_data = {
'email_address': '',
'first_name': 'Peter',
'last_name': 'Elias',
'password': '1234',
'password_confirm': '12345'

assert_false(s.valid) # True
assert_false(s.password.valid) # True
print s.password.errors # This field must match [password_confirm]
assert_true(s.location.valid) # True, field is not required

print s.results # {u'email_address': u''} etc... only yields valid values.

# Field-wide options may specified at the Schema-level or at the Field-level, field-level takes precedence.
s = Schema(require=['some_field'])
s.some_field.require(False) # overrides the schema setting

# a more useful example...

s = Schema(require='all')

# Field-wide options: [blank, require, match, alts]
s = Schema(match=[('match_me', 'to_me'), ('and_me', 'to_other_me')],
blank='all', required='all',
alts=[('match_me', 'some_random_name')])

# these two must match

# and these two must match

# May also be used declaratively
s = Schema()
s.i_am_required.require(True) # all fields are required by default though
s.i_can_be_blank.blank(True) # all fields CANNOT be blank by default

The difference between BLANK and REQUIRED is that a field must be included in the supplied DATA in order to be considered "present". If a value is not "present", and the field is required, an error is raised. If the value is "present", but BLANK (meaning some kind of empty value), and the field does NOT allow blanks, then an error is raised.

So, if a field is NOT REQUIRED, and NOT BLANK. Then you can omit the field entirely, but you cannot supply it with an empty value either.
# either supply me with a real value or leave me alone. this is the default.

### Dict-Based Schema Definitions (New in v0.0.7)

So these are really fun, especially if you are a functional programmer at heart...

``` python

my_definition = {
'email': ['require', ('alt', 'email_address'), 'email'], # email address with alternate name
'age': ['require', 'numeric', ('range', 13, 100)] # age must be numeric between 13 and 100
'password': [('text', 10, 50)],
'password_confirm': [('match', 'password')]

s = Schema.from_dict(my_definition)
s.validate(some_data) # Bam!


I know, really cool. Also probably really confusing if you didn't spot the pattern right away...

Explanation: keys are field names, options follow in a list. If you want to call an option without arguments,
simply specify it as a string. If you want to call an option (filter or modifier) with arguments, you use a tuple.

For example, the following two blocks are equivalent:

``` python

s = Schema()
s.my_happy_place.require().text(min_len=15, max_len=25)


same as

``` python

sdict = {
'my_happy_place': ['require', ('text', 15, 25)]

s = Schema.from_dict(sdict)


The only and obvious downside of dict-based definitions is that they require you to know the argument order of most of the filters. Luckily, most filters do not even take arguments and the ones that do are fairly obvious / intuitive / documented :)

The ``` from_dict ``` method takes ``` **kwargs ``` so you can just pass your Schema level options like ``` force_unicode=True ``` there.

### Schema API

Schema.from_dict(cls, dict_schema, **kwargs)

Schema.__init__(self, match=[], require=[], blank=[],
alts=[], extras='discard', force_unicode=True,
strip_missing=True, strip_blank=True)



Schema.add_filter_chain(self, name, filter_chain)

Schema.validate(self, data_dict, **kwargs)



### Filters (validators)

There are currently 42 filter functions (validators) in this library. They are spread across modules categorically, but all end up in the same namespace because of the dynamic lookup system used for the API. This is not really much of an issue, since the filters use (and will continue to use) non-ambiguous names.

The list of filters is as follows, by category:

### Web
* email
* ipv4
* url
* uri
* cidr4
* cidr6
* macaddress

### Money
* credit_card(brands=[])

### Strings
* text
* alphanumeric
* alpha
* numeric_string
* nonblank
* removespaces
* strip
* lower
* upper
* regex
* canonize
* slugify

### Numerical
* range(low, high)
* minimum(number)
* maximum(number)
* between(low, high)
* equal(number)
* zero

### Logical
* constant

### Constant
* drop_keys
* contains

### Chrono
* date(format='%m/%d/%Y')
* time(format='%I:%M:%S')
* datetime(format='%m/%d/%Y %I:%M:%S')
* time_before(deadline)
* time_after(milestone)
* time_between(milestone, deadline)

### Casting
* boolean
* strbool
* integer
* longint
* numeric
* string
* none

Project details

Release history Release notifications

This version
History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
valhalla-0.1.4.tar.gz (14.9 kB) Copy SHA256 hash SHA256 Source None Aug 1, 2014

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page