我正在学习并尝试使用 go-gin 框架创建一个简单的 api,
我试图创建一个用于验证的中间件,但是当我调用它时它说validations.signupValidator() is undifined
来自nodejs express的东西似乎有点不同,在此先感谢您的帮助
中间件
package validations
import (
"github.com/bihire/ikaze_server_app/entity"
"net/http"
"github.com/gin-gonic/gin"
"gopkg.in/validator.v2"
)
func signupValidator(ctx *gin.Context) {
var user entity.User
// if err := ctx.ShouldBindJSON(&user); err != nil {
// ctx.JSON(http.StatusBadRequest, gin.H{
// "error": err.Error(),
// })
// }
if err := validator.Validate(user); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
}
}
称它为
package routes
import (
"github.com/gin-gonic/gin"
"github.com/bihire/ikaze_server_app/service"
"github.com/bihire/ikaze_server_app/controller"
"github.com/bihire/ikaze_server_app/validations"
)
var (
userController controller.UserController = controller.New(UserService)
)
func Routes(route *gin.Engine) {
auth := route.Group("/api/auth")
{
auth.POST("login", gin.Logger(), validations.signupValidator(), func(ctx *gin.Context) {
ctx.JSON(200, userController.Save(ctx))
})
}
}
胡说叔叔
浮云间
相关分类