RepositoryRust
gengteng

gengteng/axum-valid

axum-valid is a library that provides data validation extractors for the Axum web framework. It integrates validator, garde and validify, three popular validation crates in the Rust ecosystem, to offer convenient validation and data handling extractors for Axum applications.

167stars
12forks
5issues
2views

Featured achievements

The project's latest awards

1
TINIX TRENDING·Topic: validify

#1 Repository of the month

1
TINIX TRENDING·Topic: validator

#1 Repository of the month

1
TINIX TRENDING·Topic: garde

#1 Repository of the month

axum-valid

crates.io crates.io download LICENSE dependency status GitHub Workflow Status Coverage Status

📑 Overview

axum-valid is a library that provides data validation extractors for the Axum web framework. It integrates validator, garde and validify, three popular validation crates in the Rust ecosystem, to offer convenient validation and data handling extractors for Axum applications.

🚀 Basic usage

📦 Valid<E>

  • Install
cargo add validator --features derive
cargo add axum-valid
# validator is enabled by default
  • Example
use axum::extract::Query;
use axum::routing::{get, post};
use axum::{Json, Router};
use axum_valid::Valid;
use serde::Deserialize;
use std::net::SocketAddr;
use tokio::net::TcpListener;
use validator::Validate;

#[derive(Debug, Validate, Deserialize)]
pub struct Paginator {
    #[validate(range(min = 1, max = 50))]
    pub page_size: usize,
    #[validate(range(min = 1))]
    pub page_no: usize,
}

pub async fn paginator_from_query(Valid(Query(paginator)): Valid<Query<Paginator>>) {
    assert!((1..=50).contains(&paginator.page_size));
    assert!((1..).contains(&paginator.page_no));
}

pub async fn paginator_from_json(paginator: Valid<Json<Paginator>>) {
    assert!((1..=50).contains(&paginator.page_size));
    assert!((1..).contains(&paginator.page_no));
    // NOTE: all extractors provided support automatic dereferencing
    println!("page_no: {}, page_size: {}", paginator.page_no, paginator.page_size);
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let router = Router::new()
        .route("/query", get(paginator_from_query))
        .route("/json", post(paginator_from_json));
    let listener = TcpListener::bind(&SocketAddr::from(([0u8, 0, 0, 0], 0u16))).await?;
    axum::serve(listener, router.into_make_service()).await?;
    Ok(())
}

In case of inner extractor errors, it will first return the Rejection from the inner extractor. When validation errors occur, the outer extractor will automatically return 400 with validation errors as the HTTP message body.

📦 Garde<E>

  • Install
cargo add garde --features derive
cargo add axum --features macros # for FromRef derive macro
cargo add axum-valid --features garde,basic --no-default-features
# excluding validator
  • Example
use axum::extract::{FromRef, Query, State};
use axum::routing::{get, post};
use axum::{Json, Router};
use axum_valid::Garde;
use garde::Validate;
use serde::Deserialize;
use std::net::SocketAddr;
use tokio::net::TcpListener;

#[derive(Debug, Validate, Deserialize)]
pub struct Paginator {
    #[garde(range(min = 1, max = 50))]
    pub page_size: usize,
    #[garde(range(min = 1))]
    pub page_no: usize,
}

pub async fn paginator_from_query(Garde(Query(paginator)): Garde<Query<Paginator>>) {
    assert!((1..=50).contains(&paginator.page_size));
    assert!((1..).contains(&paginator.page_no));
}

pub async fn paginator_from_json(paginator: Garde<Json<Paginator>>) {
    assert!((1..=50).contains(&paginator.page_size));
    assert!((1..).contains(&paginator.page_no));
    println!("page_no: {}, page_size: {}", paginator.page_no, paginator.page_size);
}

pub async fn get_state(_state: State<MyState>) {}

#[derive(Debug, Clone, FromRef)]
pub struct MyState {
    state_field: i32,
    without_validation_arguments: (),
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let router = Router::new()
        .route("/query", get(paginator_from_query))
        .route("/json", post(paginator_from_json));

    // WARNING: If you are using Garde and also have a state,
    // even if that state is unrelated to Garde,
    // you still need to implement FromRef<StateType> for ().
    // Tip: You can add an () field to your state and derive FromRef for it.
    let router = router.route("/state", get(get_state)).with_state(MyState {
        state_field: 1,
        without_validation_arguments: (),
    });
    let listener = TcpListener::bind(&SocketAddr::from(([0u8, 0, 0, 0], 0u16))).await?;
    axum::serve(listener, router.into_make_service()).await?;
    Ok(())
}

