Rod Validation
A powerful and flexible compile-time validation library for Rust structs and enums. Rod provides declarative validation through derive macros, allowing you to specify validation rules directly in your type definitions.
Features
- Compile-time validation: Validation rules are checked at compile time for type safety
- Declarative syntax: Define validation rules using intuitive attribute macros
- Comprehensive type support: Validate strings, integers, floats, options, tuples, and custom types
- Flexible error handling: Choose between fail-fast or collect-all error reporting
- Custom validation: Add custom validation logic with closures
- Regex support: Built-in regex validation for strings (with optional
regexfeature) - Nested validation: Support for complex nested data structures
Quick Start
Add Rod Validation to your Cargo.toml:
[]
= "0.2.0"
For regex support:
[]
= { = "0.2.0", = ["regex"] }
Basic Usage
use *;
Validation Types
String Validation
Available string formats (with regex feature):
Email- Email address validationUrl- URL validationUuid- UUID validationIpv4- IPv4 address validationIpv6- IPv6 address validationDateTime- DateTime validationRegex("pattern")- Custom regex pattern
Integer Validation
Number signs:
Positive- Greater than 0Negative- Less than 0NonPositive- Less than or equal to 0NonNegative- Greater than or equal to 0
Float Validation
Float types:
Finite- Not NaN or infiniteInfinite- Must be infiniteNan- Must be NaNNormal- Must be normalSubnormal- Must be subnormal
Option Validation
Tuple Validation
Literal Validation
Custom Validation
Iterable Validation
Error Handling
Rod provides two validation methods:
validate()- Returns the first validation error encountered (fail-fast)validate_all()- Collects and returns all validation errors
// Fail-fast validation
match user.validate
// Collect all errors
match user.validate_all
Nested Structures
Rod supports validation of nested structures that implement RodValidate:
Enums
Rod supports validation of enum variants:
The RodValidate Derive Macro
The #[derive(RodValidate)] macro generates two validation methods for your types:
validate(&self) -> Result<(), RodValidateError>- Fail-fast validation (returns on first error)validate_all(&self) -> Result<(), RodValidateErrorList>- Collect all errors before returning
Error Messages
The generated validation code produces detailed error messages with field paths:
// Example error: "Expected `user.email` to be a string with length 3..=50, got 2"
Compilation Guarantees
The macro provides several compile-time guarantees:
- Type Safety: Validation attributes must match the field type
- Nested Types: Custom types must implement
RodValidate - Attribute Validation: Invalid attribute combinations are caught at compile time
Technical Details
Code Generation Strategy
The macro generates validation code that:
- Traverses struct/enum fields recursively
- Applies validation rules in the order they appear in attributes
- Handles early return (fail-fast) or error collection (validate-all) modes
- Provides detailed error context with complete field paths
Performance Considerations
- All validation logic is generated at compile time
- No runtime reflection or dynamic dispatch overhead
- Validation performance is equivalent to hand-written code
- Zero-cost abstractions for unused validation paths
Limitations
- Union types: Not supported (will generate
todo!()panic) - Double references:
&&Ttypes are not allowed - Custom validation closures: Must return
booltype - Regex features: Require the
regexcrate feature to be enabled
Rust Features
- Default features:
["regex"] regex: Enables regex-based string format validation
Documentation
For detailed documentation and examples, visit docs.rs/rod_validation.
License
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a pull request.