Skip to content

Commit

Permalink
Added single Validated import (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranger-ross authored Jul 13, 2024
1 parent 1d13716 commit fcdb2eb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@ Any type that implments the Actix [`FromRequest`](https://docs.rs/actix-web/late
actix-web-validation = { version = "0.0.0", features = ["validator"]}
# or
actix-web-validation = { version = "0.0.0", features = ["garde"]}
# or
actix-web-validation = { version = "0.0.0", features = ["custom"]}
```

```rust,ignore
use actix_web_validation::validator::Validated;
use validator::Validate;
// or for garde
// use actix_web_validation::garde::Validated;
// use garde::Validate;
use actix_web_validation::Validated;
// Do validation using your validation library
#[derive(Debug, Validate, Deserialize)]
Expand Down Expand Up @@ -61,8 +58,6 @@ struct CustomErrorResponse {
errors: Vec<String>,
}
// impl Display for CustomErrorResponse { ... }
impl ResponseError for CustomErrorResponse {
fn status_code(&self) -> actix_web::http::StatusCode {
actix_web::http::StatusCode::BAD_REQUEST
Expand Down Expand Up @@ -133,3 +128,8 @@ The actix-web-validator is great but there are a few pain points I would like to
- More explict validation by using the `Validated` extractor to reduce the risk of using the wrong `Json`/`Query`/ect 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 implmentations, the `actix_web_validation::Validated` can only be used when 1 feature flag is enabled. This probalby 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 willl need to fully qualify the import like `actix_web_validation::validator::Validated`, `actix_web_validation::garde::Validated`, and `actix_web_validation::custom::Validated`.

9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@ pub mod custom;
pub mod garde;
#[cfg(feature = "validator")]
pub mod validator;

#[cfg(all(feature = "validator", not(feature = "garde"), not(feature = "custom")))]
pub use crate::validator::Validated;

#[cfg(all(feature = "garde", not(feature = "validator"), not(feature = "custom")))]
pub use crate::garde::Validated;

#[cfg(all(feature = "custom", not(feature = "validator"), not(feature = "garde")))]
pub use crate::custom::Validated;

0 comments on commit fcdb2eb

Please sign in to comment.