📦 Validated<E>, Modified<E>, Validified<E> and ValidifiedByRef<E>

  • Install
cargo add validify
cargo add axum-valid --features validify,basic --no-default-features
  • Example

Extra dependencies of this example:

cargo add axum_typed_multipart
cargo add axum-valid --features validify,basic,typed_multipart --no-default-features
use axum::extract::Query;
use axum::routing::{get, post};
use axum::{Form, Json, Router};
use axum_typed_multipart::{TryFromMultipart, TypedMultipart};
use axum_valid::{Modified, Validated, Validified, ValidifiedByRef};
use serde::Deserialize;
use std::net::SocketAddr;
use tokio::net::TcpListener;
use validify::{Payload, Validate, Validify};

#[derive(Debug, Validify, Deserialize)]
pub struct Paginator {
    #[validate(range(min = 1.0, max = 50.0))]
    pub page_size: usize,
    #[validate(range(min = 1.0))]
    pub page_no: usize,
}

pub async fn paginator_from_query(Validated(Query(paginator)): Validated<Query<Paginator>>) {
    assert!((1..=50).contains(&paginator.page_size));
    assert!((1..).contains(&paginator.page_no));
}

// Payload is now required for Validified. (Added in validify 1.3.0)
#[derive(Debug, Validify, Deserialize, Payload)]
pub struct Parameters {
    #[modify(lowercase)]
    #[validate(length(min = 1, max = 50))]
    pub v0: String,
    #[modify(trim)]
    #[validate(length(min = 1, max = 100))]
    pub v1: String,
}

pub async fn parameters_from_json(modified_parameters: Modified<Json<Parameters>>) {
    assert_eq!(
        modified_parameters.v0,
        modified_parameters.v0.to_lowercase()
    );
    assert_eq!(modified_parameters.v1, modified_parameters.v1.trim())
    // but modified_parameters may be invalid
}

// NOTE: missing required fields will be treated as validation errors.
pub async fn parameters_from_form(parameters: Validified<Form<Parameters>>) {
    assert_eq!(parameters.v0, parameters.v0.to_lowercase());
    assert_eq!(parameters.v1, parameters.v1.trim());
    assert!(parameters.validate().is_ok());
}

// NOTE: TypedMultipart doesn't using serde::Deserialize to construct data
// we should use ValidifiedByRef instead of Validified
#[derive(Debug, Validify, TryFromMultipart)]
pub struct FormData {
    #[modify(lowercase)]
    #[validate(length(min = 1, max = 50))]
    pub v0: String,
    #[modify(trim)]
    #[validate(length(min = 1, max = 100))]
    pub v1: String,
}

pub async fn parameters_from_typed_multipart(
    ValidifiedByRef(TypedMultipart(data)): ValidifiedByRef<TypedMultipart<FormData>>,
) {
    assert_eq!(data.v0, data.v0.to_lowercase());
    assert_eq!(data.v1, data.v1.trim());
    assert!(data.validate().is_ok());
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let router = Router::new()
        .route("/validated", get(paginator_from_query))
        .route("/modified", post(parameters_from_json))
        .route("/validified", post(parameters_from_form))
        .route("/validified_by_ref", post(parameters_from_typed_multipart));
    let listener = TcpListener::bind(&SocketAddr::from(([0u8, 0, 0, 0], 0u16))).await?;
    axum::serve(listener, router.into_make_service()).await?;
    Ok(())
}

To see how each inner extractor can be used with validation extractors, please refer to the example in the documentation of the corresponding module.

🚀 Argument-Based Validation

📦 ValidEx<E>

  • Install
cargo add validator --features derive
cargo add axum-valid
# validator is enabled by default
  • Example
use axum::routing::post;
use axum::{Form, Router};
use axum_valid::ValidEx;
use serde::Deserialize;
use std::net::SocketAddr;
use std::ops::{RangeFrom, RangeInclusive};
use tokio::net::TcpListener;
use validator::{Validate, ValidationError};

// NOTE: When some fields use custom validation functions with arguments,
// `#[derive(Validate)]` will implement `ValidateArgs` instead of `Validate` for the type.
#[derive(Debug, Validate, Deserialize)]
#[validate(context = PaginatorValidArgs)] // context is required
pub struct Paginator {
    #[validate(custom(function = "validate_page_size", use_context))]
    pub page_size: usize,
    #[validate(custom(function = "validate_page_no", use_context))]
    pub page_no: usize,
}

