Expand description
§RustAPI Validation
Validation system for RustAPI framework. Provides declarative validation
on structs using the #[derive(Validate)] macro.
§Example
ⓘ
use rustapi_validate::prelude::*;
use validator::Validate;
#[derive(Validate)]
struct CreateUser {
#[validate(email)]
email: String,
#[validate(length(min = 3, max = 50))]
username: String,
#[validate(range(min = 18, max = 120))]
age: u8,
}§V2 Validation Engine
The v2 module provides a custom validation engine with async support:
ⓘ
use rustapi_validate::v2::prelude::*;
#[derive(Validate)]
struct CreateUser {
#[validate(email, message = "Invalid email format")]
email: String,
#[validate(length(min = 3, max = 50))]
username: String,
#[validate(async_unique(table = "users", column = "email"))]
unique_email: String,
}§Validation Rules
email- Validates email formatlength(min = X, max = Y)- String length validationrange(min = X, max = Y)- Numeric range validationregex = "..."- Regex pattern validationurl- URL format validationrequired- Non-empty string/option validationasync_unique(table, column)- Database uniqueness checkasync_exists(table, column)- Database existence checkasync_api(endpoint)- External API validation
§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§
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.
Derive Macros§
- Derive
Validate - Derive macro for implementing Validate and AsyncValidate traits
- Validator
Validate