validator-collection

1.5.0 · active · verified Mon Apr 13

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

Install

Imports

Quickstart

This quickstart demonstrates the two primary ways to use `validator-collection`: 'validator' functions which return the validated data or raise an error, and 'checker' functions which return a boolean indicating validity.

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

view raw JSON →