Gin: c.Bind with int 0 cause error?

Created on 7 Feb 2018  路  2Comments  路  Source: gin-gonic/gin

type HcStatus struct {
    Name    string `json:"name" binding:"required"`
    IsAlert int    `json:"isalert" binding:"required"`
}

func UpdateStatus(c *gin.Context) {
    var inputs HcStatus
    err := c.Bind(&inputs)
    fmt.Println("error:====>",err)
}

when the IsAlert bigger than 0, it will be correct. But the alert is 0, it will be Key: 'HcStatus.IsAlert' Error:Field validation for 'IsAlert' failed on the 'required' tag.
sorry,I am a newbie. How can I resolve it?

All 2 comments

The message says the reason itself. You specified the "required" constraint to the param IsAlert, and it means IsAlert must have values except the zero value -- 0 for int.

If you want to permit 0 for IsAlert, you should not use "required" for IsAlert.

@delphinus ok,thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lilee picture lilee  路  3Comments

olegsobchuk picture olegsobchuk  路  3Comments

frederikhors picture frederikhors  路  3Comments

sofish picture sofish  路  3Comments

wangcn picture wangcn  路  3Comments