Python Data Validation for Humans™ - comprehensive validation library for various data types without schema definitions
Core validators for fundamental data types including numeric ranges, string lengths, identifiers, and format validation. These validators provide the building blocks for most validation scenarios.
PossibleValueTypes = TypeVar("PossibleValueTypes", int, float, str, datetime, None)
class AbsMin:
"""Represents unlimited minimum value (negative infinity)."""
pass
class AbsMax:
"""Represents unlimited maximum value (positive infinity)."""
passValidates that numeric values fall within specified minimum and maximum bounds.
def between(value: PossibleValueTypes, /, *, min_val: Union[PossibleValueTypes, AbsMin, None] = None, max_val: Union[PossibleValueTypes, AbsMax, None] = None) -> Union[Literal[True], ValidationError]:
"""
Validate that a number is between minimum and/or maximum values.
Parameters:
- value: The numeric value to validate
- min_val: Minimum allowed value (inclusive), or None for no minimum
- max_val: Maximum allowed value (inclusive), or None for no maximum
Returns:
True if value is within bounds, ValidationError otherwise
"""Usage examples:
import validators
# Check if value is within range
validators.between(5, min_val=1, max_val=10) # True
validators.between(15, min_val=1, max_val=10) # ValidationError
# Check minimum only
validators.between(100, min_val=50) # True
# Check maximum only
validators.between(25, max_val=50) # TrueValidates that string lengths fall within specified character count bounds.
def length(value: str, /, *, min_val: Union[int, None] = None, max_val: Union[int, None] = None) -> Union[Literal[True], ValidationError]:
"""
Validate string length within specified range.
Parameters:
- value: The string to validate
- min_val: Minimum allowed length, or None for no minimum
- max_val: Maximum allowed length, or None for no maximum
Returns:
True if string length is within bounds, ValidationError otherwise
"""Usage examples:
import validators
# Password length validation
validators.length("mypassword", min_val=8, max_val=32) # True
validators.length("abc", min_val=8) # ValidationError
# Username length validation
validators.length("user123", min_val=3, max_val=20) # TrueValidates UUID (Universally Unique Identifier) version 4 strings in standard format.
def uuid(value: Union[str, UUID], /) -> Union[Literal[True], ValidationError]:
"""
Validate UUID version 4 strings.
Parameters:
- value: String or UUID object to validate
Returns:
True if value is a valid UUID, ValidationError otherwise
Accepts both string and UUID object inputs.
Validates proper UUID format: 8-4-4-4-12 hexadecimal digits.
"""Usage examples:
import validators
from uuid import UUID
# Valid UUID strings
validators.uuid('550e8400-e29b-41d4-a716-446655440000') # True
validators.uuid('6ba7b810-9dad-11d1-80b4-00c04fd430c8') # True
# UUID object
uuid_obj = UUID('550e8400-e29b-41d4-a716-446655440000')
validators.uuid(uuid_obj) # True
# Invalid formats
validators.uuid('not-a-uuid') # ValidationError
validators.uuid('550e8400-e29b-41d4-a716') # ValidationError (too short)Validates URL-friendly slugs containing only lowercase letters, numbers, and hyphens.
def slug(value: str, /) -> Union[Literal[True], ValidationError]:
"""
Validate URL slug format.
Parameters:
- value: String to validate as slug
Returns:
True if value is a valid slug, ValidationError otherwise
Valid slugs contain only:
- Lowercase letters (a-z)
- Numbers (0-9)
- Hyphens (-)
- Cannot start or end with hyphen
"""Usage examples:
import validators
# Valid slugs
validators.slug('my-blog-post') # True
validators.slug('product-123') # True
validators.slug('hello-world') # True
# Invalid slugs
validators.slug('My-Blog-Post') # ValidationError (uppercase)
validators.slug('hello_world') # ValidationError (underscore)
validators.slug('-start-dash') # ValidationError (starts with dash)
validators.slug('end-dash-') # ValidationError (ends with dash)Install with Tessl CLI
npx tessl i tessl/pypi-validators