I'm using gin to handle requests, but I'm having trouble validating data in a request body, e.g.
type User struct {
Username string `json:"username" binding:"required,min=1,max=16"`
Name string `json:"name" binding:"required,min=1,max=16"`
Password string `json:"password" binding:"required,min=1,max=16"`
}
func loginHandler(ctx *gin.Context) {
var user User
if err := ctx.ShouldBindJSON(&user); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
}
How can I handle values with whitespaces like " username " or " John Doe "? As far as I know it's not possible to use regex in gin's validator.
What are the best practices or patterns for request body validation in Golang?