Repository navigation
Incorrect format of error response from rest apis #1543
Replies: 7 comments 6 replies
|
Check request validation here: https://loco.rs/docs/the-app/controller/ |
|
Hey @saurabh-git-dev, Regarding the missing field validation, you can handle it by using the #[derive(Debug, Deserialize, Validate)]
pub struct DataParams {
#[validate(length(min = 5, message = "custom message"))]
pub first_name: String,
#[validate(required)]
pub last_name: Option<String>,
#[validate(email)]
pub email: String,
}
#[debug_handler]
pub async fn index(
State(_ctx): State<AppContext>,
JsonValidateWithMessage(params): JsonValidateWithMessage<DataParams>,
) -> Result<Response> {
format::text("ok")
}CURL request: ❯ curl -X POST http://localhost:5150/api/tests \
-H "Content-Type: application/json" \
-d '{"first_name": "test", "email": "john@example.com"}' | jq .Response: {
"errors": {
"first_name": [
{
"code": "length",
"message": "custom message",
"params": {
"min": 5,
"value": "test"
}
}
],
"last_name": [
{
"code": "required",
"message": null,
"params": {
"value": null
}
}
]
}
}Note: As for query param validation, we added support for that in version 0.16.2 (thanks to @Davidzhu001!). You can now use |
|
@Davidzhu001! had implemented
|
|
no. only query parameters. |
|
I conducted a test and discovered some cases where this validator is not particularly useful. For example:
#[derive(Debug, Validate, Deserialize)]
pub struct TestPathParams {
#[validate(range(min = 1, max = 10))]
id: i32,
}
#[derive(Debug, Validate, Deserialize)]
pub struct TestQueryParams {
#[validate(range(min = 1, max = 10))]
page: i32,
#[validate(range(min = 1, max = 100))]
size: i32,
}
pub async fn path_validate(
PathValidateWithMessage(params): PathValidateWithMessage<TestPathParams>,
QueryValidateWithMessage(payload): QueryValidateWithMessage<TestQueryParams>,
) -> Result<Response> {
println!("Path ID: {}", params.id);
println!("Query Data: {:?}", payload);
format::json(params.id.to_string())
}
/// routes
.add("/records/{id}", get(path_validate))You are required to make requests like payload: Result<QueryValidate<PagyParams>>,to handle defaults. I found that there are numerous configurations required for this use case. I then tested other errors and discovered that you can still add them back to use the validator. Currently, I am investigating ways to implement a better request validator, so I have not addressed this issue extensively. |
|
I am lazy developer, I believe that if there is no elegant way to accomplish a task, a better approach is necessary. Therefore, I am planning to develop an improved request validator. |
|
@Davidzhu001 |

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I believe the rest api should return an error as JSON by default instead of a string when we pass an invalid parameter in the request.
See the attached image.
I am also looking solution for customizing the errors.. like validating the body. and returning with specific error messages, and missing fields errors.
All reactions