Validators

class validators.Validator(*args, **kwargs)

Validator base class.

Validator is identified by a combination of own class name and all arguments. That is, validator is the same as other if one is instantiate from same class with same arguments.

inherit tips:
call Validator.__init__() with ALL arguments.

Note

All arguments of validator are must be serializable.

Because identifier of validator was generated by pickle.

ident

Validator’s identifier getter method (decoreted by property()).

It is string that generated from Validator’s all arguments.

It is used by __eq__() and __ne__().

__call__(value)
__eq__(other)
__ne__(other)
validate(value)

Do validate.

Parameters:value – Validatee value.
class validators.All(*validators)

AND operation for validators.

class validators.Any(*validators)

OR operation for validators.

class validators.ValueAdapter(*validators)

Adapt value to validators when validate a value.

on_adapt(value)

Value processor.

Must be return processed value.

class validators.Not(validator)

NOT operation for validator.

Raises ValidationError:
 Not raised ValidationError from the validator given at initialization.
class validators.Failure(*args, **kwargs)

Surely fail validator.

Raises ValidationError:
 Always the exception raises.
class validators.Pass(*args, **kwargs)

Surely pass validator.

class validators.Number(min=None, max=None)

Number validator.

Parameters:
  • min – Min of valid value.
  • max – Max of valid value.
validate(value)

Validate the value.

Parameters:

value (Accepted type of float().) – A number.

Raises:
  • InvalidValueErrorvalue is invalid.
  • InvalidTypeError – Type of value is invalid.
class validators.FreeText(ban_phrases=None, ignore_chars=None)

Free text validator.

Parameters:
  • ban_phrases (List of string.) – List of ban phrase.
  • ignore_chars – List of ignore string. If ignore string is found in value, erase from the value when before check ban phrases.
Raises:
  • InvalidTypeError – The type of given value is not string.
  • InvalidValueError – Ban phrase is found in the value.
class validators.Equal(eq_value)

Equal value validator.

Parameters:eq_value – If the value is the same as eq_value is evaluated as “valid”.

Note

If type of value is str and type of eq_value is unicode, the value is treated as UTF-8 string.

Raises InvalidValueError:
 The value is not equal to eq_value or Failed decode eq_value to UTF-8.
class validators.Regex(regexp, is_match=True, flags=None)

Value validation by regexp.

Parameters:
  • regexp – Regular expression.
  • is_match

    If True, use re.match(). Otherwise use re.search().

    This flag is True by default.

  • flags

    Sequence of flags. Acceptable types are: list, tuple, set, frozenset, basestring

    • “i”: re.IGNORECASE
    • “l”: re.LOCALE
    • “m”: re.MULTILINE
    • “s”: re.DOTALL
    • “u”: re.UNICODE
    • “x”: re.VERBOSE
Raises InvalidValueError:
 

Regexp pattern is not found in the value.

__init__(regexp, is_match=True, flags=None)

Constractor.

Raises TypeError:
 regexp is not string.
class validators.AllowType(test_type, on_exception=None)

Is TYPE allowed the value?

Parameters:
  • test_typeCallable. If raise exception when call this, value is evaluated as “invalid”.
  • on_exception

    Exception callback function. When call occurred exception by test_type.

    Callback function takes one argument, it is the exception object.

Raises:
  • InvalidTypeError – Type test_type can’t accept the value.
  • InvalidValueError – Not available on_exception callback and exception is occurred from test_type.
__init__(test_type, on_exception=None)

Constractor.

Raises ValueError:
 test_type is not callable.
class validators.Prefix(prefix)

Prefix validator.

Parameters:prefix – If value that starts from prefix, evaluate the value as “valid”.
Raises InvalidValueError:
 The value is not prefixed.
class validators.Type(value_type)

Type of the value validator.

Parameters:value_type – Expected type of the value.
Raises InvalidTypeError:
 Type of the value is not same as value_type.
class validators.Length(min=0, max=None)

Limitation of length.

Parameters:
  • min – Min length.
  • max – Max length.
Raises InvalidValueError:
 

Value has exceeded the limit of the length.

class validators.Split(*validators, **options)

Split value validator.

usage:

>>> console = Any(Equal('001'), Equal('101'))
>>> model_number = Split(Equal('HVC'), console, sep='-')
>>> model_number('HVC-001')
>>> model_number('HVC-002')
validators.InvalidValueError: 002 is not equal to 001
Parameters:
  • *validators

    Validator for splitted values.

    Number of validators have to same as number of part of splited values.

  • sep

    Value separator character.

    Value will be split by this separator string.

  • rmatch

    Right match flag.

    If this flag is True, to apply validator from right side. Otherwise, to apply validator from left side.

    False by default.

Raises InvalidValueError:
 

The value can’t decode to unicode or number of splitted values and number of validators does not match.

class validators.OnelinerText(ban_phrases=None, ignore_chars=None)

Free text without linefeed code.

Arguments are the same as FreeText.

Raises:
  • InvalidTypeError – The type of given value is not string.
  • InvalidValueError – Ban phrase is found in the value.
class validators.String

String type only.

Raises InvalidTypeError:
 The type of value is not string.
class validators.Int

Int type only.

Raises InvalidTypeError:
 The type of value is not int.
class validators.SortOrder

Sort order validator.

Acceptable value:
asc
ascending order.
desc
descending order.
Raises InvalidValueError:
 The value is not equal to asc or desc.
class validators.Flag

Flag validator.

“true” and “t” and “1” as True.

“false” and “f” and “0” as False.

Note

The value ignore case.

Raises InvalidValueError:
 Not allowed value was given.

Previous topic

Fields

Next topic

Converters

This Page