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

Request validation and http error aborting #933

Open
Ken-vdE opened this issue Oct 29, 2024 · 3 comments
Open

Request validation and http error aborting #933

Ken-vdE opened this issue Oct 29, 2024 · 3 comments
Labels
enhancement New feature or request
Milestone

Comments

@Ken-vdE
Copy link
Contributor

Ken-vdE commented Oct 29, 2024

I think I can keep this question short.

Maybe I'm missing something, but it seems there is no out-of-the box way to validate incoming requests.
The only thing I can find regarding validation is model-validation, but not request validation.

There is the JsonRejection error that's thrown (when the Json does not comply with the struct used in the controller) which is returned to the user as 400 "Bad Request".
It feels like src/controller/mod.rs#L185 is missing a 422 ValidationError case as well as a few other http errors.

Am I missing something? Is there a way to validate request data and is there a way to abort into other http errors from the controller (like 403 forbidden or even just a 201 no content)?

@Ken-vdE Ken-vdE added the enhancement New feature or request label Oct 29, 2024
@ABruel
Copy link
Contributor

ABruel commented Nov 3, 2024

It seems CustomError would be the current way of doing it.

@Ken-vdE
Copy link
Contributor Author

Ken-vdE commented Nov 3, 2024

So, I've been playing around with this but it seems the error system leaves a bit to be desired.
I'm willing to put some time and effort into developing the following:

  1. Make it possible to throw a CustomError containing Json instead of a plain string and make it so a CustomError can be thrown a bit more easily.
  2. I'd like to add a ValidationError that wraps the ValidationErrors from the validator crate, which when thrown, is returned to the user as a 422 error with the corresponding json as the content.
  3. I want to add a simple Valid type that you can use to wrap Json<SomeStruct>, which when used makes sure your struct is validated using the validator crate before the controller function is called. If the data is invalid, the above ValidationError is thrown and the controller function is not called. Using the type will be optional, you can manually call validate() and convert the error using 2.

Now, before I put in the time to do this, I'd like to make sure you're interested.
If you're interested, give me a go-ahead and I'll get started.

@jondot jondot added this to the 0.13.1 milestone Nov 20, 2024
@jondot
Copy link
Contributor

jondot commented Nov 24, 2024

@Ken-vdE thanks for this! I went over the thread and reasoning -- it looks solid. please feel free to go ahead and submit a PR for us to go over

@jondot jondot modified the milestones: 0.13.1, 0.14.0 Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants