validator库参数校验若干实用技巧

1. 表单的基本验证

若要将请求主体绑定到结构体中,请使用模型绑定,目前支持JSON、XML、YAML和标准表单值(foo=bar&boo=baz)的绑定。
Gin使用 go-playground/validator 验证参数,查看完整文档

需要在绑定的字段上设置tag,比如,绑定格式为json,需要这样设置 json:”fieldname” 。
此外,Gin还提供了两套绑定方法:

  • Must bind
  • Methods - Bind, BindJSON, BindXML, BindQuery, BindYAML
  • Behavior - 这些方法底层使用 MustBindWith,如果存在绑定错误,请求将被以下指令中止 c.AbortWithError(400, err).SetType(ErrorTypeBind),响应状态代码会被设置为400,请求头Content-Type被设置为text/plain; charset=utf-8。注意,如果你试图在此之后设置响应代码,将会发出一个警告 [GIN-debug] [WARNING] Headers were already written. Wanted to override status code 400 with 422,如果你希望更好地控制行为,请使用ShouldBind相关的方法
  • Should bind
  • Methods - ShouldBind, ShouldBindJSON, ShouldBindXML, ShouldBindQuery, ShouldBindYAML
  • Behavior - 这些方法底层使用 ShouldBindWith,如果存在绑定错误,则返回错误,开发人员可以正确处理请求和错误。

当我们使用绑定方法时,Gin会根据Content-Type推断出使用哪种绑定器,如果你确定你绑定的是什么,你可以使用MustBindWith或者BindingWith。
你还可以给字段指定特定规则的修饰符,如果一个字段用binding:”required”修饰,并且在绑定时该字段的值为空,那么将返回一个错误。

  1. // 绑定为json
  2. type Login struct {
  3. User string `form:"user" json:"user" xml:"user" binding:"required"`
  4. Password string `form:"password" json:"password" xml:"password" binding:"required"`
  5. }
  6. type SignUpParam struct {
  7. Age uint8 `json:"age" binding:"gte=1,lte=130"`
  8. Name string `json:"name" binding:"required"`
  9. Email string `json:"email" binding:"required,email"`
  10. Password string `json:"password" binding:"required"`
  11. RePassword string `json:"re_password" binding:"required,eqfield=Password"`
  12. }
  13. func main() {
  14. router := gin.Default()
  15. // Example for binding JSON ({"user": "manu", "password": "123"})
  16. router.POST("/loginJSON", func(c *gin.Context) {
  17. var json Login
  18. if err := c.ShouldBindJSON(&json); err != nil {
  19. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  20. return
  21. }
  22. if json.User != "manu" || json.Password != "123" {
  23. c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
  24. return
  25. }
  26. c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
  27. })
  28. // Example for binding a HTML form (user=manu&password=123)
  29. router.POST("/loginForm", func(c *gin.Context) {
  30. var form Login
  31. // This will infer what binder to use depending on the content-type header.
  32. if err := c.ShouldBind(&form); err != nil {
  33. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  34. return
  35. }
  36. if form.User != "manu" || form.Password != "123" {
  37. c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
  38. return
  39. }
  40. c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
  41. })
  42. r.POST("/signup", func(c *gin.Context) {
  43. var u SignUpParam
  44. if err := c.ShouldBind(&u); err != nil {
  45. c.JSON(http.StatusOK, gin.H{
  46. "msg": err.Error(),
  47. })
  48. return
  49. }
  50. // 保存入库等业务逻辑代码...
  51. c.JSON(http.StatusOK, "success")
  52. })
  53. // Listen and serve on 0.0.0.0:8080
  54. router.Run(":8080")
  55. }

2. 错误翻译

3. 进一步改进校验方法

上面的错误提示看起来是可以了,但是还是差点意思,首先是错误提示中的字段并不是请求中使用的字段,例如:RePassword是我们后端定义的结构体中的字段名,而请求中使用的是re_password字段。如何是错误提示中的字段使用自定义的名称,例如jsontag指定的值呢?
只需要在初始化翻译器的时候像下面一样添加一个获取json tag的自定义方法即可。

但是还是有点瑕疵,那就是最终的错误提示信息中心还是有我们后端定义的结构体名称——SignUpParam,这个名称其实是不需要随错误提示返回给前端的,前端并不需要这个值。我们需要想办法把它去掉。
定义一个去掉结构体名称前缀的自定义方法: