13 releases
0.1.12 | Sep 12, 2024 |
---|---|
0.1.11 | Sep 12, 2024 |
#157 in #error-message
36 downloads per month
7KB
Rust Validation Library
Overview
The Rust Validation Library provides a set of powerful validation macros and functions to enforce constraints and ensure data integrity in Rust structs. This library allows you to define validation rules directly on your struct fields using attributes, making it easier to validate data and ensure that it meets your criteria before processing.
Features
- Flexible Validation Rules: Define rules for required fields, email validation, length constraints, numeric ranges, regex patterns, and more.
- Customizable Error Messages: Specify custom error messages for each validation rule or use default messages.
- Integration with
proc-macro
: Leverage Rust's procedural macros to automatically generate validation code based on your annotations.
Getting Started
Installation
Add the following to your Cargo.toml
:
[dependencies]
json_validator = "0.1"
Usage
To use the validation library, derive the Validate
trait on your struct and annotate fields with the #[validate]
attribute to specify validation rules.
Here's an example:
use json_validator::{errors::ValidationError, Validate};
#[derive(Validate)]
struct User {
#[validate(non_empty, message = "Name cannot be empty")]
name: String,
#[validate(email)]
email: String,
#[validate(min_length = 8)]
password: String,
#[validate(range = "18, 100", message = "Age must be between 18 and 100")]
age: i32,
}
Validation Functions
The library provides several built-in validation functions:
is_non_empty
: Ensures that the field is not empty.validate_email
: Validates that the field contains a valid email address.validate_min_length
: Checks that the field's length is at least a specified minimum.validate_max_length
: Ensures that the field's length does not exceed a specified maximum.validate_length_range
: Validates that the field's length falls within a specified range.validate_min_value
: Ensures that the field's value is at least a specified minimum.validate_max_value
: Checks that the field's value does not exceed a specified maximum.validate_range
: Validates that the field's value falls within a specified range.validate_regex
: Ensures that the field matches a specified regex pattern.only_numbers
: Validates that the field contains only numeric characters.only_letters
: Ensures that the field contains only alphabetic characters and spaces.
Custom Error Messages
You can provide custom error messages for each validation rule by specifying the message
attribute in the #[validate]
annotation. If no custom message is provided, a default message will be used.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please open an issue or submit a pull request if you have suggestions or improvements.
Acknowledgements
- Rust community for their support and contributions to the Rust ecosystem.
- The procedural macro and quote crates for their powerful macro capabilities.
Dependencies
~2.5–4MB
~76K SLoC