Skip to main content

Module lambda

Module lambda 

Source
Expand description

§vld_web::lambda — AWS Lambda integration for vld

Validate API Gateway / ALB / Function URL requests of lambda_http 1.x, and raw lambda_runtime events, with vld schemas (lambda_http with its default features: every event source; lambda_runtime is used through its lambda_http::lambda_runtime re-export).

FunctionSource
validate_jsonJSON body (requires a JSON Content-Type)
validate_queryqueryStringParameters (RequestExt::query_string_parameters)
validate_pathpathParameters (RequestExt::path_parameters)
error_responsethe 422 / 415 response for a VldHttpError
validate_event / event_errora raw LambdaEvent<serde_json::Value> payload; failures are a Diagnostic

Requests answer with the same bodies and parse parameters by the same rules as every HTTP module (error bodies).

§Example

use lambda_http::{Body, Error, Request, Response};
use vld_web::lambda::{validate_json, validate_path};

vld::schema! {
    #[derive(Debug)]
    pub struct UserPath {
        pub id: i64 => vld::number().int().min(1),
    }
}

vld::schema! {
    #[derive(Debug)]
    pub struct UpdateUser {
        pub name: String => vld::string().min(2),
    }
}

async fn handler(req: Request) -> Result<Response<Body>, Error> {
    let path: UserPath = match validate_path(&req) {
        Ok(p) => p,
        Err(resp) => return Ok(resp), // 422
    };
    let body: UpdateUser = match validate_json(&req) {
        Ok(b) => b,
        Err(resp) => return Ok(resp), // 422 / 415
    };
    Ok(Response::new(format!("{}: {}", path.id, body.name).into()))
}
// lambda_http::run(lambda_http::service_fn(handler)).await

Guide: https://s00d.github.io/vld/integrations/web/lambda

Modules§

prelude
Prelude — import everything you need.

Functions§

error_response
The JSON error response for error: its status (422 / 415 / 413) and body. For a plain VldError use error_response(&err.into()) (422).
event_error
A Diagnostic for a failed invocation: errorType "ValidationError", errorMessage the HTTP 422 body as JSON text.
validate_event
Validate the payload of a raw lambda_runtime event (SQS, EventBridge, a direct invocation, …) and hand back the payload and its context.
validate_json
Validate the JSON body. A missing or non-JSON Content-Type is 415, malformed JSON and schema errors are 422.
validate_path
Validate the path parameters API Gateway matched (RequestExt::path_parameters, already decoded); values are coerced as for every HTTP module’s path extractor.
validate_query
Validate the query parameters the event carries (RequestExt::query_string_parameters). Repeated keys become arrays and values are coerced as for every HTTP module’s query extractor.