|
|
@ -4,9 +4,9 @@ import ( |
|
|
|
"testing" |
|
|
|
"testing" |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
. "github.com/go-playground/assert/v2" |
|
|
|
zhongwen "github.com/go-playground/locales/zh" |
|
|
|
zhongwen "github.com/go-playground/locales/zh" |
|
|
|
ut "github.com/go-playground/universal-translator" |
|
|
|
ut "github.com/go-playground/universal-translator" |
|
|
|
. "github.com/go-playground/assert/v2" |
|
|
|
|
|
|
|
"github.com/go-playground/validator/v10" |
|
|
|
"github.com/go-playground/validator/v10" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
@ -138,6 +138,10 @@ func TestTranslations(t *testing.T) { |
|
|
|
StrPtrGte *string `validate:"gte=10"` |
|
|
|
StrPtrGte *string `validate:"gte=10"` |
|
|
|
OneOfString string `validate:"oneof=red green"` |
|
|
|
OneOfString string `validate:"oneof=red green"` |
|
|
|
OneOfInt int `validate:"oneof=5 63"` |
|
|
|
OneOfInt int `validate:"oneof=5 63"` |
|
|
|
|
|
|
|
JsonString string `validate:"json"` |
|
|
|
|
|
|
|
LowercaseString string `validate:"lowercase"` |
|
|
|
|
|
|
|
UppercaseString string `validate:"uppercase"` |
|
|
|
|
|
|
|
Datetime string `validate:"datetime=2006-01-02"` |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var test Test |
|
|
|
var test Test |
|
|
@ -184,6 +188,13 @@ func TestTranslations(t *testing.T) { |
|
|
|
test.StrPtrMaxLen = &s |
|
|
|
test.StrPtrMaxLen = &s |
|
|
|
test.StrPtrLen = &s |
|
|
|
test.StrPtrLen = &s |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test.JsonString = "{\"foo\":\"bar\",}" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test.LowercaseString = "ABCDEFG" |
|
|
|
|
|
|
|
test.UppercaseString = "abcdefg" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test.Datetime = "20060102" |
|
|
|
|
|
|
|
|
|
|
|
err = validate.Struct(test) |
|
|
|
err = validate.Struct(test) |
|
|
|
NotEqual(t, err, nil) |
|
|
|
NotEqual(t, err, nil) |
|
|
|
|
|
|
|
|
|
|
@ -614,6 +625,22 @@ func TestTranslations(t *testing.T) { |
|
|
|
ns: "Test.OneOfInt", |
|
|
|
ns: "Test.OneOfInt", |
|
|
|
expected: "OneOfInt必须是[5 63]中的一个", |
|
|
|
expected: "OneOfInt必须是[5 63]中的一个", |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
ns: "Test.JsonString", |
|
|
|
|
|
|
|
expected: "JsonString必须是一个JSON字符串", |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
ns: "Test.LowercaseString", |
|
|
|
|
|
|
|
expected: "LowercaseString必须是小写字母", |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
ns: "Test.UppercaseString", |
|
|
|
|
|
|
|
expected: "UppercaseString必须是大写字母", |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
ns: "Test.Datetime", |
|
|
|
|
|
|
|
expected: "Datetime的格式必须是2006-01-02", |
|
|
|
|
|
|
|
}, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests { |
|
|
|
for _, tt := range tests { |
|
|
|