Validators

Validation classes for various types of data.

Classes:

And(*validators[, error])

Compose multiple validators and combine their error messages.

ContainsNoneOf(iterable, *[, error])

Validator which fails if value is a sequence and any element in the sequence is a member of the sequence passed as iterable.

ContainsOnly(choices[, labels, error])

Validator which succeeds if value is a sequence and each element in the sequence is also in the sequence passed as choices.

Email(*[, error])

Validate an email address.

Equal(comparable, *[, error])

Validator which succeeds if the value passed to it is equal to comparable.

Length([min, max, equal, error])

Validator which succeeds if the value passed to it has a length between a minimum and maximum.

NoneOf(iterable, *[, error])

Validator which fails if value is a member of iterable.

OneOf(choices[, labels, error])

Validator which succeeds if value is a member of choices.

Predicate(method, *[, error])

Call the specified method of the value object.

Range([min, max, min_inclusive, ...])

Validator which succeeds if the value passed to it is within the specified range.

Regexp(regex[, flags, error])

Validator which succeeds if the value matches regex.

URL(*[, relative, absolute, schemes, ...])

Validate a URL.

Validator()

Abstract base class for validators.

class marshmallow.validate.And(*validators: Callable[[Any], Any], error: str | None = None)[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 – Validators to combine.

  • error – Error message to use when a validator returns False.

class marshmallow.validate.ContainsNoneOf(iterable: Iterable, *, error: str | None = None)[source]

Validator which fails if value is a sequence and any element in the sequence is a member of the sequence passed as iterable. Empty input is considered valid.

Parameters:

New in version 3.6.0.

class marshmallow.validate.ContainsOnly(choices: Iterable, labels: Iterable[str] | None = None, *, error: str | None = None)[source]

Validator which succeeds if value is a sequence and each element in the sequence is also in the sequence passed as choices. Empty input is considered valid.

Parameters:
  • choices (iterable) – Same as OneOf.

  • labels (iterable) – Same as OneOf.

  • error (str) – Same as OneOf.

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: str | None = None)[source]

Validate an email address.

Parameters:

error – Error message to raise in case of a validation error. Can be interpolated with {input}.

class marshmallow.validate.Equal(comparable, *, error: str | None = None)[source]

Validator which succeeds if the value passed to it is equal to comparable.

Parameters:
  • comparable – The object to compare to.

  • error – Error message to raise in case of a validation error. Can be interpolated with {input} and {other}.

class marshmallow.validate.Length(min: int | None = None, max: int | None = None, *, equal: int | None = None, error: str | None = 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 – The minimum length. If not provided, minimum length will not be checked.

  • max – The maximum length. If not provided, maximum length will not be checked.

  • equal – The exact length. If provided, maximum and minimum length will not be checked.

  • error – Error message to raise in case of a validation error. Can be interpolated with {input}, {min} and {max}.

class marshmallow.validate.NoneOf(iterable: Iterable, *, error: str | None = None)[source]

Validator which fails if value is a member of iterable.

Parameters:
  • iterable – A sequence of invalid values.

  • error – Error message to raise in case of a validation error. Can be interpolated using {input} and {values}.

class marshmallow.validate.OneOf(choices: Iterable, labels: Iterable[str] | None = None, *, error: str | None = None)[source]

Validator which succeeds if value is a member of choices.

Parameters:
  • choices – A sequence of valid values.

  • labels – Optional sequence of labels to pair with the choices.

  • error – Error message to raise in case of a validation error. Can be interpolated with {input}, {choices} and {labels}.

Methods:

options([valuegetter])

Return a generator over the (value, label) pairs, where value is a string associated with each choice.

options(valuegetter: str | ~typing.Callable[[~typing.Any], ~typing.Any] = <class 'str'>) Iterable[tuple[Any, 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 – 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() or str().

class marshmallow.validate.Predicate(method: str, *, error: str | None = None, **kwargs)[source]

Call the specified method of the value 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.

Parameters:
  • method – The name of the method to invoke.

  • error – Error message to raise in case of a validation error. Can be interpolated with {input} and {method}.

  • kwargs – Additional keyword arguments to pass to the method.

class marshmallow.validate.Range(min=None, max=None, *, min_inclusive: bool = True, max_inclusive: bool = True, error: str | None = None)[source]

Validator which succeeds if the value passed to it is within the specified range. If min is not specified, or is specified as None, no lower bound exists. If max is not specified, or is specified as None, no upper bound exists. The inclusivity of the bounds (if they exist) is configurable. If min_inclusive is not specified, or is specified as True, then the min bound is included in the range. If max_inclusive is not specified, or is specified as True, then the max 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 – Whether the min bound is included in the range.

  • max_inclusive – Whether the max bound is included in the range.

  • error – Error message to raise in case of a validation error. Can be interpolated with {input}, {min} and {max}.

class marshmallow.validate.Regexp(regex: str | bytes | Pattern, flags: int = 0, *, error: str | None = None)[source]

Validator which succeeds if the value matches regex.

Note

Uses re.match, which searches for a match at the beginning of a string.

Parameters:
  • regex – The regular expression string to use. Can also be a compiled regular expression pattern.

  • flags – The regexp flags to use, for example re.IGNORECASE. Ignored if regex is not a string.

  • error – Error message to raise in case of a validation error. Can be interpolated with {input} and {regex}.

class marshmallow.validate.URL(*, relative: bool = False, absolute: bool = True, schemes: Sequence[str] | AbstractSet[str] | None = None, require_tld: bool = True, error: str | None = None)[source]

Validate a URL.

Parameters:
  • relative – Whether to allow relative URLs.

  • absolute – Whether to allow absolute URLs.

  • error – Error message to raise in case of a validation error. Can be interpolated with {input}.

  • schemes – Valid schemes. By default, http, https, ftp, and ftps are allowed.

  • require_tld – Whether to reject non-FQDN hostnames.

class marshmallow.validate.Validator[source]

Abstract base class for validators.

Note

This class does not provide any validation behavior. It is only used to add a useful __repr__ implementation for validators.