在Gin Golang中的验证号码

huangapple go评论123阅读模式
英文:

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

  1. 当我用字符串填写整数类型的价格字段时

如果你的请求(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

  1. 你必须检查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

英文:
  1. 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

  1. You must check if err is an validator.ValidationErrors like 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

huangapple
  • 本文由 发表于 2022年8月26日 07:27:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/73494600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定