How can I tell if something was submitted as zero value or omitted from the request body? #2466
Unanswered
chrislentz
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am currently writing a PATCH endpoint with Go Echo using Go Playground for validation. I use the method below to bind my JSON request body to a struct. The issue is that this is a patch endpoint, so properties are optional in the request body JSON. How can I determine if the request body submitted a property as its zero value or was omitted? Because when I look at the struct, I get the zero value even if the field was omitted from the JSON request body.
For example, let's say I have a property called "count." This is a PATCH endpoint, so the count could be omitted completely or submitted with an integer. If I use Go Echo's bind capabilities, the bound struct will show the "count = 0" (the zero value equivalent) if it is omitted. But it wasn't submitted, so I should leave the value untouched and not update it to "0". If the request contains "count: 0", I should update it.
How would I know if "count" was submitted? I hope I don't have to compare every property to the existing database values to see if it changed before saving.
Beta Was this translation helpful? Give feedback.
All reactions