Expand description
§RustAPI Validation
Validation system for RustAPI framework. Provides declarative validation
on structs using the #[derive(Validate)] macro.
§Example
use rustapi_validate::prelude::*;
#[derive(Validate)]
struct CreateUser {
#[validate(email)]
email: String,
#[validate(length(min = 3, max = 50))]
username: String,
#[validate(range(min = 18, max = 120))]
age: u8,
}§Validation Rules
email- Validates email formatlength(min = X, max = Y)- String length validationrange(min = X, max = Y)- Numeric range validationregex = "..."- Regex pattern validationnon_empty- Non-empty string/collection validationnested- Validates nested structs
§Error Format
Validation errors return a 422 Unprocessable Entity with JSON:
{
"error": {
"type": "validation_error",
"message": "Validation failed",
"fields": [
{"field": "email", "code": "email", "message": "Invalid email format"},
{"field": "age", "code": "range", "message": "Value must be between 18 and 120"}
]
}
}Modules§
- prelude
- Prelude module for validation
Structs§
- Field
Error - A single field validation error.
- Validation
Error - Validation error containing all field errors.
Traits§
- Validate
- Trait for validatable types.
- Validator
Validate - This is the original trait that was implemented by deriving
Validate. It will still be implemented for struct validations that don’t take custom arguments. The call is being forwarded to theValidateArgs<'v_a>trait.