Open Ken-vdE opened 2 weeks ago
It seems CustomError
would be the current way of doing it.
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:
ValidationErrors
from the validator crate, which when thrown, is returned to the user as a 422 error with the corresponding json as the content.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.
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)?