英文:
Validation Number in Gin Golang
问题
在gin中验证数字时出现了错误,对于字符串数据类型没有错误并且可以检测到,但是当我用字符串填充int类型的价格字段时,它会出现500错误,我希望错误是400,有什么解决办法吗?
package web
type BookRequest struct{
    Title string `json:"title" binding:"required"`
    Description string `json:"description" binding:"required"`
    Price int `json:"price" binding:"required,numeric,gte=0"`
    Rating int `json:"rating" binding:"required,numeric"`
}
func (controller *BookControllerImpl) Create(ctx *gin.Context) {
    var bookRequest web.BookRequest
    err := ctx.ShouldBindJSON(&bookRequest)
    if err != nil {
        var error_request []string
        for _, e := range err.(validator.ValidationErrors) {
            errorMessage := fmt.Sprintf("Error field %s, Condition %s", e.Field(), e.ActualTag())
            error_request = append(error_request, errorMessage)
        }
        ctx.JSON(http.StatusBadRequest, web.WebResponse{
            Code:   http.StatusBadRequest,
            Status: "BAD REQUEST",
            Data:   error_request,
        })
        return
    }
    book, err := controller.BookService.Create(bookRequest)
    if err != nil {
        ctx.JSON(http.StatusBadRequest, web.WebResponse{
            Code:   http.StatusBadRequest,
            Status: "BAD REQUEST",
            Data:   err,
        })
    }
    ctx.JSON(http.StatusOK, web.WebResponse{
        Code:   200,
        Status: "Ok",
        Data:   book,
    })
}
英文:
I have an error when validating the number in gin, for the string data type there are no errors and they are detected, but when I fill in the price field of type int with a string, it gets an error of 500, I expect the error to be 400, what is the solution?
package web
type BookRequest struct{
    Title string `json:"title" binding:"required"`
    Description string `json:"description" binding:"required"`
    Price int `json:"price" binding:"required,numeric,gte=0"`
    Rating int `json:"rating" binding:"required,numeric"`
}
func (controller *BookControllerImpl) Create(ctx *gin.Context) {
    var bookRequest web.BookRequest
    err := ctx.ShouldBindJSON(&bookRequest)
    if err != nil {
        var error_request []string
        for _, e := range err.(validator.ValidationErrors) {
            errorMessage := fmt.Sprintf("Error field %s, Condition %s", e.Field(), e.ActualTag())
            error_request = append(error_request, errorMessage)
        }
        ctx.JSON(http.StatusBadRequest, web.WebResponse{
            Code:   http.StatusBadRequest,
            Status: "BAD REQUEST",
            Data:   error_request,
        })
        return
    }
    book, err := controller.BookService.Create(bookRequest)
    if err != nil {
        ctx.JSON(http.StatusBadRequest, web.WebResponse{
            Code:   http.StatusBadRequest,
            Status: "BAD REQUEST",
            Data:   err,
        })
    }
    ctx.JSON(http.StatusOK, web.WebResponse{
        Code:   200,
        Status: "Ok",
        Data:   book,
    })
}
答案1
得分: 2
- 当我用字符串填写整数类型的价格字段时
 
如果你的请求(payload Price)不是标准的或者可以是字符串或数字类型,你可以在你的结构体中使用json.Number类型。
type BookRequest struct{
    Title string `json:"title" binding:"required"`
    Description string `json:"description" binding:"required"`
    Price json.Number `json:"price" binding:"required,numeric,gte=0"`
    Rating int `json:"rating" binding:"required,numeric"`
}
这是一个关于json.Number的简单示例:https://go.dev/play/p/7fyCFAon2PC
- 你必须检查
err是否为validator.ValidationErrors,像这样: 
...
err := ctx.ShouldBindJSON(&bookRequest)
if err != nil {
    if vals, ok := err.(validator.ValidationErrors); ok {
        // 使用vals进行循环处理
        ...
        ctx.JSON(http.StatusBadRequest, web.WebResponse{
            Code:   http.StatusBadRequest,
            Status: "BAD REQUEST",
            Data:   error_request,
        })
        return
    }
    ctx.JSON(http.StatusInternalServerError, web.WebResponse{
        Code:   http.StatusInternalServerError,
        Status: "Internal Server Error",
        Data:   err,
    })
    return
}
...
你可以实现json.Unmarshaller来完成这个操作。
这是一个示例:
func (br *BookRequest) UnmarshalJSON(b []byte) error {
	// TODO: remove binding tag
	type helperBookRequest struct {
		Title       string `json:"title" binding:"required"`
		Description string `json:"description" binding:"required"`
		Price       any    `json:"price" binding:"required,numeric,gte=0"` // 注意这里的类型是any或interface{}
		Rating      int    `json:"rating" binding:"required,numeric"`
	}
	var hbr helperBookRequest
	err := json.Unmarshal(b, &hbr)
	if err != nil {
		return err
	}
	br.Title = hbr.Title
	br.Description = hbr.Description
	br.Rating = hbr.Rating
	switch hbr.Price.(type) {
	case float64:
		br.Price = strconv.Itoa(int(hbr.Price.(float64)))
	case string:
		br.Price = hbr.Price.(string)
	default:
		return errors.New("invalid type")
	}
	return nil
}
在playground中的简单示例:https://go.dev/play/p/tmnKW4peBgp
英文:
when I fill in the price field of type int with a string
If your request (payload Price) is not standart or it can be string or number, you can use json.Number type for your struct.
type BookRequest struct{
    Title string `json:"title" binding:"required"`
    Description string `json:"description" binding:"required"`
    Price json.Number `json:"price" binding:"required,numeric,gte=0"`
    Rating int `json:"rating" binding:"required,numeric"`
}
this is a simple example for json.Number : https://go.dev/play/p/7fyCFAon2PC
- You must check if err is an 
validator.ValidationErrorslike this: 
...
err := ctx.ShouldBindJSON(&bookRequest)
if err != nil {
    if vals, ok := err.(validator.ValidationErrors); ok {
        // do for loop from vals
        ...
        ctx.JSON(http.StatusBadRequest, web.WebResponse{
            Code:   http.StatusBadRequest,
            Status: "BAD REQUEST",
            Data:   error_request,
        })
        return
    }
    ctx.JSON(http.StatusInternalServerError, web.WebResponse{
        Code:   http.StatusInternalServerError,
        Status: "Internal Server Error",
        Data:   err,
    })
    return
}
...
========== Answer 2 ==========
You can implement json.Unmarshaller to do it
here's the example:
func (br *BookRequest) UnmarshalJSON(b []byte) error {
	// TODO: remove binding tag
	type helperBookRequest struct {
		Title       string `json:"title" binding:"required"`
		Description string `json:"description" binding:"required"`
		Price       any    `json:"price" binding:"required,numeric,gte=0"` // look at this type is any or interface{}
		Rating      int    `json:"rating" binding:"required,numeric"`
	}
	var hbr helperBookRequest
	err := json.Unmarshal(b, &hbr)
	if err != nil {
		return err
	}
	br.Title = hbr.Title
	br.Description = hbr.Description
	br.Rating = hbr.Rating
	switch hbr.Price.(type) {
	case float64:
		br.Price = strconv.Itoa(int(hbr.Price.(float64)))
	case string:
		br.Price = hbr.Price.(string)
	default:
		return errors.New("invalid type")
	}
	return nil
}
simple example in playground : https://go.dev/play/p/tmnKW4peBgp
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论