Is this the correct approach to accept dynamic fields in Gin?
It is a way of handling JSON objects with unknown names, but not necessarily the correct way. For example, if the know the the object's values all map to Go type T
, then you should use var data map[string]T
or var data map[string]*T
.
Are there any limitations or best practices I should be aware of when binding to a
map[string]interface{}
?
The limitation is that you must access the map values using type assertions or reflection. This can be tedious.
How can I validate fields or types if I don’t know the keys in advance?
If you know that the object's values correspond to some type Go type T
, then see part one of this answer.
If you don't know the object's names or the type of the object's values, then you have no information to validate.