Skip to content
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

Enhance validation error feedback for endpoint requests #638

Open
wkargul opened this issue Aug 31, 2023 · 3 comments
Open

Enhance validation error feedback for endpoint requests #638

wkargul opened this issue Aug 31, 2023 · 3 comments
Labels
question Further information is requested

Comments

@wkargul
Copy link

wkargul commented Aug 31, 2023

Issue Description:

When sending a request with the structure defined as:

#[derive(Clone, Debug, Deserialize, Serialize, Object)]
pub struct CreateDTO {
    #[oai(validator(min_length = 1, max_length = 256))]
    pub name: String,
    #[oai(validator(min_length = 1, max_length = 512))]
    pub description: String,
}

which is used in the endpoint:

    #[oai(path = "/projects", method = "post", tag = "crate::ApiTags::Projects")]
    #[instrument(skip(self), fields(org_id = ? org_id.0, request = ? request.0))]
    pub async fn create_project(
        &self,
        #[oai(name = "X-OrganizationId")] org_id: Header<Uuid>,
        request: Json<CreateDTO>,
    ) -> CreateProjectResponse<ChangedDTO> {}

If there's an invalid name or description field in the request, Poem returns a 400 Bad Request error. However, the Response Body does not provide any indication or message about the specific field or reason causing the validation failure.

Would it be feasible to enhance the error feedback mechanism to return a descriptive error message when the validator encounters an issue? This would aid in better understanding the root cause of the error for API consumers.


@wkargul wkargul added the question Further information is requested label Aug 31, 2023
@sunli829
Copy link
Collaborator

sunli829 commented Sep 2, 2023

You can define a bad_request_handler to create a response for the error, refer to this example to implement it. 🙂

When the request parsing fails, it may be of these types.

@stevenj
Copy link

stevenj commented Sep 8, 2023

FastAPI returns a Json body that looks like this:

{
    "detail": [
        {
            "loc": [
                "path",
                "item_id"
            ],
            "msg": "value is not a valid integer",
            "type": "type_error.integer"
        }
    ]
}

I find this super handy because the response tells you all your validation errors in the request, not just the first one it hit.
It would be good if the bad_request_handler got a vec of validation errors rather than a single error. This would then allow one to produce a similar response.

@root-talis
Copy link

root-talis commented Oct 10, 2023

I'd love to have the ability to list all validation errors in a formalized manner. This is a future I widely use in other frameworks to tell the client exactly why the request is bad. Comes in handy in frontend - to paint invalid form fields red and add hints to them.

Maybe add a validation_errors_handler, similiar to bad_request_handler, that would accept a vec of validation errors and return a #[derive(Object)] struct?

Should I issue a separate feature request for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants