✨ Add support for enabling/disabling input and url fields in validation errors#13657
✨ Add support for enabling/disabling input and url fields in validation errors#13657kinuax wants to merge 14 commits intofastapi:masterfrom
input and url fields in validation errors#13657Conversation
input and `url fields in validation errorinput and url fields in validation error
input and url fields in validation errorinput and url fields in validation errors
…n-validation-errors
…n-validation-errors
…n-validation-errors
|
I added docs and tests. |
|
Hi @YuriiMotov, many thanks for checking this PR out. I share a couple of discussions about this topic: Regarding the |
|
will this ever be reviewed? @kinuax maybe a rebase and adding that missing 1% of coverage will help, no? |
There was a problem hiding this comment.
It's already possible to exclude input field from the error response by setting the exception handler for RequestValidationError. Adding url field is not trivial task, but I think this is very rare use case.
This PR would bring some convenience for quite rare cases when people need to change this behavior, but it adds 2 additional parameters to FastAPI().
I think, if we add all possible configuration options that people request as parameters, we will end up having very large list of parameters and this will add extra complexity for people to navigate through all these parameters to find the one they need. So, we should only add those that are commonly needed.
Looking at the number of reactions here and on mentioned discussion, I can't say this feature is requested by many people.
See also my previous comment: #13657 (comment)
Having this, I think we should close this PR. I'll pass this to Sebastián, he will review and take a decision.
@kinuax, thanks again for your efforts!
This comment was marked as duplicate.
This comment was marked as duplicate.
|
I understand, thanks for your quick response! What do you think about FastAPI dynamically using the attribute of the schema parameter defined within the pydantic model https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict.hide_input_in_errors, then we could make it transparent to FastAPI and then be completely configurable using pydantic schemas options. Would that make sense to have that easier UX? |
As I understand it, changes on Pydantic's side required to make it working: #10025 (comment) |
|
Thanks for opening the discussion and sharing your thoughts. 👍 I understand your points regarding complexity and usability. Additionally, I believe that toggling the behavior with flags offers a transparent way to transfer functionality from Pydantic to FastAPI, directly exposing its validation features. This approach enhances Pydantic's visibility and preserves a similar interface across both tools. From a developer experience perspective, it may be more appealing to learn and adjust some arguments when needed rather than having to tweak the exception handler. Regarding complexity and maintenance, here are a few considerations: |
|
I have simplified and extended the tests to validate the new feature and maintain 100% coverage. 😄 |
|
Thanks for the interests! I currently don't think this is necessary in FastAPI itself, I would imagine it's possible to customize it in the specific code base that wants to use it, so that would be a safe alternative. I also intend to make a refactor for how routers work, allowing people to customize them better, this should also allow implementing this on the user's own code more easily. Although still, the current alternative doesn't seem to be too complicated, so we might be good already. For now, I'll pass on this one, but thanks! ☕ |
|
@tiangolo, thanks for reviewing and explaining! 👍 |

Two new flags for
FastAPIandAPIRouterinstances are proposed:include_error_inputandinclude_error_url. This feature is supported only when using Pydantic v2. Both defaults (TrueandFalse, respectively) keep the current FastAPI behavior.For instance, we could have:
Therefore, requesting
/path1/invalid,/path2/, and/path2/?query_param=invalidwould produce errors with properinputandurlfields.