Skip to content

FastAPI does not handle ValidationError when updating #4

Open
@ecosys-technology

Description

@ecosys-technology

When using the PUT endpoint to update the Student data, if gpa is higher than 4.0, FastAPI returns a 500 Internal Server Error.

A quick fix is to add the validation directly to the UpdateStudentModel's gpa like this :

class UpdateStudentModel(BaseModel):
    name: Optional[str]
    email: Optional[EmailStr]
    course: Optional[str]
    gpa: Optional[float] = Path(..., le=4.0)

    class Config:
        arbitrary_types_allowed = True
        json_encoders = {ObjectId: str}
        schema_extra = {
            "example": {
                "name": "Jane Doe",
                "email": "[email protected]",
                "course": "Experiments, Science, and Fashion in Nanophotonics",
                "gpa": "3.0",
            }
        }

But this solution is a little bit redundant. Is there a better solution ?

This is relevant : https://fastapi.tiangolo.com/tutorial/handling-errors/#requestvalidationerror-vs-validationerror

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions