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

Raise classical Pydantic ValidationError like FastApi #71

Open
Merinorus opened this issue Apr 20, 2023 · 1 comment
Open

Raise classical Pydantic ValidationError like FastApi #71

Merinorus opened this issue Apr 20, 2023 · 1 comment

Comments

@Merinorus
Copy link
Contributor

Merinorus commented Apr 20, 2023

Hello,

I'm working with this library and I found the option to raise errors (FLASK_PYDANTIC_VALIDATION_ERROR_RAISE = True).

I was expecting the same kind of error as in FastAPI/Pydantic combination:

{
   "errors":[
      {
         "loc":[
            "query",
            "request-mode"
         ],
         "msg":"field required",
         "type":"value_error.missing"
      },
      {
         "loc":[
            "body",
            "birth_date"
         ],
         "msg":"field required",
         "type":"value_error.missing"
      }
   ]
}

In Pydantic, all errors are in the errors array and the location (header, body...) is specified directly in "loc".

In Flask-Pydantic, errors are in separate folders according to the location:

{
   "body":[
      {
         "loc":[
            "birth_date"
         ],
         "msg":"field required",
         "type":"value_error.missing"
      }
   ],
   "query":[
      {
         "loc":[
            "request-mode"
         ],
         "msg":"field required",
         "type":"value_error.missing"
      }
   ]
}

The ValidationError(BaseFlaskPydanticException) exception e is raised and you can look for each group errors according to the location:

  • e.body_params
  • e.form_params
  • e.path_params
  • e.query_params

What I would like is, for instance, to add the e.errors category which contains all the errors, formatted as in the Pydantic library used by FastAPI.

Thank you!

Merinorus added a commit to Merinorus/flask-pydantic that referenced this issue Apr 20, 2023
Add "errors" field like in the original Pydantic library.
Allow errors to be in the same place in addition to having them
separated.

Implements bauerji#71
@Merinorus
Copy link
Contributor Author

I made a "draft" pull request.
Let me know if it's unclear, not useful, or not relevant at all.
If you accept this, I can improve it by adding tests.
I didn't measure the performance impact, maybe I could make a condition for this error generation (for instance, a flag in the config).

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

No branches or pull requests

1 participant