It not possible to DisallowUnknownFields() during echo.Context.Bind() #1760
Replies: 2 comments 2 replies
-
If you are only concerned about body (json) then you do not need to use echo bind at all. Using plain json.decoder has less side-effects if err := json.NewDecoder(c.Request().Body).Decode(&payload); err != nil {
return err
}
// or for DisallowUnknownFields() wrapped in your custom func
decoder := json.NewDecoder(c.Request().Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(&payload); err != nil {
return err
} If you mean returning error when request payload has fields that are not in bindable struct - you are making that endpoint more brittle. I personally would advice creating two separate structs for binding and passing to business logic and mapping them in handler. In that way you would not expose fields accidentally to binding See this and think about that |
Beta Was this translation helpful? Give feedback.
-
If you have to collect data not only from the request body, but path and/or query then you have to write too much code instead of using shiny bright e.Bind() |
Beta Was this translation helpful? Give feedback.
-
It will be useful to be able to setup a custom JSON decoder for the request body.
Beta Was this translation helpful? Give feedback.
All reactions