mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
add valid tag for gvalid
This commit is contained in:
@ -69,7 +69,12 @@ func CheckStruct(object interface{}, rules interface{}, msgs ...CustomMsg) *Erro
|
||||
continue
|
||||
}
|
||||
params[fieldName] = field.Value()
|
||||
if tag := field.Tag("gvalid"); tag != "" {
|
||||
// 同时支持valid和gvalid标签,优先使用valid
|
||||
tag := field.Tag("valid")
|
||||
if tag == "" {
|
||||
tag = field.Tag("gvalid")
|
||||
}
|
||||
if tag != "" {
|
||||
// sequence tag == struct tag, 这里的name为别名
|
||||
name, rule, msg := parseSequenceTag(tag)
|
||||
if len(name) == 0 {
|
||||
|
||||
@ -7,9 +7,10 @@
|
||||
package gvalid_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"github.com/gogf/gf/g/util/gvalid"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_CheckStruct(t *testing.T) {
|
||||
@ -51,6 +52,7 @@ func Test_CheckStruct(t *testing.T) {
|
||||
gtest.Assert(err.Maps()["password"]["required"], "登录密码不能为空")
|
||||
})
|
||||
|
||||
// gvalid tag
|
||||
gtest.Case(t, func() {
|
||||
type User struct {
|
||||
Id int `gvalid:"uid@required|min:10#|ID不能为空"`
|
||||
@ -69,4 +71,22 @@ func Test_CheckStruct(t *testing.T) {
|
||||
gtest.Assert(err.Maps()["uid"]["min"], "ID不能为空")
|
||||
})
|
||||
|
||||
// valid tag
|
||||
gtest.Case(t, func() {
|
||||
type User struct {
|
||||
Id int `valid:"uid@required|min:10#|ID不能为空"`
|
||||
Age int `valid:"age@required#年龄不能为空"`
|
||||
Username string `json:"username" gvalid:"username@required#用户名不能为空"`
|
||||
Password string `json:"password" gvalid:"password@required#登录密码不能为空"`
|
||||
}
|
||||
user := &User{
|
||||
Id: 1,
|
||||
Username: "john",
|
||||
Password: "123456",
|
||||
}
|
||||
err := gvalid.CheckStruct(user, nil)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.Assert(len(err.Maps()), 1)
|
||||
gtest.Assert(err.Maps()["uid"]["min"], "ID不能为空")
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user