fn validate_page_size(v: usize, args: &PaginatorValidArgs) -> Result<(), ValidationError> {
    args.page_size_range
        .contains(&v)
        .then_some(())
        .ok_or_else(|| ValidationError::new("page_size is out of range"))
}

fn validate_page_no(v: usize, args: &PaginatorValidArgs) -> Result<(), ValidationError> {
    args.page_no_range
        .contains(&v)
        .then_some(())
        .ok_or_else(|| ValidationError::new("page_no is out of range"))
}

// NOTE: Clone is required, consider using Arc to reduce deep copying costs.
#[derive(Debug, Clone)]
pub struct PaginatorValidArgs {
    page_size_range: RangeInclusive<usize>,
    page_no_range: RangeFrom<usize>,
}

pub async fn paginator_from_form_ex(ValidEx(Form(paginator)): ValidEx<Form<Paginator>>) {
    assert!((1..=50).contains(&paginator.page_size));
    assert!((1..).contains(&paginator.page_no));
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let router = Router::new()
        .route("/form", post(paginator_from_form_ex))
        .with_state(PaginatorValidArgs {
            page_size_range: 1..=50,
            page_no_range: 1..,
        });
    // NOTE: The PaginatorValidArgs can also be stored in a XxxState,
    // make sure it implements FromRef<XxxState>.

    let listener = TcpListener::bind(&SocketAddr::from(([0u8, 0, 0, 0], 0u16))).await?;
    axum::serve(listener, router.into_make_service()).await?;
    Ok(())
}

📦 Garde<E>

  • Install
cargo add garde
cargo add axum-valid --features garde,basic --no-default-features
# excluding validator
  • Example
use axum::routing::post;
use axum::{Form, Router};
use axum_valid::Garde;
use garde::Validate;
use serde::Deserialize;
use std::net::SocketAddr;
use std::ops::{RangeFrom, RangeInclusive};
use tokio::net::TcpListener;

#[derive(Debug, Validate, Deserialize)]
#[garde(context(PaginatorValidContext))]
pub struct Paginator {
    #[garde(custom(validate_page_size))]
    pub page_size: usize,
    #[garde(custom(validate_page_no))]
    pub page_no: usize,
}

fn validate_page_size(v: &usize, args: &PaginatorValidContext) -> garde::Result {
    args.page_size_range
        .contains(&v)
        .then_some(())
        .ok_or_else(|| garde::Error::new("page_size is out of range"))
}

fn validate_page_no(v: &usize, args: &PaginatorValidContext) -> garde::Result {
    args.page_no_range
        .contains(&v)
        .then_some(())
        .ok_or_else(|| garde::Error::new("page_no is out of range"))
}

#[derive(Debug, Clone)]
pub struct PaginatorValidContext {
    page_size_range: RangeInclusive<usize>,
    page_no_range: RangeFrom<usize>,
}

pub async fn paginator_from_form_garde(Garde(Form(paginator)): Garde<Form<Paginator>>) {
    assert!((1..=50).contains(&paginator.page_size));
    assert!((1..).contains(&paginator.page_no));
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let router = Router::new()
        .route("/form", post(paginator_from_form_garde))
        .with_state(PaginatorValidContext {
            page_size_range: 1..=50,
            page_no_range: 1..,
        });
    // NOTE: The PaginatorValidContext can also be stored in a XxxState,
    // make sure it implements FromRef<XxxState>.
    // Consider using Arc to reduce deep copying costs.
    let listener = TcpListener::bind(&SocketAddr::from(([0u8, 0, 0, 0], 0u16))).await?;
    axum::serve(listener, router.into_make_service()).await?;
    Ok(())
}

Current module documentation predominantly showcases Valid examples, the usage of ValidEx is analogous.

🗂️ Extractors List

ExtractorBackend / FeatureData's trait boundFunctionalityBenefitsDrawbacks
Valid<E>validatorvalidator::ValidateValidation
ValidEx<E>validatorvalidator::ValidateArgsValidation with arguments
Garde<E>gardegarde::ValidateValidation with or without argumentsRequire empty tuple as the argument if use state
Validated<E>validifyvalidify::ValidateValidation
Modified<E>validifyvalidify::ModifyModification / Conversion to response
Validified<E>validifyvalidify::Validify, validify::ValidifyPayload and serde::DeserializeOwnedConstruction, modification, validationTreat missing fields as validation errorsOnly works with extractors using serde
ValidifiedByRef<E>validifyvalidify::Validate and validify::ModifyModification, validation

