Crate garde_actix_web

source ·
Expand description

Actix-web wrapper for garde, a Rust validation library.

§Installation

[dependencies]
garde = "0.18"
garde-actix-web = "0.7"

§Usage example

Simply use garde-actix-web exposed types as a drop in for actix types.

Your types must implement Validate from garde. Validation happens during actix’s FromRequest invocation.

If payload is invalid, a 400 error is returned (404 for Path).

Custom error handling can be implemented with an extractor config (garde_actix_web::web::QueryConfig in place of actix_web::web::QueryConfig for example).

use actix_web::HttpResponse;
// instead of actix_web::web::Path
use garde_actix_web::web::Path;
use garde::Validate;

#[derive(Validate)]
struct MyStruct<'a> {
  #[garde(ascii, length(min=3, max=25))]
  username: &'a str,
}

fn test(id: Path<MyStruct>) -> HttpResponse {
  todo!()
}

⚠️ When using garde custom validation, the Context type needs to implement Default which is not required by garde.

§Feature flags

namedescriptionextra dependencies
serde_qsEnables the usage of garde for serde_qs::actix::QsQuery<T>serde_qs

§Compatibility matrix

garde versionserde_qs versiongarde-actix-web-version
0.140.120.1.x
0.150.120.2.x
0.160.120.3.x
0.170.120.4.x
0.180.120.5.x, 0.6.x

Modules§

  • Error exposed by garde-actix-web
  • Drop in types for actix web implementing garde