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).
| Function | Source |
|---|---|
validate_json | JSON body (requires a JSON Content-Type) |
validate_query | queryStringParameters (RequestExt::query_string_parameters) |
validate_path | pathParameters (RequestExt::path_parameters) |
error_response | the 422 / 415 response for a VldHttpError |
validate_event / event_error | a 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)).awaitModules§
- prelude
- Prelude — import everything you need.
Functions§
- error_
response - The JSON error response for
error: its status (422 / 415 / 413) and body. For a plainVldErroruseerror_response(&err.into())(422). - event_
error - A
Diagnosticfor a failed invocation:errorType"ValidationError",errorMessagethe HTTP 422 body as JSON text. - validate_
event - Validate the payload of a raw
lambda_runtimeevent (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-Typeis 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.