validators

repository·master·Indexed 22 days ago

https://github.com/python-validators/validators

A lightweight Python library for data validation that allows checking simple values—such as emails, URLs, domains, and cryptocurrency addresses—without the overhead of defining schemas or forms. It provides specialized modules for validating credit card numbers, ISO country and currency codes, financial identifiers (CUSIP, ISIN, SEDOL), cryptographic hashes (MD5, SHA), and various encoding formats (Base16, Base32, Base58, Base64).

Tokens
13.6K
Snippets
89
Records
95
Agent score
77%

What's inside validators

  1. Raise validation errors instead of returning False

    master

    By default, validator functions return False if validation fails. To change this behavior so that a validators.utils.ValidationError is raised instead, you can use one of two methods:

    Method 1: Environment Variable

    Set the RAISE_VALIDATION_ERROR environment variable to True in your shell. This affects all validator calls in the process.

    Method 2: Function Argument

    Pass the keyword argument r_ve=True to the specific validator function call.

    # Method 1: Using environment variable
    $ export RAISE_VALIDATION_ERROR=True
    $ python -c "from validators import url; print(url('https//bad_url'))"
    
    # Method 2: Using r_ve=True argument
    $ python -c "from validators.card import visa; print(visa('bad_visa_number', r_ve=True))"
  2. Validate simple values with validators

    master

    To use the library, import validators and call the specific validation function for the data type you want to check. The functions return a boolean value (True if valid, False otherwise).

    import validators
    
    # Example: Validating an email address
    result = validators.email('someone@example.com')
    print(result)  # Output: True
  3. Raise validation errors instead of returning booleans

    master

    By default, validators return a boolean. To change this behavior so that a validators.utils.ValidationError is raised when validation fails, you can use one of two methods:

    1. Use an environment variable

    Set the RAISE_VALIDATION_ERROR environment variable to True in your shell session.

    2. Use the r_ve parameter

    Pass r_ve=True as a keyword argument to the specific validation function call.

    # Method 1: Environment Variable
    $ export RAISE_VALIDATION_ERROR=True
    $ python -c "from validators import url; print(url('https//bad_url'))"
    
    # Method 2: Function Parameter
    $ python -c "from validators.card import visa; print(visa('bad_visa_number', r_ve=True))"
  4. Validate hash algorithms with validators.hashes

    master

    The validators.hashes module provides functions to verify if a given string is a valid hexadecimal representation of various cryptographic hash algorithms. Use these functions to ensure that hash strings (like those found in checksum files) are correctly formatted before processing them.

    Supported algorithms include:

    • md5
    • sha1
    • sha224
    • sha256
    • sha384
    • sha512
    from validators.hashes import sha256
    
    # Returns True if the string is a valid SHA-256 hex string
    is_valid = sha256("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
    print(is_valid)  # True
    
    # Returns False for invalid strings
    print(sha256("not-a-hash"))  # False
  5. Validate email addresses with validators.email.email

    master

    Use the validators.email.email function to check if a string is a valid email address. This function performs validation based on standard email formatting rules.

    from validators.email import email
    
    # Example usage
    result = email("user@example.com")
    print(result)  # Returns True or False
  6. Validate IPv6 addresses

    master

    Use validators.ip_address.ipv6 to check if a given string is a valid IPv6 address. This function returns True if the string is a valid IPv6 address and False otherwise.

    import validators
    
    # Returns True
    validators.ip_address.ipv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334')
    
    # Returns False
    validators.ip_address.ipv6('not-an-ipv6-address')
  7. Validate email addresses with email()

    master

    Use the email function from the validators.email module to check if a string is a valid email address. It returns True if the string is a valid email, and False otherwise.

    import validators
    
    # Returns True
    validators.email("test@example.com")
    
    # Returns False
    validators.email("invalid-email")