User Guide

Users fill the web form with necessary information, however, they often make mistakes. This is where form validation comes into play. The goal of validation is to ensure that the user provided necessary and properly formatted information needed to successfully complete an operation.

Validator

Validator is a container of validation rules that all together provide object validation. You instantiate Validator and supply it a map between attribute names being validated and list of rules. Here is an example:

credential_validator = Validator({
    'username': [required, length(max=10)],
    'password': [required, length(min=8, max=12)]
})

Validator in no way tied to object and/or class being validated, instead the only requirement is existance of attributes being validated.

Validator supports __getitem__ interface, so is applicable to dict like objects:

user = {'username': '', 'password': ''}
errors = {}
succeed = credential_validator.validate(user, errors)

Method validate returns True only in case all validation rules succeed otherwise False.

errors dictionary contains all errors reported during validation. Key corresponds to attribute name being checked, while value is a list of errors.

If you need validation check all rules for failed attribute, you need set stop attribute to False (default is to stop on first error):

succeed = credential_validator.validate(user, errors, stop=False)

Nested Validator

Validator can be nested into some other validator, so ultimately can form any hierarchically complex structure. This can be useful for composite objects, e.g. Registration model can aggregate Credential model. While each model has own validation, registration model can nest the validator for the credential model:

class Registration(object):
    def __init__(self):
        self.credential = Credential()

registration_validator = Validator({
    'credential': credential_validator
})

Internationalization

Validator supports the python standard gettext module. You need to pass gettext translations as a argument to validate method. Here is an example:

from gettext import NullTranslations

translations = NullTranslations()
succeed = credential_validator.validate(
        user,
        errors,
        translations=translations)

Thread Safety

Validator does not alter its state once initialized. It is guaranteed to be thread safe.

Validation Rules

Validation rules prevent bad data from being processed. A validation rule is a criterion used in the process of data validation. Rules support simple types attributes as well as list typ attributes, e.g. iterator rule can apply a number of other rules to each item in the list.

There are a number of validation rules already defined.

  • required. Any value evaluated to boolean True passes this rule. Also take a look at the required_but_missing list. See RequiredRule.
  • not_none. None value will not pass this rule. See NotNoneRule.
  • missing, empty. Any value evaluated to boolean False passes this rule. Also take a look at the required_but_missing list. See RequiredRule.
  • length. Result of python function len() must fall within a range defined by this rule. Supported range attributes include: min, max. See LengthRule.
  • compare. Compares attribute being validated with some other attribute value. Supported comparison operations include: equal, not_equal. See CompareRule.
  • predicate, model_predicate. Fails if predicate returns boolean False. Predicate is any callable that accepts a model and returns a boolean. It is useful for custom rules, e.g. a number of days between two model properties must not exceed a certain value, etc. See PredicateRule.
  • must, value_predicate. Fails if predicate returns boolean False. Predicate is any callable that accepts a value and returns a boolean. It is useful for custom rule applicable to multiple attributes of model. See ValuePredicateRule.
  • regex. Search for regular expression pattern. Initialized with regex as a regular expression pattern or a pre-compiled regular expression. Supports negated argument. See RegexRule.
  • slug. Ensures only letters, numbers, underscores or hyphens. See SlugRule.
  • email. Ensures a valid email. See EmailRule.
  • scientific. Ensures a valid scientific string input. See ScientificRule.
  • base64. Ensures a valid base64 string input (supports alternative alphabet for + and / characters). See Base64Rule.
  • urlsafe_base64. Ensures a valid base64 string input using an alphabet, which substitutes - instead of + and _ instead of / in the standard Base64 alphabet. The input can still contain =. See URLSafeBase64Rule.
  • range. Ensures value is in range defined by this rule. Works with any numbers including int, float, decimal, etc. Supported range attributes include: min, max. See RangeRule.
  • and_. Applies all rules regardless of validation result. See AndRule.
  • or_. Succeeds if at least one rule in rules succeed. Failed rule results are not added unless they all fail. See OrRule.
  • iterator. Applies rules to each item in value. Iterates over each rule and checks whenever any item in value fails. Designed to work with iteratable attributes: list, tuple, etc. See IteratorRule.
  • one_of. Value must match at least one element from items. Checks whenever value belongs to items. See OneOfRule.
  • relative_date, relative_utcdate, relative_tzdate, relative_datetime, relative_utcdatetime, relative_tzdatetime. Check if value is in relative date/datetime range per local, UTC or tz time. See RelativeDateDeltaRule, RelativeUTCDateDeltaRule, RelativeTZDateDeltaRule and RelativeDateTimeDeltaRule, RelativeUTCDateTimeDeltaRule, RelativeTZDateTimeDeltaRule.
  • relative_timestamp, relative_unixtime. Check if value is in relative unix timestamp range. See RelativeUnixTimeDeltaRule.
  • adapter, int_adapter. Adapts a value according to converter. This is useful when you need to keep string input in model but validate as an integer. See AdapterRule, IntAdapterRule.
  • ignore. The idea behind this rule is to be able to substitute any validation rule by this one that always succeeds. See IgnoreRule.

