PyValidX
Custom field validation for Python with Pydantic
PyValidX is a powerful and flexible validation library built on top of Pydantic that provides a rich set of validators for common use cases while allowing you to create custom validation logic with ease.
✨ Features
- 🎯 Easy to Use: Simple validation with clear, readable syntax
- 🔧 Flexible: Support for custom validators and conditional validation
- 📝 Type Safe: Built on Pydantic with full type annotation support
- 🌍 Comprehensive: Wide range of built-in validators for strings, numbers, dates, and more
- 🚀 Performance: Efficient validation with minimal overhead
- 📖 Well Documented: Comprehensive documentation with examples
🚀 Quick Example
from pyvalidx import ValidatedModel, field_validated
from pyvalidx.core import is_required
from pyvalidx.string import is_email, is_strong_password
from pyvalidx.numeric import min_value
class User(ValidatedModel):
name: str = field_validated(is_required())
email: str = field_validated(is_required(), is_email())
password: str = field_validated(is_required(), is_strong_password())
age: int = field_validated(is_required(), min_value(18))
# This will validate automatically
try:
user = User(
name="John Doe",
email="john@example.com",
password="SecurePass123!",
age=25
)
print("User created successfully!")
except ValidationException as e:
print(f"Validation failed: {e.to_dict()}")
📦 Installation
Install PyValidX using pip:
pip install pyvalidx
Or with poetry:
poetry add pyvalidx
🔌 FastAPI Integration
PyValidX works seamlessly with FastAPI for robust API validation:
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from pyvalidx import ValidatedModel, ValidationException, field_validated
from pyvalidx.core import is_required
from pyvalidx.string import is_email, is_strong_password
app = FastAPI()
# Configure global exception handler
@app.exception_handler(ValidationException)
async def validation_exception_handler(request, exc: ValidationException):
return JSONResponse(
status_code=400,
content={"error": "Validation failed", "details": exc.to_dict()}
)
# Define your DTO
class CreateUserDto(ValidatedModel):
username: str = field_validated(is_required())
email: str = field_validated(is_required(), is_email())
password: str = field_validated(is_required(), is_strong_password())
# Use it in your endpoint
@app.post("/users")
async def create_user(payload: CreateUserDto):
# Validation happens automatically!
return {"message": "User created", "username": payload.username}
Error Response Example:
{
"error": "Validation failed",
"details": {
"email": ["Invalid email format"],
"password": ["Password must be strong"]
}
}
🎯 Core Concepts
Validators
Validators are functions that check if a value meets certain criteria. They return True if valid, False otherwise.
ValidatedModel
A Pydantic model that automatically runs custom validators on initialization and provides error handling.
field_validated
A field decorator that attaches validators to model fields.
ValidationException
A custom exception that provides structured error information when validation fails.
📚 Available Validators
Core Validators
is_required()- Ensures field is not None, empty string, or empty listmin_length()- Minimum string/list lengthmax_length()- Maximum string/list lengthsame_as()- Field must match another fieldrequired_if()- Conditional requirement based on another field
String Validators
is_email()- Valid email formatis_strong_password()- Strong password requirementsmatches_regex()- Custom regex pattern matchingno_whitespace()- No spaces allowedis_phone()- Colombian phone number format
Numeric Validators
is_positive()- Positive numbers onlyis_integer()- Integer type validationis_float()- Float type validationmin_value()- Minimum numeric valuemax_value()- Maximum numeric value
Date Validators
is_date()- Valid date formatis_future_date()- Date must be in the futureis_past_date()- Date must be in the pastis_today()- Date must be today
Type Validators
is_dict()- Dictionary type validationis_list()- List type validationis_boolean()- Boolean type validationis_in()- Value must be in specified choices
🔗 Quick Links
- Getting Started - Install and set up PyValidX
- Validators - Explore all available validators
- API Reference - Complete API documentation
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
👤 Author
Harrison Alonso Arroyave Gaviria
- GitHub: @harrison-gaviria
- Email: harrisonarroyaveg@gmail.com
Metadata
Release files for pyvalidx 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pyvalidx-0.1.4.tar.gz | 11.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyvalidx-0.1.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.0 kB
Release files / pyvalidx-0.1.4.tar.gz
| Download URL | pyvalidx-0.1.4.tar.gz |
|---|---|
| Size | 11.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
38b753970e1acbc099ebf2178f64c003a132b2890ab8d127cae38ed5d75b4e07
|
|
BLAKE2b-256 checksum How to use checksums |
fd14ebcfd8eb41f247bda48ba36bb05544bc5caa123278d89fb64cb53d59dee4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.9
|
Release files / pyvalidx-0.1.4-py3-none-any.whl
| Download URL | pyvalidx-0.1.4-py3-none-any.whl |
|---|---|
| Size | 11.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
03c09eb59d47cd18cbc6adda326927cb6c555bf1fc51d5af14cd70efab5ec36f
|
|
BLAKE2b-256 checksum How to use checksums |
0018655994c35e5f64b1280535db0ec34c2f305348ef97a68cd3c100429ea1d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.9
|