Validators Submodule

class rfc3986.validators.Validator

Object used to configure validation of all objects in rfc3986.

Example usage:

>>> from rfc3986 import api, validators
>>> uri = api.uri_reference('https://github.com/')
>>> validator = validators.Validator().require_presence_of(
...    'scheme', 'host', 'path',
... ).allow_schemes(
...    'http', 'https',
... ).allow_hosts(
...    '127.0.0.1', 'github.com',
... )
>>> validator.validate(uri)
>>> invalid_uri = rfc3986.uri_reference('imap://mail.google.com')
>>> validator.validate(invalid_uri)
Traceback (most recent call last):
...
rfc3986.exceptions.MissingComponentError: ('path was required but
missing', URIReference(scheme=u'imap', authority=u'mail.google.com',
path=None, query=None, fragment=None), ['path'])
Validator.allow_schemes(*schemes)

Require the scheme to be one of the provided schemes.

Parameters:schemes – Schemes, without :// that are allowed.
Returns:The validator instance.
Return type:Validator
Validator.allow_hosts(*hosts)

Require the host to be one of the provided hosts.

Parameters:hosts – Hosts that are allowed.
Returns:The validator instance.
Return type:Validator
Validator.allow_ports(*ports)

Require the port to be one of the provided ports.

Parameters:ports – Ports that are allowed.
Returns:The validator instance.
Return type:Validator
Validator.allow_use_of_password()

Allow passwords to be present in the URI.

Validator.forbid_use_of_password()

Prevent passwords from being included in the URI.

Validator.require_presence_of(*components)

Require the components provided.

Parameters:components – Names of components from Validator.COMPONENT_NAMES.
Returns:The validator instance.
Return type:Validator
Validator.validate(uri)

Check a URI for conditions specified on this validator.

Parameters:

uri (rfc3986.uri.URIReference) – Parsed URI to validate.

Raises:
  • MissingComponentError – When a required component is missing.
  • UnpermittedComponentError – When a component is not one of those allowed.
  • PasswordForbidden – When a password is present in the userinfo component but is not permitted by configuration.