gpt4 book ai didi

go - 无法使用validator.v2 golang进行验证

转载 作者:行者123 更新时间:2023-12-01 22:04:31 26 4
gpt4 key购买 nike

我在用gin验证程序验证字段时遇到麻烦。
我每次在struct上添加验证时都会返回的错误
我在这里做错了什么
图式

package entity

type User struct {
Username string `json:"username" binding:"min=2,required"`
Email string `json:"email" binding:"required,email" validate:"email"`
Password string `json:"password" binding:"min=8,max=32,alphanum"`
ConfirmPassword string `json:"confirm_password" binding:"eqfield=Password,required"`
}

我正在尝试添加验证中间件
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 := validator.Validate(&user); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
}
}
我回到每个地方的错误,我在架构上添加了验证
{
"error": "Email: unknown tag"
}{
"username": "this the first video description",
"email": "h",
"password": "password",
"confirm_password": "password"
}

最佳答案

当前软件包中没有“电子邮件”验证程序:https://github.com/go-validator/validator/tree/v2
https://github.com/go-playground/validator中有一个“电子邮件”验证器

关于go - 无法使用validator.v2 golang进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62708313/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com