Modules

wheezy.validation

wheezy.validation.checker

checker module.

class wheezy.validation.checker.Checker(stop=True, translations=None, gettext=None)[source]

Intended to be used by unittest/doctest for validation rules. It is recommended to use test case per validator, test method per attribute, split by success check first than fails.

check(**kwargs)[source]

Returns a result of validation limited to attributes in kwargs which represents attributes of model being validated.

error(**kwargs)[source]

Returns first error reported by validator.

errors(**kwargs)[source]

Returns all errors reported by validator.

use(validator)[source]

Use validator for next series of checks.

class wheezy.validation.checker.Model[source]

Simulate plain python class, read-only dictionary access through attributes.

wheezy.validation.mixin

mixin module.

class wheezy.validation.mixin.ErrorsMixin[source]

Used primary by service layer to validate business rules.

Requirements: - self.errors

Example:

class MyService(ValidationMixin):

    def __init__(self, repository, errors, locale):
        # ...

    def authenticate(self, credential):
        if not self.factory.membership.authenticate(credentials):
            self.error('The username or password provided '
                       'is incorrect.')
            return False
        # ...
        return True
error(message, name='__ERROR__')[source]

Add message to errors.

class wheezy.validation.mixin.ValidationMixin[source]

Used primary by service layer to validate domain model.

Requirements: - self.errors - self.translations

Example:

class MyService(ValidationMixin):

    def __init__(self, repository, errors, translations, locale):
        # ...

    def authenticate(self, credential):
        if not self.validate(credential, credential_validator):
            return False
        # ...
        return True
error(message, name='__ERROR__')[source]

Add message to errors.

validate(model, validator)[source]

Validate given model using validator.

wheezy.validation.model

model module.

wheezy.validation.model.bool_value_provider(value, gettext)[source]

Converts value to bool.

wheezy.validation.model.bytes_value_provider(value, gettext)[source]

Converts value to bytes.

wheezy.validation.model.date_value_provider(value, gettext)[source]

Converts value to datetime.date.

wheezy.validation.model.datetime_value_provider(value, gettext)[source]

Converts value to datetime.datetime.

wheezy.validation.model.decimal_value_provider(value, gettext)[source]

Converts value to Decimal.

wheezy.validation.model.float_value_provider(value, gettext)[source]

Converts value to float.

wheezy.validation.model.int_value_provider(value, gettext)[source]

Converts value to int.

wheezy.validation.model.str_value_provider(value, gettext)[source]

Converts value to str.

wheezy.validation.model.time_value_provider(value, gettext)[source]

Converts value to datetime.time.

wheezy.validation.model.try_update_model(model, values, results, translations=None)[source]

Try update model with values (a dict of lists or strings), any errors encountered put into results and use translations for i18n.

wheezy.validation.patches

patches module.

wheezy.validation.patches.patch_strptime_cache_size(max_size=100)[source]

Patch for strptime regex cache max size.

wheezy.validation.patches.patch_use_cdecimal()[source]

Use cdecimal module globally. Pure python implementation in-place replacement.

wheezy.validation.rules

rules module.

class wheezy.validation.rules.AdapterRule(converter, rule, message_template=None)[source]

Adapts value according to converter. This is useful when you need keep string input in model but validate as an integer.

class wheezy.validation.rules.AndRule(*rules)[source]

Applies all rules regardless of validation result.

validate(value, name, model, result, gettext)[source]

Iterate over each rule and check whenever any item in value fail.

value - iteratable.

class wheezy.validation.rules.Base64Rule(altchars='+/', message_template=None)[source]

Ensures a valid base64 string input.

class wheezy.validation.rules.CompareRule(equal=None, not_equal=None, message_template=None)[source]

Compares attribute being validated with some other attribute value.

class wheezy.validation.rules.EmailRule(message_template=None)[source]

Ensures a valid email.

class wheezy.validation.rules.IgnoreRule(*args, **kwargs)[source]

The idea behind this rule is to be able to substitute any validation rule by this one that always succeed:

from wheezy.validation.rules import ignore as regex

This way all regex rules are ignored within a scope of import.

validate(value, name, model, result, gettext)[source]

Always succeed.

class wheezy.validation.rules.IntAdapterRule(rule, message_template=None)[source]

Adapts value to an integer.

class wheezy.validation.rules.IteratorRule(rules, stop=True)[source]

Applies rules to each item in value list.

validate(value, name, model, result, gettext)[source]

Iterate over each rule and check whenever any item in value fail.

value - iteratable.

class wheezy.validation.rules.LengthRule(min=None, max=None, message_template=None)[source]

Result of python function len() must fall within a range defined by this rule.

class wheezy.validation.rules.MissingRule(message_template=None)[source]

Any value evaluated to boolean False pass this rule.

class wheezy.validation.rules.NotNoneRule(message_template=None)[source]

None value will not pass this rule.

class wheezy.validation.rules.OneOfRule(items, message_template=None)[source]

Value must match at least one element from items. Checks are case sensitive if items are strings.

validate(value, name, model, result, gettext)[source]

