Validators

raw JSON →
0.35.0 verified Tue May 12 auth: no python install: verified

Validators is a lightweight Python library designed for human-friendly data validation without the need for complex schemas or forms. It provides a wide array of simple functions to validate common data types such as email addresses, URLs, IP addresses, and more. On success, validator functions return `True`; on failure, they return a `ValidationFailure` object. The library is actively maintained, with frequent updates to add new validators and improve existing ones.

pip install validators
error ModuleNotFoundError: No module named 'validators'
cause The 'validators' library has not been installed in the current Python environment.
fix
pip install validators
error AttributeError: module 'validators' has no attribute 'mail'
cause An attempt was made to call a validator function using an incorrect or non-existent name.
fix
Use the correct function name provided by the library, for example, validators.email() for email validation.
error TypeError: email() missing 1 required positional argument: 'email_address'
cause A validator function was called without providing the required string argument to be validated.
fix
Pass the string value you intend to validate as an argument to the function, e.g., validators.email('test@example.com').
error AttributeError: 'bool' object has no attribute 'reason'
cause The `validators` functions return `True` on successful validation. If validation succeeds, the result is a boolean `True`, not a `ValidationFailure` object, so attempting to access attributes like `reason` (which exist only on `ValidationFailure` objects) will raise an `AttributeError`.
fix
Always check if the result is False (or specifically an instance of ValidationFailure) before attempting to access its attributes:
import validators
result = validators.email('test@example.com')
if not result: # This implicitly checks for ValidationFailure
    print(f"Validation failed: {result.reason}")
else:
    print("Validation succeeded!")
breaking Version 0.35.0 dropped support for Python 3.8. Users on Python 3.8 or older must upgrade their Python version or use an older `validators` release.
fix Upgrade Python to 3.9+ or pin `validators` to `<0.35.0`.
breaking The `btc_address` validator was moved to the `crypto_addresses` submodule. Direct imports or calls to `validators.btc_address` will fail.
fix Update your code to use `validators.crypto_addresses.btc_address`.
gotcha Validator functions return `True` on success or a `ValidationFailure` object on failure. While `ValidationFailure` objects correctly evaluate to `False` in a boolean context, they are not `False` themselves. Access specific error details via attributes like `failure_object.message`, `failure_object.value`, etc.
fix Check the result in a boolean context (`if result:`) or inspect the `ValidationFailure` object's attributes for details (`if not result: print(result.message)`).
gotcha Be mindful of similarly named but distinct libraries like `validator` (for request validation with schema rules) and `validator-collection` (another collection of validation functions with a different API). Ensure you are installing and importing the correct `validators` library.
fix Double-check `pip install validators` and `import validators` to confirm you are using this specific library.
python os / libc status wheel install import disk mem side effects
3.10 alpine (musl) wheel - 0.06s 18.1M 2.8M clean
3.10 alpine (musl) - - 0.06s 18.1M 2.8M -
3.10 slim (glibc) wheel 1.5s 0.04s 19M 2.8M clean
3.10 slim (glibc) - - 0.04s 19M 2.8M -
3.11 alpine (musl) wheel - 0.09s 20.0M 2.7M clean
3.11 alpine (musl) - - 0.11s 20.0M 2.7M -
3.11 slim (glibc) wheel 1.6s 0.08s 20M 2.7M clean
3.11 slim (glibc) - - 0.08s 20M 2.7M -
3.12 alpine (musl) wheel - 0.08s 11.9M 2.8M clean
3.12 alpine (musl) - - 0.08s 11.9M 2.8M -
3.12 slim (glibc) wheel 1.5s 0.08s 12M 2.8M clean
3.12 slim (glibc) - - 0.08s 12M 2.8M -
3.13 alpine (musl) wheel - 0.07s 11.6M 3.0M clean
3.13 alpine (musl) - - 0.08s 11.5M 3.0M -
3.13 slim (glibc) wheel 1.5s 0.07s 12M 2.8M clean
3.13 slim (glibc) - - 0.08s 12M 2.8M -
3.9 alpine (musl) wheel - 0.06s 17.6M 2.7M clean
3.9 alpine (musl) - - 0.06s 17.6M 2.7M -
3.9 slim (glibc) wheel 1.7s 0.05s 18M 2.7M clean
3.9 slim (glibc) - - 0.05s 18M 2.7M -

Demonstrates basic usage of `validators.email` and `validators.url`, and how to handle `ValidationFailure` objects.

import validators

email_to_check = "test@example.com"
is_valid_email = validators.email(email_to_check)

if is_valid_email:
    print(f"'{email_to_check}' is a valid email address.")
else:
    # ValidationFailure objects evaluate to False in a boolean context
    print(f"'{email_to_check}' is not a valid email address. Reason: {is_valid_email.message}")

url_to_check = "http://invalid.domain"
is_valid_url = validators.url(url_to_check)

if is_valid_url:
    print(f"'{url_to_check}' is a valid URL.")
else:
    print(f"'{url_to_check}' is an invalid URL. Reason: {is_valid_url.message}")