🤗 [Question]: Validation with body and struct
redat00 opened this issue · 3 comments
Question Description
Hi everyone !
I'm currently working on a new API and I want to use Fiber to do so.
There is one point where I'm struggling a little bit : Validation from body to struct.
On the official documentation, there is an example on how to do validation, but it rely on the idea that you would use query parameters, rather than body. However in my case I want to validate data coming from a body..
I'm using the ctx.Bind().Body(&struct)
syntax to do so currently. But the thing is that if it doesn't bind, I'm return the error, with the following syntax.
err := ctx.Bind().Body(&struct)
if err != nil {
return err
}
How would someone use validation with the Bind()
method ?
I'm starting to think that I might have to create my own BindAndValidate
function, but maybe there is another solution that I'm missing.
Thanks in advance for any help !
Code Snippet (optional)
No response
Checklist:
- I agree to follow Fiber's Code of Conduct.
- I have checked for existing issues that describe my questions prior to opening this one.
- I understand that improperly formatted questions may be closed without explanation.
Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord
“Bind” means you are using our v3 version which is not yet fully stable
here is an example of how you can do this with it
https://docs.gofiber.io/next/api/bind#validation
in v2 I would follow the validation guide you already mentioned, there it doesn't matter where the data comes from
if you want to use the body there, use the bodyParser instead of the queryParser with the corresponding anotations if necessary
Hi !
Didn't even realise I was using the beta version, going through the documentation it seems like it's a really great new version with a lot of cool features, so thanks for the work !
Regarding my issue I was in fact able to do this really easily by adding a validator. Now everything's fine, I just need to work on some small issue.
Thanks a lot for your answer !