⚙️ Features

FeatureDescriptionModuleDefaultExampleTests
defaultEnables validator and support for Query, Json and Form[validator], [query], [json], [form]✅✅✅
validatorEnables validator (Valid, ValidEx)[validator]✅✅✅
gardeEnables garde (Garde)[garde]❌✅✅
validifyEnables validify (Validated, Modified, Validified, ValidifedByRef)[validify]❌✅✅
basicEnables support for Query, Json and Form[query], [json], [form]✅✅✅
jsonEnables support for Json[json]✅✅✅
queryEnables support for Query[query]✅✅✅
formEnables support for Form[form]✅✅✅
typed_headerEnables support for TypedHeader from axum-extra[typed_header]❌✅✅
typed_multipartEnables support for TypedMultipart and BaseMultipart from axum_typed_multipart[typed_multipart]❌✅✅
msgpackEnables support for MsgPack and MsgPackRaw from axum-serde[msgpack]❌✅✅
yamlEnables support for Yaml from axum-serde[yaml]❌✅✅
xmlEnables support for Xml from axum-serde[xml]❌✅✅
tomlEnables support for Toml from axum-serde[toml]❌✅✅
sonicEnables support for Sonic from axum-serde[sonic]❌✅✅
cborEnables support for Cbor from axum-serde[cbor]❌✅✅
extraEnables support for Cached, WithRejection from axum-extra[extra]❌✅✅
extra_typed_pathEnables support for T: TypedPath from axum-extra[extra::typed_path]❌✅✅
extra_queryEnables support for Query from axum-extra[extra::query]❌✅✅
extra_formEnables support for Form from axum-extra[extra::form]❌✅✅
extra_protobufEnables support for Protobuf from axum-extra[extra::protobuf]❌✅✅
all_extra_typesEnables support for all extractors above from axum-extraN/A❌✅✅
all_typesEnables support for all extractors aboveN/A❌✅✅
422Use 422 Unprocessable Entity instead of 400 Bad Request as the status code when validation fails[VALIDATION_ERROR_STATUS]❌✅✅
into_jsonValidation errors will be serialized into JSON format and returned as the HTTP bodyN/A❌✅✅
full_validatorEnables validator, all_types, 422 and into_jsonN/A❌✅✅
full_gardeEnables garde, all_types, 422 and into_json. Consider using default-features = false to exclude default validator supportN/A❌✅✅
full_validifyEnables validify, all_types, 422 and into_json. Consider using default-features = false to exclude default validator supportN/A❌✅✅
fullEnables all features aboveN/A❌✅✅
aideEnables support for aideN/A❌❌❌

🔌 Compatibility

To determine the compatible versions of dependencies that work together, please refer to the dependencies listed in the Cargo.toml file. The version numbers listed there will indicate the compatible versions.

If you encounter code compilation problems, it could be attributed to either missing trait bounds, unmet feature requirements, or incorrect dependency version selections.

📜 License

This project is licensed under the MIT License.

📚 References

Similar Projects

Discover other similar projects and AI models.

RepositoryRust
loco-rs/loco

🚂 🦀 The one-person framework for Rust for side-projects and startups

9.7k
Nov 27, 2026
RepositoryRust
lycheeverse/lychee

⚡ Fast, async, stream-based link checker written in Rust. Finds broken URLs and mail addresses inside Markdown, HTML, reStructuredText, websites and more!

4.2k
Nov 30, 2026
RepositoryRust
synctv-org/synctv

Synchronized viewing, theater, live streaming, video

2.5k
Nov 28, 2026
RepositoryRust
tranxuanthang/lrclib

LRCLIB server written in Rust with Axum and SQLite3 database

2.3k
Nov 10, 2026
RepositoryRust
Dominux/Pentaract

Unlimited file cloud storage based on using Telegram API

1.9k
Jan 5, 2025
RepositoryRust
Totodore/socketioxide

A socket.io server implementation in Rust that integrates with the Tower ecosystem and the Tokio stack.

1.6k
Nov 30, 2026
RepositoryRust
maxcountryman/axum-login

🪪 User identification, authentication, and authorization for Axum.

1.0k
Nov 22, 2026
RepositoryRust
matze/wastebin

wastebin is a pastebin 📝

893
Oct 20, 2026
RepositoryRust
rust-ui/ui

A Shadcn-inspired component registry for Rust. Build cross-platform apps for Web, Desktop, iOS and Android.

844
Nov 22, 2026