Skip to content

Commit

Permalink
Add info to the README about requiring components
Browse files Browse the repository at this point in the history
Add kwargs to is_valid_uri
  • Loading branch information
Ian Cordasco committed Jun 30, 2014
1 parent d5df671 commit dd371ba
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
33 changes: 33 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,39 @@ You can also very simply validate a URI::

assert is_valid_uri('hTTp://exAMPLe.COM/Some/reallY/biZZare/pAth')

Requiring Components
~~~~~~~~~~~~~~~~~~~~

You can validate that a particular string is a valid URI and require
independent components::

from rfc3986 import is_valid_uri

assert is_valid_uri('http://localhost:8774/v2/resource',
require_scheme=True,
require_authority=True,
require_path=True)

# Assert that a mailto URI is invalid if you require an authority
# component
assert is_valid_uri('mailto:user@example.com', require_authority=True) is False

If you have an instance of a ``URIReference``, you can pass the same arguments
to ``URIReference#is_valid``, e.g.,

.. code::
from rfc3986 import uri_reference
http = uri_reference('http://localhost:8774/v2/resource')
assert uri.is_valid(require_scheme=True,
require_authority=True,
require_path=True)
# Assert that a mailto URI is invalid if you require an authority
# component
mailto = uri_reference('mailto:user@example.com')
assert uri.is_valid(require_authority=True) is False
Alternatives
------------
Expand Down
14 changes: 12 additions & 2 deletions rfc3986/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def uri_reference(uri, encoding='utf-8'):
return URIReference.from_string(uri, encoding)


def is_valid_uri(uri, encoding='utf-8'):
def is_valid_uri(uri, encoding='utf-8', **kwargs):
"""Determine if the URI given is valid.
This is a convenience function. You could use either
Expand All @@ -46,10 +46,20 @@ def is_valid_uri(uri, encoding='utf-8'):
:param str uri: The URI to be validated.
:param str encoding: The encoding of the string provided
:param bool require_scheme: Set to ``True`` if you wish to require the
presence of the scheme component.
:param bool require_authority: Set to ``True`` if you wish to require the
presence of the authority component.
:param bool require_path: Set to ``True`` if you wish to require the
presence of the path component.
:param bool require_query: Set to ``True`` if you wish to require the
presence of the query component.
:param bool require_fragment: Set to ``True`` if you wish to require the
presence of the fragment component.
:returns: ``True`` if the URI is valid, ``False`` otherwise.
:rtype: bool
"""
return URIReference.from_string(uri, encoding).is_valid()
return URIReference.from_string(uri, encoding).is_valid(**kwargs)


def normalize_uri(uri, encoding='utf-8'):
Expand Down

0 comments on commit dd371ba

Please sign in to comment.