update README.md

This commit is contained in:
gengteng
2023-10-09 14:08:31 +08:00
parent 6cf325b19d
commit c421807bfc
11 changed files with 427 additions and 237 deletions

View File

@@ -12,30 +12,75 @@
//! ## Example
//!
//! ```no_run
//! use axum::routing::post;
//! use axum::Router;
//! use axum_extra::extract::Form;
//! use axum_valid::Valid;
//! use serde::Deserialize;
//! use validator::Validate;
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! let router = Router::new().route("/extra_form", post(handler));
//! axum::Server::bind(&([0u8, 0, 0, 0], 8080).into())
//! .serve(router.into_make_service())
//! .await?;
//! Ok(())
//! #[cfg(feature = "validator")]
//! mod validator_example {
//! use axum::routing::post;
//! use axum_extra::extract::Form;
//! use axum::Router;
//! use axum_valid::Valid;
//! use serde::Deserialize;
//! use validator::Validate;
//!
//! pub fn router() -> Router {
//! Router::new().route("/form", post(handler))
//! }
//!
//! async fn handler(Valid(Form(parameter)): Valid<Form<Parameter>>) {
//! assert!(parameter.validate().is_ok());
//! // Support automatic dereferencing
//! println!("v0 = {}, v1 = {}", parameter.v0, parameter.v1);
//! }
//!
//! #[derive(Validate, Deserialize)]
//! 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(Form(parameter)): Valid<Form<Parameter>>) {
//! assert!(parameter.validate().is_ok());
//! }
//! #[derive(Validate, Deserialize)]
//! pub struct Parameter {
//! #[validate(range(min = 5, max = 10))]
//! pub v0: i32,
//! #[validate(length(min = 1, max = 10))]
//! pub v1: String,
//!
//! #[cfg(feature = "garde")]
//! mod garde_example {
//! use axum::routing::post;
//! use axum_extra::extract::Form;
//! use axum::Router;
//! use axum_valid::Garde;
//! use serde::Deserialize;
//! use garde::Validate;
//!
//! pub fn router() -> Router {
//! Router::new().route("/form", post(handler))
//! }
//!
//! async fn handler(Garde(Form(parameter)): Garde<Form<Parameter>>) {
//! assert!(parameter.validate(&()).is_ok());
//! // Support automatic dereferencing
//! println!("v0 = {}, v1 = {}", parameter.v0, parameter.v1);
//! }
//!
//! #[derive(Validate, Deserialize)]
//! pub struct Parameter {
//! #[garde(range(min = 5, max = 10))]
//! pub v0: i32,
//! #[garde(length(min = 1, max = 10))]
//! pub v1: String,
//! }
//! }
//!
//! # #[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;

View File

