Closed
Description
I've noticed that encoding/json doesn't raise an error if any field is not present in the body payload.
Example:
type DecodeRequest struct{
ID int `json:"id"`
Name string `json:"name"`
}
func DecodeRequest(g *gin.Context) error{
var decodeRq DecodeRequest
dec := json.NewDecoder(g.Request.Body) //Body payload : {"id" : 123}
dec.DisallowUnknownFields()
err := dec.Decode(&decodeRq)
fmt.Println(err) -> nil
}
Ideally, this function should give an error like missingfield name
.
DisallowUnknownFields
just take care of extra fields but not the missing field.
Golang version: 1.15.6
Let me know if I've missed anything to fix it?