Skip to main content

validated

Function validated 

Source
pub async fn validated(
    request: Request,
    ctx: &Context,
    validator: &DocumentValidator,
) -> Result<Value, Errors>
Available on crate features http and validation only.
Expand description

Read a body and check it against a resource’s request schema.

The JSON:API half of FromRequest: the body is parsed as JSON and validated against the DocumentValidator registered for resource, so every structural failure — a missing member, a wrong type, an attribute of the wrong type, malformed linkage — comes back as a complete error document with a JSON Pointer at each cause.

What is left for the caller is the part a schema cannot express: mapping the validated document onto its own type.

ⓘ
impl FromRequest<App> for NewTodo {
    async fn from_request(req: Request, app: &App, ctx: &Context) -> Result<Self, Errors> {
        let doc = validated(req, ctx, app.validator("todo")).await?;
        Ok(NewTodo { message: doc["data"]["attributes"]["message"].as_str().unwrap().into() })
    }
}