validator-collection
validator-collection is a Python library offering a comprehensive suite of over 60 functions for data validation and type checking. It provides both 'validator' functions that return the validated value or raise an error, and 'checker' functions that return a boolean. The current version is 1.5.0, with releases occurring periodically, sometimes with significant gaps between minor versions.
Warnings
- gotcha URL and domain validation logic has undergone frequent fixes and changes across minor versions (e.g., 1.3.3, 1.3.5, 1.4.1, 1.4.2, 1.5.0). This means the validation behavior for specific URLs might change after updating the library, potentially causing previously accepted (or rejected) inputs to now yield different results.
- deprecated The library officially supports Python 2.7, a version that has reached its end-of-life and is no longer maintained. While the library might function, continued use on Python 2.7 is highly discouraged due to security vulnerabilities and lack of future support.
- gotcha The library provides two distinct patterns for validation: `validators.function()` which returns the validated value or raises `ValueError`/`TypeError`, and `checkers.is_function()` which returns a boolean. Mixing these up can lead to unexpected behavior or incorrect error handling in your application logic.
- gotcha The project's release cadence has shown variability, with significant gaps between some minor versions (e.g., between 2020 and 2023 for 1.5.0). While still maintained, this might imply a slower pace for bug fixes or new feature development compared to other libraries.
Install
-
pip install validator-collection
Imports
- email
from validator_collection.validators import email
- url
from validator_collection.validators import url
- is_email
from validator_collection.checkers import is_email
- is_url
from validator_collection.checkers import is_url
- validators
from validator_collection import validators
- checkers
from validator_collection import checkers
Quickstart
from validator_collection.validators import email, url
from validator_collection.checkers import is_email, is_url
# Example 1: Using validators (raise ValueError/TypeError if invalid)
try:
valid_email = email("test@example.com")
print(f"Validated Email: {valid_email}")
valid_url = url("https://www.example.com")
print(f"Validated URL: {valid_url}")
# This will raise a ValueError
# invalid_email = email("invalid-email")
# This will raise a ValueError
# invalid_url = url("not-a-url")
except (ValueError, TypeError) as e:
print(f"Validation Error: {e}")
# Example 2: Using checkers (return True/False)
print(f"Is 'test@example.com' a valid email? {is_email('test@example.com')}")
print(f"Is 'not-an-email' a valid email? {is_email('not-an-email')}")
print(f"Is 'https://example.com' a valid URL? {is_url('https://example.com')}")
print(f"Is 'ftp://another.com' a valid URL? {is_url('ftp://another.com')}") # Supports various protocols