Within the following code I want to allow age
to be a number or a string, for example 33 or “33”, but it fails when it’s a string. How can I accept it? I see process fails before starting the add
function. I am using Rocket framework.
<code>#[post("/add", data = "<user_json>")]
#[instrument(name = "user_controller/add", skip_all)]
async fn add(
app: &AppState,
mut db: ConnectionDb,
user_json: Json<UserJson>,
) -> Result<Json<User>, AppError> {
tracing::info!("==Stating add...");
let inner = user_json.into_inner();
tracing::info!("Request to add user. Name: {}, Age: {}", inner.name, inner.age);
let name = inner.name;
let age = inner.age;
if age < 18 {
return Err(AppError::new(400, "not yet 😼!"));
}
let user = app
.use_cases
.user
.create(&app.repos, &mut db, &name, age)
.await?;
Ok(Json(user))
}
</code>
<code>#[post("/add", data = "<user_json>")]
#[instrument(name = "user_controller/add", skip_all)]
async fn add(
app: &AppState,
mut db: ConnectionDb,
user_json: Json<UserJson>,
) -> Result<Json<User>, AppError> {
tracing::info!("==Stating add...");
let inner = user_json.into_inner();
tracing::info!("Request to add user. Name: {}, Age: {}", inner.name, inner.age);
let name = inner.name;
let age = inner.age;
if age < 18 {
return Err(AppError::new(400, "not yet 😼!"));
}
let user = app
.use_cases
.user
.create(&app.repos, &mut db, &name, age)
.await?;
Ok(Json(user))
}
</code>
#[post("/add", data = "<user_json>")]
#[instrument(name = "user_controller/add", skip_all)]
async fn add(
app: &AppState,
mut db: ConnectionDb,
user_json: Json<UserJson>,
) -> Result<Json<User>, AppError> {
tracing::info!("==Stating add...");
let inner = user_json.into_inner();
tracing::info!("Request to add user. Name: {}, Age: {}", inner.name, inner.age);
let name = inner.name;
let age = inner.age;
if age < 18 {
return Err(AppError::new(400, "not yet 😼!"));
}
let user = app
.use_cases
.user
.create(&app.repos, &mut db, &name, age)
.await?;
Ok(Json(user))
}
- userJson.rs
<code>use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, FromForm, Debug, Clone)]
pub struct UserJson {
pub name: String,
pub age: i32,
}
</code>
<code>use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, FromForm, Debug, Clone)]
pub struct UserJson {
pub name: String,
pub age: i32,
}
</code>
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, FromForm, Debug, Clone)]
pub struct UserJson {
pub name: String,
pub age: i32,
}
- user.rs
<code>use serde::{Deserialize, Serialize};
use sqlx::FromRow;
#[derive(Debug, FromRow, Serialize, Deserialize)]
pub struct User {
pub id: i32,
pub name: String,
pub age: Option<i32>,
}
</code>
<code>use serde::{Deserialize, Serialize};
use sqlx::FromRow;
#[derive(Debug, FromRow, Serialize, Deserialize)]
pub struct User {
pub id: i32,
pub name: String,
pub age: Option<i32>,
}
</code>
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
#[derive(Debug, FromRow, Serialize, Deserialize)]
pub struct User {
pub id: i32,
pub name: String,
pub age: Option<i32>,
}