render icon indicating copy to clipboard operation
render copied to clipboard

is possible to apply 'dec.DisallowUnknownFields()' to render.Bind ?

Open angelmotta opened this issue 2 years ago • 2 comments

🙋🏻‍♂️ Hello! I am starting with chi for my personal project. Is there some way to return an error if the received json request has an additional unexpected field? Using the json.NewDecode(r.body) we can set dec.DisallowUnknownFields() and when we unmarshal the json object via Decode method we get that information error in the err value err := dec.Decode(&someStructType) I am seeing the chi examples but I don't find examples about it.

Thanks for your assistance!

angelmotta avatar Sep 26 '23 23:09 angelmotta

Hi,

You could override render.Decode function with your own.

Or, if #47 gets merged, you could override JSON decoder only.

Imho, for most REST API use cases, people don't want to be so strict on incoming JSON payload and error out on obsolete/deprecated field, which might be sent from an older version of a REST API client. So I'm not convinced we'll ever make it a feature. Or should we?

VojtechVitek avatar Sep 27 '23 09:09 VojtechVitek

@VojtechVitek thanks for your suggestion about render.Decode I will check that option and learn a little more along the way. Interesting discussion in PR #47 about this topic.

Good point about not being too strict on incoming JSON payload, I didn't realize that aspect, I was thinking It was healthier reject the request but that could break the client.

angelmotta avatar Oct 07 '23 03:10 angelmotta