@@ -12,32 +12,79 @@
//! ## Example
//!
//! ```no_run
//! use axum::routing::post;
//! use axum::Router;
//! use axum_extra::protobuf::Protobuf;
//! use axum_valid::Valid;
//! use validator::Validate;
//! #[cfg(feature = "validator")]
//! mod validator_example {
//! use axum::routing::post;
//! use axum_extra::protobuf::Protobuf;
//! use axum::Router;
//! use axum_valid::Valid;
//! use serde::Deserialize;
//! use validator::Validate;
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! let router = Router::new().route("/protobuf", post(handler));
//! axum::Server::bind(&([0u8, 0, 0, 0], 8080).into())
//! .serve(router.into_make_service())
//! .await?;
//! Ok(())
//! pub fn router() -> Router {
//! Router::new().route("/protobuf", post(handler))
//! }
//!
//! async fn handler(Valid(Protobuf(parameter)): Valid<Protobuf<Parameter>>) {
//! assert!(parameter.validate().is_ok());
//! // Support automatic dereferencing
//! println!("v0 = {}, v1 = {}", parameter.v0, parameter.v1);
//! }
//!
//! #[derive(Validate, prost::Message)]
//! pub struct Parameter {
//! #[validate(range(min = 5, max = 10))]
//! #[prost(int32, tag = "1")]
//! pub v0: i32,
//! #[validate(length(min = 1, max = 10))]
//! #[prost(string, tag = "2")]
//! pub v1: String,
//! }
//! }
//! async fn handler(Valid(Protobuf(parameter)): Valid<Protobuf<Parameter>>) {
//! assert!(parameter.validate().is_ok());
//! }
//! #[derive(Validate, prost::Message)]
//! pub struct Parameter {
//! #[validate(range(min = 5, max = 10))]
//! #[prost(int32, tag = "1")]
//! pub v0: i32,
//! #[validate(length(min = 1, max = 10))]
//! #[prost(string, tag = "2")]
//! pub v1: String,
//!
//! #[cfg(feature = "garde")]
//! mod garde_example {
//! use axum::routing::post;
//! use axum::Router;
//! use axum_extra::protobuf::Protobuf;
//! use axum_valid::Garde;
//! use serde::Deserialize;
//! use garde::Validate;
//!
//! pub fn router() -> Router {
//! Router::new().route("/protobuf", post(handler))
//! }
//!
//! async fn handler(Garde(Protobuf(parameter)): Garde<Protobuf<Parameter>>) {
//! assert!(parameter.validate(&()).is_ok());
//! // Support automatic dereferencing
//! println!("v0 = {}, v1 = {}", parameter.v0, parameter.v1);
//! }
//!
//! #[derive(Validate, prost::Message)]
//! pub struct Parameter {
//! #[validate(range(min = 5, max = 10))]
//! #[prost(int32, tag = "1")]
//! pub v0: i32,
//! #[validate(length(min = 1, max = 10))]
//! #[prost(string, tag = "2")]
//! pub v1: String,
//! }
//! }
//!
//! # #[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;

View File

@@ -12,33 +12,75 @@
//! ## Example
//!
//! ```no_run
//! use axum::routing::post;
//! use axum::Router;
//! use axum_extra::extract::Query;
//! use axum_valid::Valid;
//! use serde::Deserialize;
//! use validator::Validate;
//! #[cfg(feature = "validator")]
//! mod validator_example {
//! use axum_extra::extract::Query;
//! use axum::routing::post;
//! use axum::Router;
//! use axum_valid::Valid;
//! use serde::Deserialize;
//! use validator::Validate;
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! let router = Router::new().route("/extra_query", post(handler));
//! axum::Server::bind(&([0u8, 0, 0, 0], 8080).into())
//! .serve(router.into_make_service())
//! .await?;
//! Ok(())
//! pub fn router() -> Router {
//! Router::new().route("/query", post(handler))
//! }
//!
//! async fn handler(Valid(Query(parameter)): Valid<Query<Parameter>>) {
//! assert!(parameter.validate().is_ok());
//! // Support automatic dereferencing
//! println!("v0 = {}, v1 = {}", parameter.v0, parameter.v1);
//! }
//!
//! #[derive(Validate, Deserialize)]
//! 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(Query(parameter)): Valid<Query<Parameter>>) {
//! assert!(parameter.validate().is_ok());
//! #[cfg(feature = "garde")]
//! mod garde_example {
//! use axum::routing::post;
//! use axum_extra::extract::Query;
//! use axum::Router;
//! use axum_valid::Garde;
//! use serde::Deserialize;
//! use garde::Validate;
//!
//! pub fn router() -> Router {
//! Router::new().route("/query", post(handler))
//! }
//!
//! async fn handler(Garde(Query(parameter)): Garde<Query<Parameter>>) {
//! assert!(parameter.validate(&()).is_ok());
//! // Support automatic dereferencing
//! println!("v0 = {}, v1 = {}", parameter.v0, parameter.v1);
//! }
//!
//! #[derive(Validate, Deserialize)]
//! pub struct Parameter {
//! #[garde(range(min = 5, max = 10))]
//! pub v0: i32,
//! #[garde(length(min = 1, max = 10))]
//! pub v1: String,
//! }
//! }
//!
//! #[derive(Validate, Deserialize)]
//! pub struct Parameter {
//! #[validate(range(min = 5, max = 10))]
//! pub v0: i32,
//! #[validate(length(min = 1, max = 10))]
//! pub v1: String,
//! }
//! # #[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;