axum-valid
📑 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 Query;
use ;
use ;
use Valid;
use Deserialize;
use SocketAddr;
use TcpListener;
use Validate;
pub async : )
pub async
async
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-valid --features garde,basic --no-default-features
# excluding validator
- Example
use ;
use ;
use ;
use Garde;
use Validate;
use Deserialize;
use SocketAddr;
use TcpListener;
pub async : )
pub async
pub async
async
📦 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 Query;
use ;
use ;
use ;
use ;
use Deserialize;
use SocketAddr;
use TcpListener;
use ;
pub async : )
// Payload is now required for Validified. (Added in validify 1.3.0)
pub async
// NOTE: missing required fields will be treated as validation errors.
pub async
// NOTE: TypedMultipart doesn't using serde::Deserialize to construct data
// we should use ValidifiedByRef instead of Validified
pub async : ,
)
async
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 post;
use ;
use ValidEx;
use Deserialize;
use SocketAddr;
use ;
use TcpListener;
use ;
// NOTE: When some fields use custom validation functions with arguments,
// `#[derive(Validate)]` will implement `ValidateArgs` instead of `Validate` for the type.
// context is required
// NOTE: Clone is required, consider using Arc to reduce deep copying costs.
pub async : )
async
📦 Garde<E>
- Install
cargo add garde
cargo add axum-valid --features garde,basic --no-default-features
# excluding validator
- Example
use post;
use ;
use Garde;
use Validate;
use Deserialize;
use SocketAddr;
use ;
use TcpListener;
pub async : )
async
Current module documentation predominantly showcases Valid
examples, the usage of ValidEx
is analogous.
🗂️ Extractors List
Extractor | Backend / Feature | Data's trait bound | Functionality | Benefits | Drawbacks |
---|---|---|---|---|---|
Valid<E> |
validator | validator::Validate |
Validation | ||
ValidEx<E> |
validator | validator::ValidateArgs |
Validation with arguments | ||
Garde<E> |
garde | garde::Validate |
Validation with or without arguments | Require empty tuple as the argument if use state | |
Validated<E> |
validify | validify::Validate |
Validation | ||
Modified<E> |
validify | validify::Modify |
Modification / Conversion to response | ||
Validified<E> |
validify | validify::Validify , validify::ValidifyPayload and serde::DeserializeOwned |
Construction, modification, validation | Treat missing fields as validation errors | Only works with extractors using serde |
ValidifiedByRef<E> |
validify | validify::Validate and validify::Modify |
Modification, validation |
⚙️ Features
Feature | Description | Module | Default | Example | Tests |
---|---|---|---|---|---|
default | Enables validator and support for Query , Json and Form |
[validator ], [query ], [json ], [form ] |
✅ | ✅ | ✅ |
validator | Enables validator (Valid , ValidEx ) |
[validator ] |
✅ | ✅ | ✅ |
garde | Enables garde (Garde ) |
[garde ] |
❌ | ✅ | ✅ |
validify | Enables validify (Validated , Modified , Validified , ValidifedByRef ) |
[validify ] |
❌ | ✅ | ✅ |
basic | Enables support for Query , Json and Form |
[query ], [json ], [form ] |
✅ | ✅ | ✅ |
json | Enables support for Json |
[json ] |
✅ | ✅ | ✅ |
query | Enables support for Query |
[query ] |
✅ | ✅ | ✅ |
form | Enables support for Form |
[form ] |
✅ | ✅ | ✅ |
typed_header | Enables support for TypedHeader from axum-extra |
[typed_header ] |
❌ | ✅ | ✅ |
typed_multipart | Enables support for TypedMultipart and BaseMultipart from axum_typed_multipart |
[typed_multipart ] |
❌ | ✅ | ✅ |
msgpack | Enables support for MsgPack and MsgPackRaw from axum-serde |
[msgpack ] |
❌ | ✅ | ✅ |
yaml | Enables support for Yaml from axum-serde |
[yaml ] |
❌ | ✅ | ✅ |
xml | Enables support for Xml from axum-serde |
[xml ] |
❌ | ✅ | ✅ |
toml | Enables support for Toml from axum-serde |
[toml ] |
❌ | ✅ | ✅ |
sonic | Enables support for Sonic from axum-serde |
[sonic ] |
❌ | ✅ | ✅ |
cbor | Enables support for Cbor from axum-serde |
[cbor ] |
❌ | ✅ | ✅ |
extra | Enables support for Cached , WithRejection from axum-extra |
[extra ] |
❌ | ✅ | ✅ |
extra_typed_path | Enables support for T: TypedPath from axum-extra |
[extra::typed_path ] |
❌ | ✅ | ✅ |
extra_query | Enables support for Query from axum-extra |
[extra::query ] |
❌ | ✅ | ✅ |
extra_form | Enables support for Form from axum-extra |
[extra::form ] |
❌ | ✅ | ✅ |
extra_protobuf | Enables support for Protobuf from axum-extra |
[extra::protobuf ] |
❌ | ✅ | ✅ |
all_extra_types | Enables support for all extractors above from axum-extra |
N/A | ❌ | ✅ | ✅ |
all_types | Enables support for all extractors above | N/A | ❌ | ✅ | ✅ |
422 | Use 422 Unprocessable Entity instead of 400 Bad Request as the status code when validation fails |
[VALIDATION_ERROR_STATUS ] |
❌ | ✅ | ✅ |
into_json | Validation errors will be serialized into JSON format and returned as the HTTP body | N/A | ❌ | ✅ | ✅ |
full_validator | Enables validator , all_types , 422 and into_json |
N/A | ❌ | ✅ | ✅ |
full_garde | Enables garde , all_types , 422 and into_json . Consider using default-features = false to exclude default validator support |
N/A | ❌ | ✅ | ✅ |
full_garde | Enables validify , all_types , 422 and into_json . Consider using default-features = false to exclude default validator support |
N/A | ❌ | ✅ | ✅ |
full | Enables all features above | N/A | ❌ | ✅ | ✅ |
aide | Enables support for aide |
N/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.