-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
axum_garde
and State
#47
Comments
impl FromRef<AppState> for () {
fn from_ref(_: &AppState) -> Self {}
} Full example//! Simple usage of using `axum_garde` for a REST API
//!
//! Run the example using
//!
//! ```sh
//! cargo run --example json --features=json
//! ```
use axum::extract::FromRef;
use axum::response::IntoResponse;
use axum::routing::post;
use axum::{Json, Router, Server};
use axum_garde::WithValidation;
use garde::Validate;
use serde::{Deserialize, Serialize};
// Define your valid scheme
#[derive(Debug, Serialize, Deserialize, Validate)]
struct Person {
#[garde(ascii, length(min = 3, max = 25))]
username: String,
#[garde(length(min = 15))]
password: String,
}
async fn insert_valid_person(
State(state): State<AppState>,
// Perform validation on the request payload
WithValidation(person): WithValidation<Json<Person>>,
) -> impl IntoResponse {
println!("Inserted person on database: {person:?}");
Json(person.into_inner())
}
#[derive(Clone)]
struct AppState {}
impl FromRef<AppState> for () {
fn from_ref(_: &AppState) -> Self {}
}
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/person", post(insert_valid_person))
// Create the application state
.with_state(AppState {});
println!("See example: http://127.0.0.1:8080/person");
Server::bind(&([127, 0, 0, 1], 8080).into())
.serve(app.into_make_service())
.await
.unwrap();
} |
Thank you @jprochazk. With this example I was able to get my application up and running w/ |
To help demonstrate this issue I'll use the
json
example in this repository.I've been successful in using
garde
andaxum
together to pass state and validate parameters, so thank you! Your project is quite helpful. The way I've been successful is to useaxum
's olderExtension
method for passing state, see here:That seems to work just fine. That said, there is a relatively new feature in
axum
calledState
- https://tokio.rs/blog/2022-08-whats-new-in-axum-0-6-0-rc1. This is now the preferred way to pass state as it is type safe, whereasExtension
is not. The article also claims thatState
is faster thanExtension
.When I rework the example above to use
State
rather thanExtension
I get an error:This error message is currently beyond my ability to comprehend. If I remove the
garde
validation, the example works fine, so I guess there is some friction betweengarde
and this newish feature inaxum
that is causing the issue. Any help would be appreciated.The text was updated successfully, but these errors were encountered: