Replies: 4 comments
-
Why not do the validation in the endpoint or a service call? Why does the validator need to be done in the request parameters? For the 1% of the time validation needs to be more complex, why not handle it with python in the request function rather than add complexity to FastAPI? |
Beta Was this translation helpful? Give feedback.
-
Yes, this would be possible. I‘ve opened #471 for this. The use case for both issues is, that it is currently hard to perform additonal validation and return an error that is formatted like the ones returned by the built-in validation. If #471 will be implemented, this issue can be closed. :) |
Beta Was this translation helpful? Give feedback.
-
Thanks for your help @wshayes ! @sscherfke a simpler way to achieve this would be with a dependency. The dependency could require the path parameter and error out if your custom validation fails. And then your path operations could use that dependency instead of doing all the validation in the main code. Another option that could be closer to what your want to achieve is a custom Pydantic data type that inherits from a https://pydantic-docs.helpmanual.io/#custom-data-types That's probably what you're looking for. |
Beta Was this translation helpful? Give feedback.
-
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues. |
Beta Was this translation helpful? Give feedback.
-
Is your feature request related to a problem? Please describe.
Path parameters can currently only be validated using a regular expression, which may not always be sufficient.
Describe the solution you'd like
It would be nice if I could provide a validator function for the
Path
instance that can perform additional checks (and transformation) or raise an error:Describe alternatives you've considered
Validate inside the function and manually raise an error, see #471 .
Beta Was this translation helpful? Give feedback.
All reactions