/// This crate provides a procedural macro for validating fields in Rust structs. /// The `Validate` derive macro allows you to annotate fields in a struct with validation attributes. /// It supports a variety of validation rules, such as non-empty, email format, minimum and maximum length, /// and custom regex patterns, among others. /// /// # Example /// /// ``` /// #[derive(Validate)] /// struct User { /// #[validate(non_empty, message = "Name cannot be empty")] /// name: String, /// /// #[validate(email, message = "Invalid email address")] /// email: String, /// /// #[validate(range = "(18, 100)", message = "Age must be between 18 and 100")] /// age: i32, /// } /// /// Validation errors are returned as a `Vec`. Each `ValidationError` contains details about the field and the error message. /// /// Errors occur if: /// - A required field is empty. /// - An email field does not contain a valid email address. /// - Numeric fields are out of the specified range. /// - String fields do not match the given length constraints or regex patterns. /// ``` use json_validator::{errors::ValidationError, Validate}; #[derive(Validate)] struct TestStruct { #[validate(non_empty, message = "El campo nombre no puede estar vacío")] #[validate(valid_name, message = "El campo nombre solo puede contener letras")] name: String, #[validate(non_empty)] #[validate(email, message = "El campo email es inválido")] email: String, #[validate(only_numbers, message = "El campo celular solo puede contener números")] phone: String, #[validate(range = "18, 100", message = "La edad debe estar entre 18 y 100 años")] age: i32, } #[test] fn test_valid_instance() { let valid_instance = TestStruct { name: "Ian Eduardo".to_string(), email: "test@example.com".to_string(), phone: "123456780".to_string(), age: 17, }; let errors = valid_instance.validate(); // Imprimir los errores (si los hubiera) if !errors.is_empty() { for error in &errors { println!("{}", error.error); } } assert!(errors.is_empty()); }