Skip to content

proposal: encoding/json: require all fields #44138

Closed
@jaykam

Description

@jaykam

Hi @rsc @adg

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions