Validators

0.35.0 · active · verified Sun Mar 29

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.

Warnings

Install

Imports

Quickstart

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}")

view raw JSON →