Skip to main content

Python library to validate usernames suitable for use in public facing applications.

Project description

python-usernames

Build Status Coverage Status PyPI version

Python library to validate usernames suitable for use in public facing applications where use can choose login names and sub-domains.

Features

  • Provides a default regex validator
  • Validates against list of banned words that should not be used as username.
  • Python 3.8+

Installation

pip install python-usernames

Usages

from python_usernames import is_safe_username

>>> is_safe_username("jerk")
False  # contains one of the banned words

>>> is_safe_username("handsome!")
False  # contains non-url friendly `!`

is_safe_username takes the following optional arguments:

  • whitelist: a case insensitive list of words that should be considered as always safe. Default: []

  • blacklist: a case insensitive list of words that should be considered as unsafe. Default: []

  • max_length: specify the maximun character a username can have. Default: None

  • regex: regular expression string that must pass before the banned : words is checked.

The default regular expression is as follows:

^                       # beginning of string
(?!_$)                  # no only _
(?![-.])                # no - or . at the beginning
(?!.*[_.-]{2})          # no __ or _. or ._ or .. or -- inside
[a-zA-Z0-9_.-]+         # allowed characters, atleast one must be present
(?<![.-])               # no - or . at the end
$                       # end of string

Further Reading

Gotchas

Words like bigcock12 will validated just fine, only equality against the banned word lists is checked. We don't try to be smart to avoid Scunthorpe problem. If you can come up with a algorithm/solution, please create an issue/pr :).

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_usernames-1.0.0.tar.gz (5.0 kB view hashes)

Uploaded Source

Built Distribution

python_usernames-1.0.0-py3-none-any.whl (5.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page