json_validator

Crates.iojson_validator
lib.rsjson_validator
version0.1.12
sourcesrc
created_at2024-09-11 22:25:06.681656
updated_at2024-09-12 00:07:31.651837
descriptionA library for JSON validation with custom error messages and validation rules.
homepagehttps://github.com/Lann892
repositoryhttps://github.com/Lann892/json_validator
max_upload_size
id1372314
size8,244
Ian Vega (Lann892)

documentation

https://docs.rs/json_validator

README

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.
Commit count: 0

cargo fmt