Validation Library
A flexible, extensible validation framework for Rust with support for both synchronous and asynchronous validation, including MongoDB integration for unique checks.
Features
- 🛠️ 40+ built-in validators for common validation scenarios
- ⚡ Async support for database-backed validation
- 🏗️ Composable rules with fluent builder pattern
- 📝 Nested field validation using dot notation
- 🧩 Custom validators for specialized requirements
- 🗃️ MongoDB integration for unique field validation
- 🏷️ Default values for missing fields
- 🚦 Flexible error handling with detailed error messages
Installation
Add to your Cargo.toml:
[]
= "0.1"
Quick Start
use ;
use json;
Core Concepts
1. Basic Validation
use Rules;
use Rule;
// Single field validation
let is_adult = new.add.add;
assert!;
// or use macro
let is_adult = rules!;
assert!;
2. Combining Rules
let password_validator = new.add
.add
.add;
3. Form Validation
let form_validator = new
.add
.add
.add;
4. Async Validation (MongoDB)
async
Available Validators
Basic Validators
required()- Field must be present and not nullstring()- Must be a stringinteger()- Must be an integerfloat()- Must be a floatboolean()- Must be a booleanarray()- Must be an arrayobject()- Must be an object
String Validators
length(n)- Exact lengthmin_length(n)- Minimum lengthmax_length(n)- Maximum lengthemail()- Valid email formaturl()- Valid URL formatip()- Valid IP addressregex()- Matches regex patternaccepted()- Common "accepted" terms (true, 1, "yes", "on")
Numeric Validators
min_value(n)- Minimum valuemax_value(n)- Maximum valueequal(n)- Exact value matchnumeric()- Can be parsed as number
Collection Validators
in_values()- Value must be in allowed setnot_in_values()- Value must not be in excluded set
Database Validators
unique()- Field value must be unique in MongoDB collection
File Validators
extensions()- File extension must be in allowed set
Advanced Usage
Custom Validators
let validator = custom;
Error Handling
match validator.validate
Performance
The library is designed for efficiency:
- Minimal allocations
- Lazy validation (stops on first error when configured)
- Thread-safe by design
Contributing
Contributions are welcome! Please open an issue or submit a PR for:
- New features
- Performance improvements
- Bug fixes
License
Dual-licensed under MIT or Apache 2.0 at your option.