尝试学习 golang,我迷失在验证中间件中使用context.Request.Body它struct
简要介绍它们如何相互连接,在此先感谢您的帮助
我的中间件
package validations
import (
"github.com/bihire/ikaze_server_app/entity"
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
)
func SignupValidator(c *gin.Context) {
// user := c.Request.Body
var user entity.User
validate := validator.New()
if err := validate.Struct(&user); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
}
}
我的结构
package entity
type User struct {
Username string `json:"username" validate:"required"`
Email string `json:"email" validate:"email"`
Password string `json:"password" validate:"min=8,max=32,alphanum"`
ConfirmPassword string `json:"confirm_password" validate:"eqfield=Password,required"`
}
返回响应错误
{
"error": "Key: 'User.Username' Error:Field validation for 'Username' failed on the 'required' tag\nKey: 'User.Email' Error:Field validation for 'Email' failed on the 'email' tag\nKey: 'User.Password' Error:Field validation for 'Password' failed on the 'min' tag\nKey: 'User.ConfirmPassword' Error:Field validation for 'ConfirmPassword' failed on the 'required' tag"
}{
"username": "bihire",
"email": "hgh@gmail.com",
"password": "password",
"confirm_password": "password"
}
带中间件的路由器
auth.POST("login", gin.Logger(), validations.SignupValidator, func(ctx *gin.Context) {
ctx.JSON(200, videoController.Save(ctx))
})
Qyouu
相关分类