- Enforce axum best practices across all 13 workspace crates (max 200 LOC/file, no comments, no unwrap, clean architecture) - Fix domain→infrastructure dependency inversions in imphnen-iam and imphnen-dimentorin - Extract imphnen-storage (MinIO) and imphnen-email (Lettre) as standalone crates - Centralize all config in ENV struct: CDN_URL, CORS_ALLOWED_ORIGINS - Centralize SMTP through imphnen-email; remove dead HackathonConfig - Centralize database: QR crate now shares main DB pool (single DATABASE_URL) - Rename QR users table to qr_users to avoid collision with main users table - Merge imphnen-qr into imphnen-cms/src/qr (13 crates, down from 14) - Restructure imphnen-hackathon flat modules into clean architecture - Remove all stale env vars from .env.example (SurrealDB, QR_JWT, Hackathon infra) - Fix Dockerfile to include all current workspace crates - Bump all crate versions 0.2.0 → 0.3.0 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
59 lines
1.2 KiB
Rust
59 lines
1.2 KiB
Rust
use axum::{
|
|
Json,
|
|
body::Bytes,
|
|
extract::{FromRequest, Request},
|
|
http::StatusCode,
|
|
response::{IntoResponse, Response},
|
|
};
|
|
use serde_json::json;
|
|
|
|
use super::zod_validate::ZodValidate;
|
|
|
|
pub struct ValidatedJson<T>(pub T);
|
|
|
|
impl<T, S> FromRequest<S> for ValidatedJson<T>
|
|
where
|
|
T: ZodValidate + 'static,
|
|
S: Send + Sync,
|
|
{
|
|
type Rejection = Response;
|
|
|
|
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
|
let bytes = Bytes::from_request(req, state).await.map_err(|e| {
|
|
(
|
|
StatusCode::BAD_REQUEST,
|
|
Json(json!({
|
|
"message": format!("Failed to read body: {e}"),
|
|
"version": env!("CARGO_PKG_VERSION"),
|
|
})),
|
|
)
|
|
.into_response()
|
|
})?;
|
|
|
|
let json_value: serde_json::Value =
|
|
serde_json::from_slice(&bytes).map_err(|e| {
|
|
(
|
|
StatusCode::BAD_REQUEST,
|
|
Json(json!({
|
|
"message": format!("Invalid JSON: {e}"),
|
|
"version": env!("CARGO_PKG_VERSION"),
|
|
})),
|
|
)
|
|
.into_response()
|
|
})?;
|
|
|
|
let value = T::zod_validate(&json_value).map_err(|e| {
|
|
(
|
|
StatusCode::BAD_REQUEST,
|
|
Json(json!({
|
|
"message": format!("Validation error: {e}"),
|
|
"version": env!("CARGO_PKG_VERSION"),
|
|
})),
|
|
)
|
|
.into_response()
|
|
})?;
|
|
|
|
Ok(ValidatedJson(value))
|
|
}
|
|
}
|