Skip to main content

Crate scrutiny_axum

Crate scrutiny_axum 

Source
Expand description

Axum integration for the scrutiny crate.

Provides extractors that deserialize and validate request data in one step. Deserialization errors are reported as field-level validation errors — not generic error strings. This means typed fields like uuid::Uuid or chrono::NaiveDate produce the same structured error format as validation failures.

§Extractors

ExtractorWrapsDescription
Valid<T>Json<T>JSON body with validation
ValidWith<T, E>Json<T>JSON body with custom error response
ValidForm<T>Form<T>Form-encoded body with validation
ValidQuery<T>axum-extra’s Query<T>Query parameters with validation (uses serde_html_form)

§Quick Start

ⓘ
use scrutiny::Validate;
use scrutiny_axum::Valid;
use axum::{Router, routing::post, response::IntoResponse, Json};
use serde::Deserialize;

#[derive(Validate, Deserialize)]
struct CreateUser {
    #[validate(required, min = 2)]
    name: Option<String>,
    id: uuid::Uuid,  // deserialization error → field-level validation error
}

async fn create_user(Valid(user): Valid<CreateUser>) -> impl IntoResponse {
    Json(serde_json::json!({ "name": user.name }))
}

let app = Router::new().route("/users", post(create_user));

Both validation failures and deserialization errors produce the same format:

{
  "message": "The given data was invalid.",
  "errors": {
    "name": ["The name field is required."],
    "id": ["invalid type: string \"not-a-uuid\", expected UUID"]
  }
}

§Replaces WithRejection from axum-extra

These extractors are standalone replacements for the common WithRejection<Json<T>, AppError> pattern from axum-extra. You don’t need axum-extra or WithRejection — our extractors handle deserialization, validation, and error responses in one step.

ⓘ
// Before (axum-extra):
use axum_extra::extract::WithRejection;
async fn handler(
    WithRejection(Json(body), _): WithRejection<Json<CreateUser>, AppError>,
) -> Result<impl IntoResponse> { ... }

// After (scrutiny-axum):
use scrutiny_axum::Valid;
async fn handler(Valid(body): Valid<CreateUser>) -> impl IntoResponse { ... }

// With custom error format (replaces AppError):
use scrutiny_axum::ValidWith;
async fn handler(r: ValidWith<CreateUser, MyApiError>) -> impl IntoResponse { ... }

§Custom Error Responses

Implement ValidationErrorResponse to control the HTTP response format:

ⓘ
use scrutiny_axum::{ValidWith, ValidationErrorResponse};

struct MyApiError;
impl ValidationErrorResponse for MyApiError {
    fn from_validation_errors(errors: ValidationErrors) -> Response { /* ... */ }
}

async fn handler(result: ValidWith<CreateUser, MyApiError>) -> impl IntoResponse {
    let user = result.into_inner();
    // ...
}

Structs§

DefaultErrorResponse
Default error response: Laravel-compatible 422 JSON.
Valid
Extracts and validates JSON body. Drop-in replacement for axum::Json<T>.
ValidForm
Extracts and validates form-encoded body.
ValidQuery
Extracts and validates query parameters.
ValidWith
Extracts and validates JSON body with a custom error response type.

Traits§

ValidationErrorResponse
Trait for customizing how validation errors become HTTP responses. Implement this to use your own error format with ValidWith<T, E>.