Crates.io | json_validator |
lib.rs | json_validator |
version | 0.1.12 |
source | src |
created_at | 2024-09-11 22:25:06.681656 |
updated_at | 2024-09-12 00:07:31.651837 |
description | A library for JSON validation with custom error messages and validation rules. |
homepage | https://github.com/Lann892 |
repository | https://github.com/Lann892/json_validator |
max_upload_size | |
id | 1372314 |
size | 8,244 |
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.
proc-macro
: Leverage Rust's procedural macros to automatically generate validation code based on your annotations.Add the following to your Cargo.toml
:
[dependencies]
json_validator = "0.1"
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,
}
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.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.
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request if you have suggestions or improvements.