FluentVal
A fluent validation library for Rust with a builder pattern API. FluentVal provides an intuitive, chainable API for validating data structures with comprehensive error reporting.
Features
- 🎯 Fluent Builder API - Chain validation rules in a readable, expressive way
- 📝 Comprehensive Rules - Built-in validators for strings, numbers, emails, and more
- 🔧 Custom Rules - Define your own validation logic
- 📊 Rich Error Reporting - Detailed validation errors grouped by property
- 🚀 Type-Safe - Leverages Rust's type system for compile-time safety
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Quick Start
Basic String Validation
use *;
let rule_fn = for_property
.not_empty
.min_length
.max_length
.build;
let errors = rule_fn;
// Returns validation errors if validation fails
Validating Complex Objects
use *;
let validator = new
.rule_for
.rule_for
.rule_for
.build;
let user = User ;
let result = validate;
if result.is_valid else
Validating with Custom Error Messages
You can specify custom error messages for each rule to provide more meaningful feedback:
use *;
let validator = new
.rule_for
.rule_for
.rule_for
.rule_for
.build;
let invalid_user = User ;
let result = validate;
if !result.is_valid
Available Rules
String Rules
not_empty()- Validates that a string is not empty or whitespacemin_length(min)- Validates minimum string lengthmax_length(max)- Validates maximum string lengthlength(min, max)- Validates string length rangeemail()- Validates email format
Numeric Rules
greater_than(min)- Value must be greater than minimumgreater_than_or_equal(min)- Value must be greater than or equal to minimumless_than(max)- Value must be less than maximumless_than_or_equal(max)- Value must be less than or equal to maximuminclusive_between(min, max)- Value must be within range (inclusive)
Option Rules
not_null()- Validates that an Option is Some
Custom Rules
rule(predicate)- Add a custom validation rulemust(predicate, message)- Validate with a custom predicate
Advanced Usage
Cross-Property Validation
Validate a property based on other properties in the same struct. The must() method in ValidatorBuilder allows you to access both the entire object and the property value:
use *;
// Helper function to validate phone number based on country
// Helper function to validate tax number based on country
let validator = new
// Validate phone number based on country
.must
// Validate that alt phone is different from primary phone
.must
// Validate tax number based on country
.must
.build;
// Example: Invalid phone number for US
let invalid_command = Command ;
let result = validate;
if !result.is_valid
// Example: Alt phone same as primary
let invalid_command2 = Command ;
let result = validate;
if !result.is_valid
You can also validate a property without using the object context (ignore the object parameter with _):
// Simulate allowed countries
let validator = new
// Validate country without needing the object context
.must
.build;
Custom Error Messages
All rules accept optional custom error messages:
for_property
.email
.min_length
Working with Validation Results
let result = validate;
// Check if valid
if result.is_valid
// Get all errors
for error in result.errors
// Get errors grouped by property
let errors_by_prop = result.errors_by_property;
// Get first error for a specific property
if let Some = result.first_error_for
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.