Custom Message

You are able to customize the error message by using message_template argument during rule declaration:

credential_validator = Validator({
    'username': [required(message_template='Required field')]
})

Every rule supports message_template argument during rule declaration.

gettext utilities

Please remember to add msgid/msgstr of customized validation error to po file. You can extract gettext messages by:

$ xgettext --join-existing --sort-by-file --omit-header \
            -o i18n/translations.po src/*.py

Compile po files:

$ msgfmt -v translations.po -o translations.mo

Custom Rules

It is easy to provide your own validation rule. The rule is any callable with the following contract:

def check(self, value, name, model, result, gettext):

Here is a description of each attribute:

  • value - value that is currently validated.
  • name - name of attribute.
  • model - object being validated.
  • result - a dictionary that accepts validation errors.
  • gettext - a function used to provide i18n support.

Validation Mixin

ValidationMixin provides a sort of contextual integration with third party modules. Specifically this mixin requires two attributes: errors and translations. Once these two attributes provided, validation can be simplified. Let’s review it by example:

user_validator = Validator({
    'name': [required]
})

We defined user_validator. Now here is our integration in some service:

class MyService(ValidationMixin):

     def __init__(self):
         self.errors = {}
         self.translations = {'validation': None}

     def signin(self, user):
         succeed = self.validate(user, user_validator)
         ...
         self.error('Unauthorized')
         return False

If the signin operation fails the client can request all validation errors from errors attribute. Note that general error message (‘Unauthorized’) is stored under __ERROR__ key. Thus it can be used to display general information to the end user.

Model Update

Web form submit is a dictionary where key is the name of the input element being submitted and value is a list. That list can have just a single value for elements like input or several values that depict user choice.

try_update_model() method is provided to try update any given object with values submitted by web form.

The convention used by try_update_model() method is requirement for the model to be properly initialized with default values, e.g. integer attributes must default to some integer value, etc.

List of supported value_providers:

        try:
            return date(*strptime(
                value,
                default_date_input_format(gettext))[:3])
        except ValueError:
            for fmt in fallback_date_input_formats(gettext).split('|'):
                try:
                    return date(*strptime(value, fmt)[:3])
                except ValueError:
                    continue
            raise ValueError()

Example of domain model initialized with defaults:

class Credential(object):

    def __init__(self):
        self.username = ''
        self.password = ''

Values submitted by web form:

values = {'username': [''], 'password': ['']}

Typical use case as follows:

from wheezy.validation.model import try_update_model

credential = Credential()
errors = {}
succeed = try_update_model(credential, values, errors)

errors dictionary contains all errors reported during model update. Key corresponds to attribute being updated, while value is a list of errors.

Numbers

Number value providers ( int_value_provider(), decimal_value_provider(), float_value_provider()) supports thousands separator as well as decimal separator. Take a look at the validation.po file.

Date and Time

Date and time value providers ( date_value_provider(), time_value_provider(), datetime_value_provider()) support a number of formats. Generally there is a default format and fallback formats. It tries the default format and if it fails tries fallback formats. Take a look at the validation.po file for a list of supported formats.

Please note that datetime_value_provider() falls back to date_value_provider() in case none of its own formats matched. Empty value is converted to minimal value for date/time.

Lists

try_update_model() method supports list attributes. Note that existing model list is used (it is not overwritten).

>>> class User(object):
...     def __init__(self):
...         self.prefs = []
...         self.prefs2 = [0]
>>> user = User()
>>> values = {'prefs': ['1', '2'], 'prefs2': ['1', '2']}
>>> results = {}
>>> try_update_model(user, values, results)
True
>>> user.prefs
['1', '2']
>>> user.prefs2
[1, 2]

Note that the type of the first element in the list selects value_provider for all elements in the list.

Custom Value Providers

Value provider is any callable of the following contract:

def my_value_provider(str_value, gettext):
    return parsed_value

You can add your value provider to defaults:

from wheezy.validation.model import value_providers

value_providers['my_type'] = my_value_provider