Skip to main content

FormRequest

Trait FormRequest 

Source
pub trait FormRequest:
    DeserializeOwned
    + Send
    + Sync
    + 'static {
    // Required method
    fn rules() -> RuleSet;

    // Provided methods
    fn authorize<'life0, 'async_trait>(
        request: &'life0 Request,
    ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
       where 'life0: 'async_trait { ... }
    fn messages() -> Vec<(&'static str, &'static str)> { ... }
    fn validation_data(request: &Request) -> Value { ... }
    fn from_input(input: Value) -> Result<Self> { ... }
    fn validate_request<'life0, 'async_trait>(
        request: &'life0 Request,
    ) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn validator() -> Validator { ... }
    fn check(request: &Request) -> Result<(), ValidationErrors> { ... }
}
Expand description

A typed, authorised, validated request payload.

Required Methods§

Source

fn rules() -> RuleSet

The validation rules, by field.

Provided Methods§

Source

fn authorize<'life0, 'async_trait>( request: &'life0 Request, ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
where 'life0: 'async_trait,

Whether the caller may make this request at all.

Runs before validation, so an unauthorised caller cannot use validation messages to probe what the endpoint expects. Returning false produces a 403.

async because a real authorisation check reads a policy, a role, or the record being modified.

Source

fn messages() -> Vec<(&'static str, &'static str)>

Messages overriding the defaults, keyed "field.rule".

Source

fn validation_data(request: &Request) -> Value

What to validate. Defaults to the merged query string and body.

Override to fold in route parameters — an update contract that needs {post} to check uniqueness-except-self, for instance.

Source

fn from_input(input: Value) -> Result<Self>

Build the payload from the validated input.

The default deserialises with string coercion, so a urlencoded form fills a u32 field. Override for a payload that is not a plain deserialisation of the input.

Source

fn validate_request<'life0, 'async_trait>( request: &'life0 Request, ) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Run authorisation and validation, and build the payload.

The whole contract in one call, so it can be used outside an extractor — from a console command, or a test.

Source

fn validator() -> Validator

The configured Validator for this contract.

Source

fn check(request: &Request) -> Result<(), ValidationErrors>

Validate without building the payload, returning the failures.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§