database
从内部获取数据(字段名称)的方法是validator.go
什么?
main.go
package main
import (
"v/utils"
"os"
"github.com/go-playground/validator/v10"
)
func init() {
env.Load(".env")
}
func main() {
e := echo.New()
e.Validator = &utils.CustomValidator{Validator: validator.New()}
e.Logger.Fatal(e.Start("1234"))
}
验证器.go
package utils
import (
"unicode"
"unicode/utf8"
"github.com/go-playground/validator/v10"
)
// CustomValidator is type setting of third party validator
type CustomValidator struct {
Validator *validator.Validate
}
// Init validator
func (cv *CustomValidator) Init() {
cv.Validator.RegisterValidation("name", func(fl validator.FieldLevel) bool {
// ******
// **Need to fetch the `names` from database and check any duplicate**
// *******
return true
})
}
// Validate Data
func (cv *CustomValidator) Validate(i interface{}) error {
return cv.Validator.Struct(i)
}