pub trait FormRequest:
Sized
+ DeserializeOwned
+ Validate
+ Send {
// Provided methods
fn authorize(_req: &Request) -> bool { ... }
fn extract<'async_trait>(
req: Request,
) -> Pin<Box<dyn Future<Output = Result<Self, FrameworkError>> + Send + 'async_trait>>
where Self: 'async_trait { ... }
}Expand description
Trait for validated form/JSON request data
Implement this trait on request structs to enable automatic:
- Body parsing (JSON or form-urlencoded based on Content-Type)
- Validation using the
validatorcrate - Authorization checks
§Example
ⓘ
use ferro_rs::FormRequest;
use serde::Deserialize;
use validator::Validate;
#[derive(FormRequest)] // Auto-derives Deserialize, Validate, and FormRequest impl
pub struct CreateUserRequest {
#[validate(email)]
pub email: String,
#[validate(length(min = 8))]
pub password: String,
}
// In controller:
#[handler]
pub async fn store(form: CreateUserRequest) -> Response {
// `form` is already validated - returns 422 if invalid
json_response!({ "email": form.email })
}§Authorization
Override authorize() to add authorization logic:
ⓘ
impl FormRequest for CreateUserRequest {
fn authorize(_req: &Request) -> bool {
// Check if user is authenticated
true
}
}Provided Methods§
Check if the request is authorized
Override this method to add authorization logic.
Returns true by default (all requests authorized).
Returning false will result in a 403 Forbidden response.
Sourcefn extract<'async_trait>(
req: Request,
) -> Pin<Box<dyn Future<Output = Result<Self, FrameworkError>> + Send + 'async_trait>>where
Self: 'async_trait,
fn extract<'async_trait>(
req: Request,
) -> Pin<Box<dyn Future<Output = Result<Self, FrameworkError>> + Send + 'async_trait>>where
Self: 'async_trait,
Extract and validate data from the request
This method:
- Checks authorization
- Parses the request body (JSON or form based on Content-Type)
- Validates the parsed data
Returns Err(FrameworkError) on authorization failure, parse error,
or validation failure.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.