Included Validators

Scalars

Containers

Numbers

Email Addresses

class IsEmail(**kw)

Validates email addresses.

The default behavior takes a very permissive stance on allowed characters in the local-part and a relatively strict stance on the domain. Given local-part@domain:

  • local-part must be present and contain at least one non-whitespace character. Any character is permitted, including international characters.

  • domain must be preset, less than 253 characters and each dot-separated component must be 63 characters or less. domain may contain non-ASCII international characters, and will be converted to IDN representation before length assertions are applied. No top level domain validations are applied.

Attributes

non_local

Default True. When true, require at minimum two domain name components and reject local email addresses such as postmaster@localhost or user@workstation.

local_part_pattern

No default. If present, a compiled regular expression that will be matched to the local-part. Override this to implement more stringent checking such as RFC-compliant addresses.

domain_pattern

Defaults to a basic domain-validating regular expression with no notion of valid top level domains. Override this to require certain TLDs (or alternately and more simply, add another validator to your chain that checks the endings of the string against your list of TLDs.)

The default pattern rejects the valid but obscure quoted IP-address form ([1.2.3.4]).

Messages

invalid

Emitted if the email address is not valid.

Construct a validator.

Parameters:

**kw – override any extant class attribute on this instance.

URLs

class URLValidator(**kw)

A general URL validator.

Validates that a URL is well-formed and may optionally restrict the set of valid schemes and other URL components.

Attributes

allowed_schemes

Restrict URLs to just this sequence of named schemes, or allow all schemes with (‘*’,). Defaults to all schemes. Example:

allowed_schemes = ('http', 'https', 'ssh')
allowed_parts

A sequence of 0 or more part names in urlparse’s vocabulary:

'scheme', 'netloc', 'path', 'params', 'query', 'fragment'

Defaults to all parts allowed.

urlparse

By default the urlparse module, but may be replaced by any object that implements urlparse.urlparse() and urlparse.urlunparse().

Messages

bad_format

Emitted for an unparseable URL.

blocked_scheme

Emitted if the URL scheme: is not present in allowed_schemes.

blocked_part

Emitted if the URL has a component not present in allowed_parts.

Construct a validator.

Parameters:

**kw – override any extant class attribute on this instance.

class HTTPURLValidator(**kw)

Validates http and https URLs.

Validates that an http-like URL is well-formed and may optionally require and restrict the permissible values of its components.

Attributes

all_parts

A sequence of known URL parts. Defaults to the full 10-tuple of names in urlparse’s vocabulary for HTTP-like URLs.

required_parts

A mapping of part names. If value is True, the part is required. The value may also be a sequence of strings; the value of the part must be present in this collection to validate.

The default requires a scheme of 'http' or 'https'.

forbidden_parts

A mapping of part names. If value is True, the part is forbidden and validation fails. The value may also be a sequence of strings; the value of the part must not be present in this collection to validate.

The default forbids username and password parts.

urlparse

By default the urlparse module, but may be replaced by any object that implements urlparse.urlparse() and urlparse.urlunparse().

Messages

bad_format

Emitted for an unparseable URL.

required_part

Emitted if URL is missing a part present in required_parts.

forbidden_part

Emitted if URL contains a part present in forbidden_parts.

Construct a validator.

Parameters:

**kw – override any extant class attribute on this instance.

class URLCanonicalizer(**kw)

A URL canonicalizing validator.

Given a valid URL, re-writes it with unwanted parts removed. The default implementation drops the #fragment from the URL, if present.

Attributes

discard_parts

A sequence of 0 or more part names in urlparse’s vocabulary:

'scheme', 'netloc', 'path', 'params', 'query', 'fragment'
urlparse

By default the urlparse module, but may be replaced by any object that implements urlparse.urlparse() and urlparse.urlunparse().

Messages

bad_format

Emitted for an unparseable URL. This is impossible to hit with the Python’s standard library implementation of urlparse.

Construct a validator.

Parameters:

**kw – override any extant class attribute on this instance.