Python Data Validation for Humans™ - comprehensive validation library for various data types without schema definitions
Validators for various encoding formats and cryptographic hash functions. These validators ensure data integrity and proper encoding format compliance for base encodings and cryptographic hashes.
Validates strings encoded in various base formats commonly used in data transmission and storage.
def base16(value: str, /) -> Union[Literal[True], ValidationError]:
"""
Validate base16 (hexadecimal) encoded strings.
Parameters:
- value: String to validate as base16
Returns:
True if valid base16, ValidationError otherwise
Validates hexadecimal strings containing only:
- Digits: 0-9
- Letters: A-F or a-f
- Even number of characters (each byte = 2 hex chars)
"""
def base32(value: str, /) -> Union[Literal[True], ValidationError]:
"""
Validate base32 encoded strings per RFC 4648.
Parameters:
- value: String to validate as base32
Returns:
True if valid base32, ValidationError otherwise
Validates base32 strings containing:
- Letters: A-Z
- Digits: 2-7
- Padding: = characters at end if needed
- Proper length alignment (groups of 8 characters)
"""
def base58(value: str, /) -> Union[Literal[True], ValidationError]:
"""
Validate base58 encoded strings.
Parameters:
- value: String to validate as base58
Returns:
True if valid base58, ValidationError otherwise
Validates base58 strings using Bitcoin alphabet:
- Excludes confusing characters: 0, O, I, l
- Uses: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
- Commonly used in cryptocurrency addresses
"""
def base64(value: str, /) -> Union[Literal[True], ValidationError]:
"""
Validate base64 encoded strings per RFC 4648.
Parameters:
- value: String to validate as base64
Returns:
True if valid base64, ValidationError otherwise
Validates base64 strings containing:
- Letters: A-Z, a-z
- Digits: 0-9
- Symbols: +, /
- Padding: = characters at end if needed
- Proper length alignment (groups of 4 characters)
"""Validates cryptographic hash strings for integrity verification and security applications.
def md5(value: str, /) -> Union[Literal[True], ValidationError]:
"""
Validate MD5 hash strings.
Parameters:
- value: String to validate as MD5 hash
Returns:
True if valid MD5 hash, ValidationError otherwise
Validates MD5 hashes:
- Length: exactly 32 hexadecimal characters
- Characters: 0-9, a-f (case insensitive)
- Format: 128-bit hash as 32-character hex string
Note: MD5 is cryptographically broken, use for legacy compatibility only.
"""
def sha1(value: str, /) -> Union[Literal[True], ValidationError]:
"""
Validate SHA-1 hash strings.
Parameters:
- value: String to validate as SHA-1 hash
Returns:
True if valid SHA-1 hash, ValidationError otherwise
Validates SHA-1 hashes:
- Length: exactly 40 hexadecimal characters
- Characters: 0-9, a-f (case insensitive)
- Format: 160-bit hash as 40-character hex string
Note: SHA-1 is deprecated for cryptographic use.
"""
def sha224(value: str, /) -> Union[Literal[True], ValidationError]:
"""
Validate SHA-224 hash strings.
Parameters:
- value: String to validate as SHA-224 hash
Returns:
True if valid SHA-224 hash, ValidationError otherwise
Validates SHA-224 hashes:
- Length: exactly 56 hexadecimal characters
- Characters: 0-9, a-f (case insensitive)
- Format: 224-bit hash as 56-character hex string
"""
def sha256(value: str, /) -> Union[Literal[True], ValidationError]:
"""
Validate SHA-256 hash strings.
Parameters:
- value: String to validate as SHA-256 hash
Returns:
True if valid SHA-256 hash, ValidationError otherwise
Validates SHA-256 hashes:
- Length: exactly 64 hexadecimal characters
- Characters: 0-9, a-f (case insensitive)
- Format: 256-bit hash as 64-character hex string
Most commonly used secure hash algorithm.
"""
def sha384(value: str, /) -> Union[Literal[True], ValidationError]:
"""
Validate SHA-384 hash strings.
Parameters:
- value: String to validate as SHA-384 hash
Returns:
True if valid SHA-384 hash, ValidationError otherwise
Validates SHA-384 hashes:
- Length: exactly 96 hexadecimal characters
- Characters: 0-9, a-f (case insensitive)
- Format: 384-bit hash as 96-character hex string
"""
def sha512(value: str, /) -> Union[Literal[True], ValidationError]:
"""
Validate SHA-512 hash strings.
Parameters:
- value: String to validate as SHA-512 hash
Returns:
True if valid SHA-512 hash, ValidationError otherwise
Validates SHA-512 hashes:
- Length: exactly 128 hexadecimal characters
- Characters: 0-9, a-f (case insensitive)
- Format: 512-bit hash as 128-character hex string
Highest security level in SHA-2 family.
"""import validators
# Base encoding validation
validators.base16('48656c6c6f20576f726c64') # True (hex)
validators.base16('Hello World') # ValidationError (not hex)
validators.base32('JBSWY3DPEBLW64TMMQQQ====') # True
validators.base32('HelloWorld123') # ValidationError (invalid chars)
validators.base58('3mJr7YhWNM') # True
validators.base58('3mJr7YhWNM0O') # ValidationError (contains 0, O)
validators.base64('SGVsbG8gV29ybGQ=') # True
validators.base64('Hello World!') # ValidationError (invalid chars)
# Hash validation
validators.md5('5d41402abc4b2a76b9719d911017c592') # True (32 chars)
validators.md5('hello') # ValidationError (too short)
validators.sha1('aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d') # True (40 chars)
validators.sha256('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855') # True (64 chars)
# Case insensitive validation
validators.md5('5D41402ABC4B2A76B9719D911017C592') # True (uppercase)
validators.sha256('E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855') # True
# Invalid hash examples
validators.md5('5d41402abc4b2a76b9719d911017c59') # ValidationError (31 chars, too short)
validators.sha256('hello') # ValidationError (too short)
validators.sha512('not-a-hash') # ValidationError (invalid format)import validators
import hashlib
def verify_file_hash(file_content: bytes, expected_hash: str, algorithm: str = 'sha256'):
"""Verify file integrity using hash validation."""
# First validate the hash format
if algorithm == 'md5' and not validators.md5(expected_hash):
raise ValueError("Invalid MD5 hash format")
elif algorithm == 'sha256' and not validators.sha256(expected_hash):
raise ValueError("Invalid SHA-256 hash format")
# Calculate actual hash
hasher = hashlib.new(algorithm)
hasher.update(file_content)
actual_hash = hasher.hexdigest()
return actual_hash.lower() == expected_hash.lower()
# Usage
file_data = b"Hello, World!"
expected = "dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f"
if verify_file_hash(file_data, expected):
print("File integrity verified")import validators
import base64
def validate_and_decode_base64(encoded_data: str) -> bytes:
"""Validate base64 format and decode if valid."""
if not validators.base64(encoded_data):
raise ValueError("Invalid base64 format")
try:
return base64.b64decode(encoded_data)
except Exception as e:
raise ValueError(f"Base64 decode failed: {e}")
# Usage
encoded = "SGVsbG8gV29ybGQ="
if validators.base64(encoded):
decoded = validate_and_decode_base64(encoded)
print(f"Decoded: {decoded.decode()}")Install with Tessl CLI
npx tessl i tessl/pypi-validators