add examples and support for typed_path
This commit is contained in:
@@ -1,5 +1,42 @@
|
||||
//! # Implementation of the `HasValidate` trait for the `Form` extractor in `axum-extra`.
|
||||
//! # Support for `Form<T>` from `axum-extra`
|
||||
//!
|
||||
//! ## Feature
|
||||
//!
|
||||
//! Enable the `extra_form` feature to use `Valid<Form<T>>`.
|
||||
//!
|
||||
//! ## Usage
|
||||
//!
|
||||
//! 1. Implement `Deserialize` and `Validate` for your data type `T`.
|
||||
//! 2. In your handler function, use `Valid<Form<T>>` as some parameter's type.
|
||||
//!
|
||||
//! ## 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(())
|
||||
//! }
|
||||
//! 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,
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
use crate::HasValidate;
|
||||
use axum_extra::extract::Form;
|
||||
|
||||
@@ -1,5 +1,44 @@
|
||||
//! # Implementation of the `HasValidate` trait for the `Form` extractor.
|
||||
//! # Support for `Protobuf<T>` from `axum-extra`
|
||||
//!
|
||||
//! ## Feature
|
||||
//!
|
||||
//! Enable the `extra_protobuf` feature to use `Valid<Protobuf<T>>`.
|
||||
//!
|
||||
//! ## Usage
|
||||
//!
|
||||
//! 1. Implement `prost::Message` and `Validate` for your data type `T`.
|
||||
//! 2. In your handler function, use `Valid<Protobuf<T>>` as some parameter's type.
|
||||
//!
|
||||
//! ## Example
|
||||
//!
|
||||
//! ```no_run
|
||||
//! use axum::routing::post;
|
||||
//! use axum::Router;
|
||||
//! use axum_extra::protobuf::Protobuf;
|
||||
//! use axum_valid::Valid;
|
||||
//! 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(())
|
||||
//! }
|
||||
//! 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,
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
use crate::HasValidate;
|
||||
use axum_extra::protobuf::Protobuf;
|
||||
|
||||
@@ -1,5 +1,45 @@
|
||||
//! # Implementation of the `HasValidate` trait for the `Query` extractor in `axum-extra`.
|
||||
//! # Support for `Query<T>` from `axum-extra`
|
||||
//!
|
||||
//! ## Feature
|
||||
//!
|
||||
//! Enable the `extra_query` feature to use `Valid<Query<T>>`.
|
||||
//!
|
||||
//! ## Usage
|
||||
//!
|
||||
//! 1. Implement `Deserialize` and `Validate` for your data type `T`.
|
||||
//! 2. In your handler function, use `Valid<Query<T>>` as some parameter's type.
|
||||
//!
|
||||
//! ## 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;
|
||||
//!
|
||||
//! #[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(())
|
||||
//! }
|
||||
//!
|
||||
//! async fn handler(Valid(Query(parameter)): Valid<Query<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,
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
use crate::HasValidate;
|
||||
use axum_extra::extract::Query;
|
||||
|
||||
64
src/extra/typed_path.rs
Normal file
64
src/extra/typed_path.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
//! # Support for `T: TypedPath` from `axum-extra`
|
||||
//!
|
||||
//! ## Feature
|
||||
//!
|
||||
//! Enable the `extra_typed_path` feature to use `Valid<T: TypedPath>`.
|
||||
//!
|
||||
//! ## Usage
|
||||
//!
|
||||
//! 1. Implement `TypedPath`, `Deserialize`, `Validate` and `HasValidate` for your data type `T`.
|
||||
//! 2. In your handler function, use `Valid<T>` as some parameter's type.
|
||||
//!
|
||||
//! ## Example
|
||||
//!
|
||||
//! ```no_run
|
||||
//! 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(())
|
||||
//! }
|
||||
//!
|
||||
//! 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,
|
||||
//! }
|
||||
//!
|
||||
//! impl HasValidate for Parameter {
|
||||
//! type Validate = Self;
|
||||
//!
|
||||
//! fn get_validate(&self) -> &Self::Validate {
|
||||
//! self
|
||||
//! }
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
use crate::Valid;
|
||||
use axum_extra::routing::TypedPath;
|
||||
use std::fmt::{Display, Formatter};
|
||||
|
||||
impl<T: Display> Display for Valid<T> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: TypedPath + Display> TypedPath for Valid<T> {
|
||||
const PATH: &'static str = T::PATH;
|
||||
}
|
||||
Reference in New Issue
Block a user