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

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

Open
angelmotta opened this issue Sep 26, 2023 · 2 comments
Open

Comments

@angelmotta
Copy link

πŸ™‹πŸ»β€β™‚οΈ 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!

@VojtechVitek
Copy link
Contributor

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?

@angelmotta
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants