Actix Web Validation
Request validation for actix-web.
Supported Validation Libraries
Usage
Any type that implements the Actix FromRequest
trait can be automatically validated.
# Cargo.toml
= { = "0.0.0", = ["validator"] }
# or
= { = "0.0.0", = ["garde"] }
# or
= { = "0.0.0", = ["custom"] }
use Validated;
// Do validation using your validation library
// Wrap your Actix extractor with `Validated` to automatically run validation
async : )
Custom Errors
Custom error responses can achieved by providing an error handler.
Below is an example custom error response that responds with JSON
Below is an example for the validator
crate
async
Below is an example for the garde
crate
async
Motivations
This library is heavily inspired by Spring Validation and actix-web-validator.
The actix-web-validator is great but there are a few pain points I would like to address with this library.
- More explicit validation by using the
Validated
extractor to reduce the risk of using the wrongJson
/Query
/etc extractor by mistake. - Provide a common interface for validation libraries that can be extended as the Rust ecosystem evolves.
Limitations
Due to how Rust handles overlapping trait implementations, the actix_web_validation::Validated
can only be used when 1 feature flag is enabled. This probably won't impact most use cases because most applications will just use 1 validation library for everything. If you need to use multiple validation libraries at the same time, this library can still be used but, you will need to fully qualify the import like actix_web_validation::validator::Validated
, actix_web_validation::garde::Validated
, and actix_web_validation::custom::Validated
.