Check whenever value belongs to self.items.

class wheezy.validation.rules.OrRule(*rules)[source]

Succeed if at least one rule in rules succeed.

validate(value, name, model, result, gettext)[source]

Iterate over each rule and check whenever value fail. Stop on first succeed.

class wheezy.validation.rules.PredicateRule(predicate, message_template=None)[source]

Fails if predicate return False. Predicate is any callable of the following contract:

def predicate(model):
    return True
class wheezy.validation.rules.RangeRule(min=None, max=None, message_template=None)[source]

Ensures value is in range defined by this rule.

Works with any numbers including Decimal.

class wheezy.validation.rules.RegexRule(regex, negated=False, message_template=None)[source]

Search for regular expression pattern.

class wheezy.validation.rules.RelativeDateDeltaRule(min=None, max=None, message_template=None)[source]

Check if value is in relative date range local time.

class wheezy.validation.rules.RelativeDateTimeDeltaRule(min=None, max=None, message_template=None)[source]

Check if value is in relative datetime range local time.

class wheezy.validation.rules.RelativeDeltaRule(min=None, max=None, message_template=None)[source]

Check if value is in relative date/time range.

>>> r = RelativeDeltaRule()
>>> r.now() 
Traceback (most recent call last):
    ...
NotImplementedError: ...
class wheezy.validation.rules.RelativeTZDateDeltaRule(min=None, max=None, tz=None, message_template=None)[source]

Check if value is in relative date range TZ time.

class wheezy.validation.rules.RelativeTZDateTimeDeltaRule(min=None, max=None, tz=None, message_template=None)[source]

Check if value is in relative date range TZ time.

class wheezy.validation.rules.RelativeUTCDateDeltaRule(min=None, max=None, message_template=None)[source]

Check if value is in relative date range UTC time.

class wheezy.validation.rules.RelativeUTCDateTimeDeltaRule(min=None, max=None, message_template=None)[source]

Check if value is in relative datetime range UTC time.

class wheezy.validation.rules.RelativeUnixTimeDeltaRule(min=None, max=None, message_template=None)[source]

Check if value is in relative unix range local time.

class wheezy.validation.rules.RequiredRule(message_template=None)[source]

Any value evaluated to boolean True pass this rule. You can extend this validator by supplying additional false values to required_but_missing list.

class wheezy.validation.rules.ScientificRule(message_template=None)[source]

Ensures a valid scientific string input.

class wheezy.validation.rules.SlugRule(message_template=None)[source]

Ensures only letters, numbers, underscores or hyphens.

class wheezy.validation.rules.URLSafeBase64Rule(message_template=None)[source]

Ensures a valid base64 URL-safe string input using an alphabet, which substitutes - instead of + and _ instead of / in the standard Base64 alphabet. The input can still contain =.

class wheezy.validation.rules.ValuePredicateRule(predicate, message_template=None)[source]

Fails if predicate return False. Predicate is any callable of the following contract:

def predicate(value):
    return True
wheezy.validation.rules.adapter

alias of AdapterRule

wheezy.validation.rules.and_

alias of AndRule

wheezy.validation.rules.compare

alias of CompareRule

wheezy.validation.rules.ignore

alias of IgnoreRule

wheezy.validation.rules.int_adapter

alias of IntAdapterRule

wheezy.validation.rules.iterator

alias of IteratorRule

wheezy.validation.rules.length

alias of LengthRule

wheezy.validation.rules.model_predicate

alias of PredicateRule

wheezy.validation.rules.must

alias of ValuePredicateRule

wheezy.validation.rules.one_of

alias of OneOfRule

wheezy.validation.rules.or_

alias of OrRule

wheezy.validation.rules.predicate

alias of PredicateRule

wheezy.validation.rules.range

alias of RangeRule

wheezy.validation.rules.regex

alias of RegexRule

wheezy.validation.rules.relative_date

alias of RelativeDateDeltaRule

wheezy.validation.rules.relative_datetime

alias of RelativeDateTimeDeltaRule

wheezy.validation.rules.relative_timestamp

alias of RelativeUnixTimeDeltaRule

wheezy.validation.rules.relative_tzdate

alias of RelativeTZDateDeltaRule

wheezy.validation.rules.relative_tzdatetime

alias of RelativeTZDateTimeDeltaRule

wheezy.validation.rules.relative_unixtime

alias of RelativeUnixTimeDeltaRule

wheezy.validation.rules.relative_utcdate

alias of RelativeUTCDateDeltaRule

wheezy.validation.rules.relative_utcdatetime

alias of RelativeUTCDateTimeDeltaRule

wheezy.validation.rules.value_predicate

alias of ValuePredicateRule

wheezy.validation.validator

validator module.

class wheezy.validation.validator.Validator(mapping)[source]

Container of validation rules that all together provide object validation.

validate(model, results, stop=True, translations=None, gettext=None)[source]

Validates given model with results of validation stored in results. Be default the validation stops on first rule fail, however with supplied stop argument set False the result will get all errors reported by a rule.

There is a way to internationalize validation errors with translations or gettext.