Validators Submodule

class rfc3986.validators.Validator

Object used to configure validation of all objects in rfc3986.

New in version 1.0.

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.

New in version 1.0.

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.

New in version 1.0.

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.

New in version 1.0.

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.

New in version 1.0.

Returns:The validator instance.
Return type:Validator
Validator.check_validity_of(*components)

Check the validity of the components provided.

This can be specified repeatedly.

New in version 1.1.

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

Prevent passwords from being included in the URI.

New in version 1.0.

Returns:The validator instance.
Return type:Validator
Validator.require_presence_of(*components)

Require the components provided.

This can be specified repeatedly.

New in version 1.0.

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.

New in version 1.0.

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.
  • InvalidComponentsError – When a component was found to be invalid.