update README.md and doc tests
This commit is contained in:
490
src/extra.rs
490
src/extra.rs
@@ -26,54 +26,114 @@
|
||||
//! #### Example
|
||||
//!
|
||||
//! ```no_run
|
||||
//! #![cfg(feature = "validator")]
|
||||
//! #[cfg(feature = "validator")]
|
||||
//! mod validator_example {
|
||||
//! use axum::extract::FromRequestParts;
|
||||
//! use axum::http::request::Parts;
|
||||
//! use axum::response::{IntoResponse, Response};
|
||||
//! use axum::routing::post;
|
||||
//! use axum::Router;
|
||||
//! use axum_extra::extract::Cached;
|
||||
//! use axum_valid::Valid;
|
||||
//! use validator::Validate;
|
||||
//!
|
||||
//! use axum::extract::FromRequestParts;
|
||||
//! use axum::http::request::Parts;
|
||||
//! use axum::response::{IntoResponse, Response};
|
||||
//! use axum::routing::post;
|
||||
//! use axum::Router;
|
||||
//! use axum_extra::extract::Cached;
|
||||
//! use axum_valid::Valid;
|
||||
//! use validator::Validate;
|
||||
//! #[tokio::main]
|
||||
//! async fn main() -> anyhow::Result<()> {
|
||||
//! let router = Router::new().route("/cached", post(handler));
|
||||
//! axum::Server::bind(&([0u8, 0, 0, 0], 8080).into())
|
||||
//! .serve(router.into_make_service())
|
||||
//! .await?;
|
||||
//! Ok(())
|
||||
//! }
|
||||
//! async fn handler(Valid(Cached(parameter)): Valid<Cached<Parameter>>) {
|
||||
//! assert!(parameter.validate().is_ok());
|
||||
//! }
|
||||
//! #[derive(Validate, Clone)]
|
||||
//! pub struct Parameter {
|
||||
//! #[validate(range(min = 5, max = 10))]
|
||||
//! pub v0: i32,
|
||||
//! #[validate(length(min = 1, max = 10))]
|
||||
//! pub v1: String,
|
||||
//! }
|
||||
//! pub fn router() -> Router {
|
||||
//! Router::new().route("/cached", post(handler))
|
||||
//! }
|
||||
//!
|
||||
//! pub struct ParameterRejection;
|
||||
//! async fn handler(Valid(Cached(parameter)): Valid<Cached<Parameter>>) {
|
||||
//! assert!(parameter.validate().is_ok());
|
||||
//! }
|
||||
//!
|
||||
//! impl IntoResponse for ParameterRejection {
|
||||
//! fn into_response(self) -> Response {
|
||||
//! todo!()
|
||||
//! #[derive(Validate, Clone)]
|
||||
//! pub struct Parameter {
|
||||
//! #[validate(range(min = 5, max = 10))]
|
||||
//! pub v0: i32,
|
||||
//! #[validate(length(min = 1, max = 10))]
|
||||
//! pub v1: String,
|
||||
//! }
|
||||
//!
|
||||
//! pub struct ParameterRejection;
|
||||
//!
|
||||
//! impl IntoResponse for ParameterRejection {
|
||||
//! fn into_response(self) -> Response {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! #[axum::async_trait]
|
||||
//! impl<S> FromRequestParts<S> for Parameter
|
||||
//! where
|
||||
//! S: Send + Sync,
|
||||
//! {
|
||||
//! type Rejection = ParameterRejection;
|
||||
//!
|
||||
//! async fn from_request_parts(_parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! }
|
||||
//! }
|
||||
//! #[cfg(feature = "garde")]
|
||||
//! mod garde_example {
|
||||
//! use axum::extract::FromRequestParts;
|
||||
//! use axum::http::request::Parts;
|
||||
//! use axum::response::{IntoResponse, Response};
|
||||
//! use axum::routing::post;
|
||||
//! use axum::Router;
|
||||
//! use axum_extra::extract::Cached;
|
||||
//! use axum_valid::Garde;
|
||||
//! use garde::Validate;
|
||||
//!
|
||||
//! pub fn router() -> Router {
|
||||
//! Router::new().route("/cached", post(handler))
|
||||
//! }
|
||||
//!
|
||||
//! async fn handler(Garde(Cached(parameter)): Garde<Cached<Parameter>>) {
|
||||
//! assert!(parameter.validate(&()).is_ok());
|
||||
//! }
|
||||
//!
|
||||
//! #[derive(Validate, Clone)]
|
||||
//! pub struct Parameter {
|
||||
//! #[garde(range(min = 5, max = 10))]
|
||||
//! pub v0: i32,
|
||||
//! #[garde(length(min = 1, max = 10))]
|
||||
//! pub v1: String,
|
||||
//! }
|
||||
//!
|
||||
//! pub struct ParameterRejection;
|
||||
//!
|
||||
//! impl IntoResponse for ParameterRejection {
|
||||
//! fn into_response(self) -> Response {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! #[axum::async_trait]
|
||||
//! impl<S> FromRequestParts<S> for Parameter
|
||||
//! where
|
||||
//! S: Send + Sync,
|
||||
//! {
|
||||
//! type Rejection = ParameterRejection;
|
||||
//!
|
||||
//! async fn from_request_parts(_parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! #[axum::async_trait]
|
||||
//! impl<S> FromRequestParts<S> for Parameter
|
||||
//! where
|
||||
//! S: Send + Sync,
|
||||
//! {
|
||||
//! type Rejection = ParameterRejection;
|
||||
//!
|
||||
//! async fn from_request_parts(_parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! }
|
||||
//! # #[tokio::main]
|
||||
//! # async fn main() -> anyhow::Result<()> {
|
||||
//! # use axum::Router;
|
||||
//! # let router = Router::new();
|
||||
//! # #[cfg(feature = "validator")]
|
||||
//! # let router = router.nest("/validator", validator_example::router());
|
||||
//! # #[cfg(feature = "garde")]
|
||||
//! # let router = router.nest("/garde", garde_example::router());
|
||||
//! # axum::Server::bind(&([0u8, 0, 0, 0], 8080).into())
|
||||
//! # .serve(router.into_make_service())
|
||||
//! # .await?;
|
||||
//! # Ok(())
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ### `Valid<WithRejection<T, R>>`
|
||||
@@ -87,60 +147,125 @@
|
||||
//! #### Example
|
||||
//!
|
||||
//! ```no_run
|
||||
//! #![cfg(feature = "validator")]
|
||||
//! #[cfg(feature = "validator")]
|
||||
//! mod validator_example {
|
||||
//! use axum::extract::FromRequestParts;
|
||||
//! use axum::http::request::Parts;
|
||||
//! use axum::http::StatusCode;
|
||||
//! use axum::response::{IntoResponse, Response};
|
||||
//! use axum::routing::post;
|
||||
//! use axum::Router;
|
||||
//! use axum_extra::extract::WithRejection;
|
||||
//! use axum_valid::Valid;
|
||||
//! use validator::Validate;
|
||||
//!
|
||||
//! use axum::extract::FromRequestParts;
|
||||
//! use axum::http::request::Parts;
|
||||
//! use axum::http::StatusCode;
|
||||
//! use axum::response::{IntoResponse, Response};
|
||||
//! use axum::routing::post;
|
||||
//! use axum::Router;
|
||||
//! use axum_extra::extract::WithRejection;
|
||||
//! use axum_valid::Valid;
|
||||
//! use validator::Validate;
|
||||
//! #[tokio::main]
|
||||
//! async fn main() -> anyhow::Result<()> {
|
||||
//! let router = Router::new().route("/valid_with_rejection", post(handler));
|
||||
//! axum::Server::bind(&([0u8, 0, 0, 0], 8080).into())
|
||||
//! .serve(router.into_make_service())
|
||||
//! .await?;
|
||||
//! Ok(())
|
||||
//! }
|
||||
//! async fn handler(
|
||||
//! Valid(WithRejection(parameter, _)): Valid<
|
||||
//! WithRejection<Parameter, ValidWithRejectionRejection>,
|
||||
//! >,
|
||||
//! ) {
|
||||
//! assert!(parameter.validate().is_ok());
|
||||
//! }
|
||||
//! pub fn router() -> Router {
|
||||
//! Router::new().route("/valid_with_rejection", post(handler))
|
||||
//! }
|
||||
//!
|
||||
//! #[derive(Validate)]
|
||||
//! pub struct Parameter {
|
||||
//! #[validate(range(min = 5, max = 10))]
|
||||
//! pub v0: i32,
|
||||
//! #[validate(length(min = 1, max = 10))]
|
||||
//! pub v1: String,
|
||||
//! }
|
||||
//! async fn handler(
|
||||
//! Valid(WithRejection(parameter, _)): Valid<
|
||||
//! WithRejection<Parameter, ValidWithRejectionRejection>,
|
||||
//! >,
|
||||
//! ) {
|
||||
//! assert!(parameter.validate().is_ok());
|
||||
//! }
|
||||
//!
|
||||
//! pub struct ValidWithRejectionRejection;
|
||||
//! #[derive(Validate)]
|
||||
//! pub struct Parameter {
|
||||
//! #[validate(range(min = 5, max = 10))]
|
||||
//! pub v0: i32,
|
||||
//! #[validate(length(min = 1, max = 10))]
|
||||
//! pub v1: String,
|
||||
//! }
|
||||
//!
|
||||
//! impl IntoResponse for ValidWithRejectionRejection {
|
||||
//! fn into_response(self) -> Response {
|
||||
//! StatusCode::BAD_REQUEST.into_response()
|
||||
//! pub struct ValidWithRejectionRejection;
|
||||
//!
|
||||
//! impl IntoResponse for ValidWithRejectionRejection {
|
||||
//! fn into_response(self) -> Response {
|
||||
//! StatusCode::BAD_REQUEST.into_response()
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! #[axum::async_trait]
|
||||
//! impl<S> FromRequestParts<S> for Parameter
|
||||
//! where
|
||||
//! S: Send + Sync,
|
||||
//! {
|
||||
//! type Rejection = ValidWithRejectionRejection;
|
||||
//!
|
||||
//! async fn from_request_parts(_parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! #[axum::async_trait]
|
||||
//! impl<S> FromRequestParts<S> for Parameter
|
||||
//! where
|
||||
//! S: Send + Sync,
|
||||
//! {
|
||||
//! type Rejection = ValidWithRejectionRejection;
|
||||
//! #[cfg(feature = "garde")]
|
||||
//! mod garde_example {
|
||||
//! use axum::extract::FromRequestParts;
|
||||
//! use axum::http::request::Parts;
|
||||
//! use axum::http::StatusCode;
|
||||
//! use axum::response::{IntoResponse, Response};
|
||||
//! use axum::routing::post;
|
||||
//! use axum::Router;
|
||||
//! use axum_extra::extract::WithRejection;
|
||||
//! use axum_valid::Garde;
|
||||
//! use garde::Validate;
|
||||
//!
|
||||
//! async fn from_request_parts(_parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
//! todo!()
|
||||
//! pub fn router() -> Router {
|
||||
//! Router::new().route("/valid_with_rejection", post(handler))
|
||||
//! }
|
||||
//!
|
||||
//! async fn handler(
|
||||
//! Garde(WithRejection(parameter, _)): Garde<
|
||||
//! WithRejection<Parameter, ValidWithRejectionRejection>,
|
||||
//! >,
|
||||
//! ) {
|
||||
//! assert!(parameter.validate(&()).is_ok());
|
||||
//! }
|
||||
//!
|
||||
//! #[derive(Validate)]
|
||||
//! pub struct Parameter {
|
||||
//! #[garde(range(min = 5, max = 10))]
|
||||
//! pub v0: i32,
|
||||
//! #[garde(length(min = 1, max = 10))]
|
||||
//! pub v1: String,
|
||||
//! }
|
||||
//!
|
||||
//! pub struct ValidWithRejectionRejection;
|
||||
//!
|
||||
//! impl IntoResponse for ValidWithRejectionRejection {
|
||||
//! fn into_response(self) -> Response {
|
||||
//! StatusCode::BAD_REQUEST.into_response()
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! #[axum::async_trait]
|
||||
//! impl<S> FromRequestParts<S> for Parameter
|
||||
//! where
|
||||
//! S: Send + Sync,
|
||||
//! {
|
||||
//! type Rejection = ValidWithRejectionRejection;
|
||||
//!
|
||||
//! async fn from_request_parts(_parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! # #[tokio::main]
|
||||
//! # async fn main() -> anyhow::Result<()> {
|
||||
//! # use axum::Router;
|
||||
//! # let router = Router::new();
|
||||
//! # #[cfg(feature = "validator")]
|
||||
//! # let router = router.nest("/validator", validator_example::router());
|
||||
//! # #[cfg(feature = "garde")]
|
||||
//! # let router = router.nest("/garde", garde_example::router());
|
||||
//! # axum::Server::bind(&([0u8, 0, 0, 0], 8080).into())
|
||||
//! # .serve(router.into_make_service())
|
||||
//! # .await?;
|
||||
//! # Ok(())
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ### `WithRejection<Valid<T>, R>`
|
||||
@@ -155,80 +280,153 @@
|
||||
//! #### Example
|
||||
//!
|
||||
//! ```no_run
|
||||
//! #![cfg(feature = "validator")]
|
||||
//! #[cfg(feature = "validator")]
|
||||
//! mod validator_example {
|
||||
//! use axum::extract::FromRequestParts;
|
||||
//! use axum::http::request::Parts;
|
||||
//! use axum::response::{IntoResponse, Response};
|
||||
//! use axum::routing::post;
|
||||
//! use axum::Router;
|
||||
//! use axum_extra::extract::WithRejection;
|
||||
//! use axum_valid::{HasValidate, Valid, ValidRejection};
|
||||
//! use validator::Validate;
|
||||
//!
|
||||
//! use axum::extract::FromRequestParts;
|
||||
//! use axum::http::request::Parts;
|
||||
//! use axum::response::{IntoResponse, Response};
|
||||
//! use axum::routing::post;
|
||||
//! use axum::Router;
|
||||
//! use axum_extra::extract::WithRejection;
|
||||
//! use axum_valid::{HasValidate, Valid, ValidRejection};
|
||||
//! use validator::Validate;
|
||||
//! #[tokio::main]
|
||||
//! async fn main() -> anyhow::Result<()> {
|
||||
//! let router = Router::new().route("/with_rejection_valid", post(handler));
|
||||
//! axum::Server::bind(&([0u8, 0, 0, 0], 8080).into())
|
||||
//! .serve(router.into_make_service())
|
||||
//! .await?;
|
||||
//! Ok(())
|
||||
//! }
|
||||
//! async fn handler(
|
||||
//! WithRejection(Valid(parameter), _): WithRejection<
|
||||
//! Valid<Parameter>,
|
||||
//! WithRejectionValidRejection,
|
||||
//! >,
|
||||
//! ) {
|
||||
//! assert!(parameter.validate().is_ok());
|
||||
//! }
|
||||
//! pub fn router() -> Router {
|
||||
//! Router::new().route("/with_rejection_valid", post(handler))
|
||||
//! }
|
||||
//!
|
||||
//! #[derive(Validate)]
|
||||
//! pub struct Parameter {
|
||||
//! #[validate(range(min = 5, max = 10))]
|
||||
//! pub v0: i32,
|
||||
//! #[validate(length(min = 1, max = 10))]
|
||||
//! pub v1: String,
|
||||
//! }
|
||||
//! async fn handler(
|
||||
//! WithRejection(Valid(parameter), _): WithRejection<
|
||||
//! Valid<Parameter>,
|
||||
//! WithRejectionValidRejection,
|
||||
//! >,
|
||||
//! ) {
|
||||
//! assert!(parameter.validate().is_ok());
|
||||
//! }
|
||||
//!
|
||||
//! impl HasValidate for Parameter {
|
||||
//! type Validate = Self;
|
||||
//! #[derive(Validate)]
|
||||
//! pub struct Parameter {
|
||||
//! #[validate(range(min = 5, max = 10))]
|
||||
//! pub v0: i32,
|
||||
//! #[validate(length(min = 1, max = 10))]
|
||||
//! pub v1: String,
|
||||
//! }
|
||||
//!
|
||||
//! fn get_validate(&self) -> &Self::Validate {
|
||||
//! self
|
||||
//! impl HasValidate for Parameter {
|
||||
//! type Validate = Self;
|
||||
//!
|
||||
//! fn get_validate(&self) -> &Self::Validate {
|
||||
//! self
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! pub struct ParameterRejection;
|
||||
//!
|
||||
//! impl IntoResponse for ParameterRejection {
|
||||
//! fn into_response(self) -> Response {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! #[axum::async_trait]
|
||||
//! impl<S> FromRequestParts<S> for Parameter
|
||||
//! where
|
||||
//! S: Send + Sync,
|
||||
//! {
|
||||
//! type Rejection = ParameterRejection;
|
||||
//!
|
||||
//! async fn from_request_parts(_parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! pub struct WithRejectionValidRejection;
|
||||
//!
|
||||
//! impl From<ValidRejection<ParameterRejection>> for WithRejectionValidRejection {
|
||||
//! fn from(_inner: ValidRejection<ParameterRejection>) -> Self {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! impl IntoResponse for WithRejectionValidRejection {
|
||||
//! fn into_response(self) -> Response {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! pub struct ParameterRejection;
|
||||
//! #[cfg(feature = "garde")]
|
||||
//! mod garde_example {
|
||||
//! use axum::extract::FromRequestParts;
|
||||
//! use axum::http::request::Parts;
|
||||
//! use axum::response::{IntoResponse, Response};
|
||||
//! use axum::routing::post;
|
||||
//! use axum::Router;
|
||||
//! use axum_extra::extract::WithRejection;
|
||||
//! use axum_valid::{HasValidate, Garde, GardeRejection};
|
||||
//! use garde::Validate;
|
||||
//!
|
||||
//! impl IntoResponse for ParameterRejection {
|
||||
//! fn into_response(self) -> Response {
|
||||
//! todo!()
|
||||
//! pub fn router() -> Router {
|
||||
//! Router::new().route("/with_rejection_valid", post(handler))
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! #[axum::async_trait]
|
||||
//! impl<S> FromRequestParts<S> for Parameter
|
||||
//! where
|
||||
//! S: Send + Sync,
|
||||
//! {
|
||||
//! type Rejection = ParameterRejection;
|
||||
//!
|
||||
//! async fn from_request_parts(_parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
//! todo!()
|
||||
//! async fn handler(
|
||||
//! WithRejection(Garde(parameter), _): WithRejection<
|
||||
//! Garde<Parameter>,
|
||||
//! WithRejectionGardeRejection,
|
||||
//! >,
|
||||
//! ) {
|
||||
//! assert!(parameter.validate(&()).is_ok());
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! pub struct WithRejectionValidRejection;
|
||||
//!
|
||||
//! impl From<ValidRejection<ParameterRejection>> for WithRejectionValidRejection {
|
||||
//! fn from(_inner: ValidRejection<ParameterRejection>) -> Self {
|
||||
//! todo!()
|
||||
//! #[derive(Validate)]
|
||||
//! pub struct Parameter {
|
||||
//! #[garde(range(min = 5, max = 10))]
|
||||
//! pub v0: i32,
|
||||
//! #[garde(length(min = 1, max = 10))]
|
||||
//! pub v1: String,
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! impl IntoResponse for WithRejectionValidRejection {
|
||||
//! fn into_response(self) -> Response {
|
||||
//! todo!()
|
||||
//! impl HasValidate for Parameter {
|
||||
//! type Validate = Self;
|
||||
//!
|
||||
//! fn get_validate(&self) -> &Self::Validate {
|
||||
//! self
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! pub struct ParameterRejection;
|
||||
//!
|
||||
//! impl IntoResponse for ParameterRejection {
|
||||
//! fn into_response(self) -> Response {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! #[axum::async_trait]
|
||||
//! impl<S> FromRequestParts<S> for Parameter
|
||||
//! where
|
||||
//! S: Send + Sync,
|
||||
//! {
|
||||
//! type Rejection = ParameterRejection;
|
||||
//!
|
||||
//! async fn from_request_parts(_parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! pub struct WithRejectionGardeRejection;
|
||||
//!
|
||||
//! impl From<GardeRejection<ParameterRejection>> for WithRejectionGardeRejection {
|
||||
//! fn from(_inner: GardeRejection<ParameterRejection>) -> Self {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! impl IntoResponse for WithRejectionGardeRejection {
|
||||
//! fn into_response(self) -> Response {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! }
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
@@ -63,10 +63,10 @@
|
||||
//!
|
||||
//! #[derive(Validate, prost::Message)]
|
||||
//! pub struct Parameter {
|
||||
//! #[validate(range(min = 5, max = 10))]
|
||||
//! #[garde(range(min = 5, max = 10))]
|
||||
//! #[prost(int32, tag = "1")]
|
||||
//! pub v0: i32,
|
||||
//! #[validate(length(min = 1, max = 10))]
|
||||
//! #[garde(length(min = 1, max = 10))]
|
||||
//! #[prost(string, tag = "2")]
|
||||
//! pub v1: String,
|
||||
//! }
|
||||
|
||||
@@ -12,41 +12,87 @@
|
||||
//! ## Example
|
||||
//!
|
||||
//! ```no_run
|
||||
//! use axum::Router;
|
||||
//! use axum_extra::routing::{RouterExt, TypedPath};
|
||||
//! use axum_valid::{HasValidate, Valid};
|
||||
//! use serde::Deserialize;
|
||||
//! use validator::Validate;
|
||||
//! #[cfg(feature = "validator")]
|
||||
//! mod validator_example {
|
||||
//! use axum::Router;
|
||||
//! use axum_extra::routing::{RouterExt, TypedPath};
|
||||
//! use axum_valid::{HasValidate, Valid};
|
||||
//! use serde::Deserialize;
|
||||
//! use validator::Validate;
|
||||
//!
|
||||
//! #[tokio::main]
|
||||
//! async fn main() -> anyhow::Result<()> {
|
||||
//! let router = Router::new().typed_get(handler);
|
||||
//! axum::Server::bind(&([0u8, 0, 0, 0], 8080).into())
|
||||
//! .serve(router.into_make_service())
|
||||
//! .await?;
|
||||
//! Ok(())
|
||||
//! }
|
||||
//! pub fn router() -> Router {
|
||||
//! Router::new().typed_get(handler)
|
||||
//! }
|
||||
//!
|
||||
//! async fn handler(parameter: Valid<Parameter>) {
|
||||
//! assert!(parameter.validate().is_ok());
|
||||
//! }
|
||||
//! async fn handler(parameter: Valid<Parameter>) {
|
||||
//! assert!(parameter.validate().is_ok());
|
||||
//! }
|
||||
//!
|
||||
//! #[derive(TypedPath, Deserialize, Validate)]
|
||||
//! #[typed_path("/extra_typed_path/:v0/:v1")]
|
||||
//! struct Parameter {
|
||||
//! #[validate(range(min = 5, max = 10))]
|
||||
//! v0: i32,
|
||||
//! #[validate(length(min = 1, max = 10))]
|
||||
//! v1: String,
|
||||
//! }
|
||||
//! #[derive(TypedPath, Deserialize, Validate)]
|
||||
//! #[typed_path("/extra_typed_path/:v0/:v1")]
|
||||
//! struct Parameter {
|
||||
//! #[validate(range(min = 5, max = 10))]
|
||||
//! v0: i32,
|
||||
//! #[validate(length(min = 1, max = 10))]
|
||||
//! v1: String,
|
||||
//! }
|
||||
//!
|
||||
//! impl HasValidate for Parameter {
|
||||
//! type Validate = Self;
|
||||
//! impl HasValidate for Parameter {
|
||||
//! type Validate = Self;
|
||||
//!
|
||||
//! fn get_validate(&self) -> &Self::Validate {
|
||||
//! self
|
||||
//! fn get_validate(&self) -> &Self::Validate {
|
||||
//! self
|
||||
//! }
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! #[cfg(feature = "garde")]
|
||||
//! mod garde_example {
|
||||
//! use axum::Router;
|
||||
//! use axum_extra::routing::{RouterExt, TypedPath};
|
||||
//! use axum_valid::{HasValidate, Garde};
|
||||
//! use serde::Deserialize;
|
||||
//! use garde::Validate;
|
||||
//!
|
||||
//! pub fn router() -> Router {
|
||||
//! Router::new().typed_get(handler)
|
||||
//! }
|
||||
//!
|
||||
//! async fn handler(parameter: Garde<Parameter>) {
|
||||
//! assert!(parameter.validate(&()).is_ok());
|
||||
//! }
|
||||
//!
|
||||
//! #[derive(TypedPath, Deserialize, Validate)]
|
||||
//! #[typed_path("/extra_typed_path/:v0/:v1")]
|
||||
//! struct Parameter {
|
||||
//! #[garde(range(min = 5, max = 10))]
|
||||
//! v0: i32,
|
||||
//! #[garde(length(min = 1, max = 10))]
|
||||
//! v1: String,
|
||||
//! }
|
||||
//!
|
||||
//! impl HasValidate for Parameter {
|
||||
//! type Validate = Self;
|
||||
//!
|
||||
//! fn get_validate(&self) -> &Self::Validate {
|
||||
//! self
|
||||
//! }
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! # #[tokio::main]
|
||||
//! # async fn main() -> anyhow::Result<()> {
|
||||
//! # use axum::Router;
|
||||
//! # let router = Router::new();
|
||||
//! # #[cfg(feature = "validator")]
|
||||
//! # let router = router.nest("/validator", validator_example::router());
|
||||
//! # #[cfg(feature = "garde")]
|
||||
//! # let router = router.nest("/garde", garde_example::router());
|
||||
//! # axum::Server::bind(&([0u8, 0, 0, 0], 8080).into())
|
||||
//! # .serve(router.into_make_service())
|
||||
//! # .await?;
|
||||
//! # Ok(())
|
||||
//! # }
|
||||
//! ```
|
||||
|
||||
#[cfg(feature = "garde")]
|
||||
|
||||
45
src/garde.rs
45
src/garde.rs
@@ -1,4 +1,9 @@
|
||||
//! # Garde support
|
||||
//!
|
||||
//! ## Feature
|
||||
//!
|
||||
//! Enable the `garde` feature to use `Garde<E>`.
|
||||
//!
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod test;
|
||||
@@ -40,7 +45,7 @@ impl<T: Display> Display for Garde<T> {
|
||||
}
|
||||
|
||||
impl<E> Garde<E> {
|
||||
/// Consumes the `ValidEx` and returns the validated data within.
|
||||
/// Consumes the `Garde` and returns the validated data within.
|
||||
///
|
||||
/// This returns the `E` type which represents the data that has been
|
||||
/// successfully validated.
|
||||
@@ -60,14 +65,14 @@ fn response_builder(ve: garde::Report) -> Response {
|
||||
}
|
||||
}
|
||||
|
||||
/// `ValidRejection` is returned when the `Valid` extractor fails.
|
||||
/// `GardeRejection` is returned when the `Garde` extractor fails.
|
||||
///
|
||||
/// This enumeration captures two types of errors that can occur when using `Valid`: errors related to the validation
|
||||
/// logic itself (encapsulated in `Valid`), and errors that may arise within the inner extractor (represented by `Inner`).
|
||||
/// This enumeration captures two types of errors that can occur when using `Garde`: errors related to the validation
|
||||
/// logic itself (encapsulated in `Garde`), and errors that may arise within the inner extractor (represented by `Inner`).
|
||||
///
|
||||
#[derive(Debug)]
|
||||
pub enum GardeRejection<E> {
|
||||
/// `Valid` variant captures errors related to the validation logic. It contains `garde::Report`
|
||||
/// `Report` variant captures errors related to the validation logic. It contains `garde::Report`
|
||||
/// which is a collection of validation failures for each field.
|
||||
Report(garde::Report),
|
||||
/// `Inner` variant represents potential errors that might occur within the inner extractor.
|
||||
@@ -152,6 +157,8 @@ where
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use garde::{Path, Report};
|
||||
use std::io;
|
||||
|
||||
const GARDE: &str = "garde";
|
||||
|
||||
@@ -163,6 +170,34 @@ mod tests {
|
||||
inner.push_str(GARDE);
|
||||
v.deref_mut().push_str(GARDE);
|
||||
assert_eq!(&inner, v.deref());
|
||||
println!("{}", v);
|
||||
assert_eq!(inner, v.into_inner());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn display_error() {
|
||||
// ValidRejection::Valid Display
|
||||
let mut report = Report::new();
|
||||
report.append(Path::empty(), garde::Error::new(GARDE));
|
||||
let s = report.to_string();
|
||||
let vr = GardeRejection::<String>::Report(report);
|
||||
assert_eq!(vr.to_string(), s);
|
||||
|
||||
// ValidRejection::Inner Display
|
||||
let inner = String::from(GARDE);
|
||||
let vr = GardeRejection::<String>::Inner(inner.clone());
|
||||
assert_eq!(inner.to_string(), vr.to_string());
|
||||
|
||||
// ValidRejection::Valid Error
|
||||
let mut report = Report::new();
|
||||
report.append(Path::empty(), garde::Error::new(GARDE));
|
||||
let vr = GardeRejection::<io::Error>::Report(report);
|
||||
assert!(matches!(vr.source(), Some(source) if source.downcast_ref::<Report>().is_some()));
|
||||
|
||||
// ValidRejection::Valid Error
|
||||
let vr = GardeRejection::<io::Error>::Inner(io::Error::new(io::ErrorKind::Other, GARDE));
|
||||
assert!(
|
||||
matches!(vr.source(), Some(source) if source.downcast_ref::<io::Error>().is_some())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,55 +12,111 @@
|
||||
//! ## Example
|
||||
//!
|
||||
//! ```no_run
|
||||
//! #![cfg(feature = "validator")]
|
||||
//! #[cfg(feature = "validator")]
|
||||
//! mod validator_example {
|
||||
//! use axum::headers::{Error, Header, HeaderValue};
|
||||
//! use axum::http::HeaderName;
|
||||
//! use axum::routing::post;
|
||||
//! use axum::{Router, TypedHeader};
|
||||
//! use axum_valid::Valid;
|
||||
//! use validator::Validate;
|
||||
//!
|
||||
//! use axum::headers::{Error, Header, HeaderValue};
|
||||
//! use axum::http::HeaderName;
|
||||
//! use axum::routing::post;
|
||||
//! use axum::{Router, TypedHeader};
|
||||
//! use axum_valid::Valid;
|
||||
//! use validator::Validate;
|
||||
//!
|
||||
//! #[tokio::main]
|
||||
//! async fn main() -> anyhow::Result<()> {
|
||||
//! let router = Router::new().route("/typed_header", post(handler));
|
||||
//! axum::Server::bind(&([0u8, 0, 0, 0], 8080).into())
|
||||
//! .serve(router.into_make_service())
|
||||
//! .await?;
|
||||
//! Ok(())
|
||||
//! }
|
||||
//!
|
||||
//! async fn handler(Valid(TypedHeader(parameter)): Valid<TypedHeader<Parameter>>) {
|
||||
//! assert!(parameter.validate().is_ok());
|
||||
//! }
|
||||
//!
|
||||
//! #[derive(Validate)]
|
||||
//! pub struct Parameter {
|
||||
//! #[validate(range(min = 5, max = 10))]
|
||||
//! pub v0: i32,
|
||||
//! #[validate(length(min = 1, max = 10))]
|
||||
//! pub v1: String,
|
||||
//! }
|
||||
//!
|
||||
//! static HEADER_NAME: HeaderName = HeaderName::from_static("my-header");
|
||||
//!
|
||||
//! impl Header for Parameter {
|
||||
//! fn name() -> &'static HeaderName {
|
||||
//! &HEADER_NAME
|
||||
//! pub fn router() -> Router {
|
||||
//! Router::new().route("/typed_header", post(handler))
|
||||
//! }
|
||||
//!
|
||||
//! fn decode<'i, I>(_values: &mut I) -> Result<Self, Error>
|
||||
//! where
|
||||
//! Self: Sized,
|
||||
//! I: Iterator<Item = &'i HeaderValue>,
|
||||
//! {
|
||||
//! todo!()
|
||||
//! async fn handler(Valid(TypedHeader(parameter)): Valid<TypedHeader<Parameter>>) {
|
||||
//! assert!(parameter.validate().is_ok());
|
||||
//! }
|
||||
//!
|
||||
//! fn encode<E: Extend<HeaderValue>>(&self, _values: &mut E) {
|
||||
//! todo!()
|
||||
//! #[derive(Validate)]
|
||||
//! pub struct Parameter {
|
||||
//! #[validate(range(min = 5, max = 10))]
|
||||
//! pub v0: i32,
|
||||
//! #[validate(length(min = 1, max = 10))]
|
||||
//! pub v1: String,
|
||||
//! }
|
||||
//!
|
||||
//! static HEADER_NAME: HeaderName = HeaderName::from_static("my-header");
|
||||
//!
|
||||
//! impl Header for Parameter {
|
||||
//! fn name() -> &'static HeaderName {
|
||||
//! &HEADER_NAME
|
||||
//! }
|
||||
//!
|
||||
//! fn decode<'i, I>(_values: &mut I) -> Result<Self, Error>
|
||||
//! where
|
||||
//! Self: Sized,
|
||||
//! I: Iterator<Item = &'i HeaderValue>,
|
||||
//! {
|
||||
//! todo!()
|
||||
//! }
|
||||
//!
|
||||
//! fn encode<E: Extend<HeaderValue>>(&self, _values: &mut E) {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! #[cfg(feature = "garde")]
|
||||
//! mod garde_example {
|
||||
//! use axum::headers::{Error, Header, HeaderValue};
|
||||
//! use axum::http::HeaderName;
|
||||
//! use axum::routing::post;
|
||||
//! use axum::{Router, TypedHeader};
|
||||
//! use axum_valid::Garde;
|
||||
//! use garde::Validate;
|
||||
//!
|
||||
//! pub fn router() -> Router {
|
||||
//! Router::new().route("/typed_header", post(handler))
|
||||
//! }
|
||||
//!
|
||||
//! async fn handler(Garde(TypedHeader(parameter)): Garde<TypedHeader<Parameter>>) {
|
||||
//! assert!(parameter.validate(&()).is_ok());
|
||||
//! }
|
||||
//!
|
||||
//! #[derive(Validate)]
|
||||
//! pub struct Parameter {
|
||||
//! #[garde(range(min = 5, max = 10))]
|
||||
//! pub v0: i32,
|
||||
//! #[garde(length(min = 1, max = 10))]
|
||||
//! pub v1: String,
|
||||
//! }
|
||||
//!
|
||||
//! static HEADER_NAME: HeaderName = HeaderName::from_static("my-header");
|
||||
//!
|
||||
//! impl Header for Parameter {
|
||||
//! fn name() -> &'static HeaderName {
|
||||
//! &HEADER_NAME
|
||||
//! }
|
||||
//!
|
||||
//! fn decode<'i, I>(_values: &mut I) -> Result<Self, Error>
|
||||
//! where
|
||||
//! Self: Sized,
|
||||
//! I: Iterator<Item = &'i HeaderValue>,
|
||||
//! {
|
||||
//! todo!()
|
||||
//! }
|
||||
//!
|
||||
//! fn encode<E: Extend<HeaderValue>>(&self, _values: &mut E) {
|
||||
//! todo!()
|
||||
//! }
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! # #[tokio::main]
|
||||
//! # async fn main() -> anyhow::Result<()> {
|
||||
//! # use axum::Router;
|
||||
//! # let router = Router::new();
|
||||
//! # #[cfg(feature = "validator")]
|
||||
//! # let router = router.nest("/validator", validator_example::router());
|
||||
//! # #[cfg(feature = "garde")]
|
||||
//! # let router = router.nest("/garde", garde_example::router());
|
||||
//! # axum::Server::bind(&([0u8, 0, 0, 0], 8080).into())
|
||||
//! # .serve(router.into_make_service())
|
||||
//! # .await?;
|
||||
//! # Ok(())
|
||||
//! # }
|
||||
//! ```
|
||||
|
||||
use crate::HasValidate;
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
//! # Validator support
|
||||
//!
|
||||
//! ## Feature
|
||||
//!
|
||||
//! Enable the `validator` feature (enabled by default) to use `Valid<E>` and `ValidEx<E, A>`.
|
||||
//!
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod test;
|
||||
@@ -287,6 +292,7 @@ where
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use super::*;
|
||||
use std::fmt::Formatter;
|
||||
use std::io;
|
||||
use validator::ValidationError;
|
||||
const TEST: &str = "test";
|
||||
@@ -299,6 +305,7 @@ pub mod tests {
|
||||
inner.push_str(TEST);
|
||||
v.deref_mut().push_str(TEST);
|
||||
assert_eq!(&inner, v.deref());
|
||||
println!("{}", v);
|
||||
assert_eq!(inner, v.into_inner());
|
||||
}
|
||||
|
||||
@@ -316,12 +323,18 @@ pub mod tests {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Validate)]
|
||||
#[derive(Debug, Validate)]
|
||||
struct Data {
|
||||
#[validate(custom(function = "validate", arg = "i32"))]
|
||||
v: i32,
|
||||
}
|
||||
|
||||
impl Display for Data {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
struct DataVA {
|
||||
a: i32,
|
||||
}
|
||||
@@ -337,6 +350,7 @@ pub mod tests {
|
||||
let data = Data { v: 12 };
|
||||
let args = DataVA { a: 123 };
|
||||
let ve = ValidEx(data, args);
|
||||
println!("{}", ve);
|
||||
assert_eq!(ve.v, 12);
|
||||
let a = ve.arguments();
|
||||
assert_eq!(a, 123);
|
||||
|
||||
Reference in New Issue
Block a user