Validators¶
Validation classes for various types of data.
Classes:
Compose multiple validators and combine their error messages. |
|
Validator which fails if |
|
Validator which succeeds if |
|
Validate an email address. |
|
Validator which succeeds if the |
|
Validator which succeeds if the value passed to it has a length between a minimum and maximum. |
|
Validator which fails if |
|
Validator which succeeds if |
|
Call the specified |
|
Validator which succeeds if the value passed to it is within the specified range. |
|
Validator which succeeds if the |
|
Validate a URL. |
|
Abstract base class for validators. |
- class marshmallow.validate.And(*validators)[source]¶
Compose multiple validators and combine their error messages.
Example:
from marshmallow import validate, ValidationError def is_even(value): if value % 2 != 0: raise ValidationError("Not an even value.") validator = validate.And(validate.Range(min=0), is_even) validator(-1) # ValidationError: ['Must be greater than or equal to 0.', 'Not an even value.']
- Parameters:
validators (types.Validator) – Validators to combine.
- class marshmallow.validate.ContainsNoneOf(iterable, *, error=None)[source]¶
Validator which fails if
value
is a sequence and any element in the sequence is a member of the sequence passed asiterable
. Empty input is considered valid.Added in version 3.6.0.
- class marshmallow.validate.ContainsOnly(choices, labels=None, *, error=None)[source]¶
Validator which succeeds if
value
is a sequence and each element in the sequence is also in the sequence passed aschoices
. Empty input is considered valid.- Parameters:
Changed in version 3.0.0b2: Duplicate values are considered valid.
Changed in version 3.0.0b2: Empty input is considered valid. Use
validate.Length(min=1)
to validate against empty inputs.
- class marshmallow.validate.Email(*, error=None)[source]¶
Validate an email address.
- Parameters:
error (str | None) – Error message to raise in case of a validation error. Can be interpolated with
{input}
.
- class marshmallow.validate.Equal(comparable, *, error=None)[source]¶
Validator which succeeds if the
value
passed to it is equal tocomparable
.- Parameters:
comparable – The object to compare to.
error (str | None) – Error message to raise in case of a validation error. Can be interpolated with
{input}
and{other}
.
- class marshmallow.validate.Length(min=None, max=None, *, equal=None, error=None)[source]¶
Validator which succeeds if the value passed to it has a length between a minimum and maximum. Uses len(), so it can work for strings, lists, or anything with length.
- Parameters:
min (int | None) – The minimum length. If not provided, minimum length will not be checked.
max (int | None) – The maximum length. If not provided, maximum length will not be checked.
equal (int | None) – The exact length. If provided, maximum and minimum length will not be checked.
error (str | None) – Error message to raise in case of a validation error. Can be interpolated with
{input}
,{min}
and{max}
.
- class marshmallow.validate.NoneOf(iterable, *, error=None)[source]¶
Validator which fails if
value
is a member ofiterable
.
- class marshmallow.validate.OneOf(choices, labels=None, *, error=None)[source]¶
Validator which succeeds if
value
is a member ofchoices
.- Parameters:
- options(valuegetter=<class 'str'>)[source]¶
Return a generator over the (value, label) pairs, where value is a string associated with each choice. This convenience method is useful to populate, for instance, a form select field.
- Parameters:
valuegetter (str | Callable[[Any], Any]) – Can be a callable or a string. In the former case, it must be a one-argument callable which returns the value of a choice. In the latter case, the string specifies the name of an attribute of the choice objects. Defaults to
str()
orstr()
.- Return type:
- class marshmallow.validate.Predicate(method, *, error=None, **kwargs)[source]¶
Call the specified
method
of thevalue
object. The validator succeeds if the invoked method returns an object that evaluates to True in a Boolean context. Any additional keyword argument will be passed to the method.
- class marshmallow.validate.Range(min=None, max=None, *, min_inclusive=True, max_inclusive=True, error=None)[source]¶
Validator which succeeds if the value passed to it is within the specified range. If
min
is not specified, or is specified asNone
, no lower bound exists. Ifmax
is not specified, or is specified asNone
, no upper bound exists. The inclusivity of the bounds (if they exist) is configurable. Ifmin_inclusive
is not specified, or is specified asTrue
, then themin
bound is included in the range. Ifmax_inclusive
is not specified, or is specified asTrue
, then themax
bound is included in the range.- Parameters:
min – The minimum value (lower bound). If not provided, minimum value will not be checked.
max – The maximum value (upper bound). If not provided, maximum value will not be checked.
min_inclusive (bool) – Whether the
min
bound is included in the range.max_inclusive (bool) – Whether the
max
bound is included in the range.error (str | None) – Error message to raise in case of a validation error. Can be interpolated with
{input}
,{min}
and{max}
.
- class marshmallow.validate.Regexp(regex, flags=0, *, error=None)[source]¶
Validator which succeeds if the
value
matchesregex
.Note
Uses
re.match
, which searches for a match at the beginning of a string.- Parameters:
regex (str | bytes | Pattern) – The regular expression string to use. Can also be a compiled regular expression pattern.
flags (int) – The regexp flags to use, for example re.IGNORECASE. Ignored if
regex
is not a string.error (str | None) – Error message to raise in case of a validation error. Can be interpolated with
{input}
and{regex}
.
- class marshmallow.validate.URL(*, relative=False, absolute=True, schemes=None, require_tld=True, error=None)[source]¶
Validate a URL.
- Parameters:
relative (bool) – Whether to allow relative URLs.
absolute (bool) – Whether to allow absolute URLs.
error (str | None) – Error message to raise in case of a validation error. Can be interpolated with
{input}
.schemes (types.StrSequenceOrSet | None) – Valid schemes. By default,
http
,https
,ftp
, andftps
are allowed.require_tld (bool) – Whether to reject non-FQDN hostnames.