|
|
@ -5,14 +5,14 @@ import ( |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
. "github.com/go-playground/assert/v2" |
|
|
|
. "github.com/go-playground/assert/v2" |
|
|
|
zhongwen "github.com/go-playground/locales/zh" |
|
|
|
chinese "github.com/go-playground/locales/zh" |
|
|
|
ut "github.com/go-playground/universal-translator" |
|
|
|
ut "github.com/go-playground/universal-translator" |
|
|
|
"github.com/go-playground/validator/v10" |
|
|
|
"github.com/go-playground/validator/v10" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func TestTranslations(t *testing.T) { |
|
|
|
func TestTranslations(t *testing.T) { |
|
|
|
|
|
|
|
|
|
|
|
zh := zhongwen.New() |
|
|
|
zh := chinese.New() |
|
|
|
uni := ut.New(zh, zh) |
|
|
|
uni := ut.New(zh, zh) |
|
|
|
trans, _ := uni.GetTranslator("zh") |
|
|
|
trans, _ := uni.GetTranslator("zh") |
|
|
|
|
|
|
|
|
|
|
@ -28,6 +28,11 @@ func TestTranslations(t *testing.T) { |
|
|
|
GteCSFieldString string |
|
|
|
GteCSFieldString string |
|
|
|
LtCSFieldString string |
|
|
|
LtCSFieldString string |
|
|
|
LteCSFieldString string |
|
|
|
LteCSFieldString string |
|
|
|
|
|
|
|
RequiredIf string |
|
|
|
|
|
|
|
RequiredUnless string |
|
|
|
|
|
|
|
RequiredWith string |
|
|
|
|
|
|
|
RequiredWithout string |
|
|
|
|
|
|
|
RequiredWithoutAll string |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type Test struct { |
|
|
|
type Test struct { |
|
|
@ -35,6 +40,12 @@ func TestTranslations(t *testing.T) { |
|
|
|
RequiredString string `validate:"required"` |
|
|
|
RequiredString string `validate:"required"` |
|
|
|
RequiredNumber int `validate:"required"` |
|
|
|
RequiredNumber int `validate:"required"` |
|
|
|
RequiredMultiple []string `validate:"required"` |
|
|
|
RequiredMultiple []string `validate:"required"` |
|
|
|
|
|
|
|
RequiredIf string `validate:"required_if=Inner.RequiredIf abcd"` |
|
|
|
|
|
|
|
RequiredUnless string `validate:"required_unless=Inner.RequiredUnless abcd"` |
|
|
|
|
|
|
|
RequiredWith string `validate:"required_with=Inner.RequiredWith"` |
|
|
|
|
|
|
|
RequiredWithAll string `validate:"required_with_all=Inner.GtCSFieldString Inner.GteCSFieldString"` |
|
|
|
|
|
|
|
RequiredWithout string `validate:"required_without=Inner.RequiredWithout"` |
|
|
|
|
|
|
|
RequiredWithoutAll string `validate:"required_without_all=Inner.RequiredUnless Inner.RequiredWithout"` |
|
|
|
LenString string `validate:"len=1"` |
|
|
|
LenString string `validate:"len=1"` |
|
|
|
LenNumber float64 `validate:"len=1113.00"` |
|
|
|
LenNumber float64 `validate:"len=1113.00"` |
|
|
|
LenMultiple []string `validate:"len=7"` |
|
|
|
LenMultiple []string `validate:"len=7"` |
|
|
@ -161,6 +172,9 @@ func TestTranslations(t *testing.T) { |
|
|
|
test.Inner.EqCSFieldString = "1234" |
|
|
|
test.Inner.EqCSFieldString = "1234" |
|
|
|
test.Inner.GtCSFieldString = "1234" |
|
|
|
test.Inner.GtCSFieldString = "1234" |
|
|
|
test.Inner.GteCSFieldString = "1234" |
|
|
|
test.Inner.GteCSFieldString = "1234" |
|
|
|
|
|
|
|
test.Inner.RequiredIf = "abcd" |
|
|
|
|
|
|
|
test.Inner.RequiredUnless = "" |
|
|
|
|
|
|
|
test.Inner.RequiredWith = "abcd" |
|
|
|
|
|
|
|
|
|
|
|
test.MaxString = "1234" |
|
|
|
test.MaxString = "1234" |
|
|
|
test.MaxNumber = 2000 |
|
|
|
test.MaxNumber = 2000 |
|
|
@ -226,6 +240,7 @@ func TestTranslations(t *testing.T) { |
|
|
|
ns string |
|
|
|
ns string |
|
|
|
expected string |
|
|
|
expected string |
|
|
|
}{ |
|
|
|
}{ |
|
|
|
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
ns: "Test.IsColor", |
|
|
|
ns: "Test.IsColor", |
|
|
|
expected: "IsColor必须是一个有效的颜色", |
|
|
|
expected: "IsColor必须是一个有效的颜色", |
|
|
@ -638,6 +653,30 @@ func TestTranslations(t *testing.T) { |
|
|
|
ns: "Test.RequiredMultiple", |
|
|
|
ns: "Test.RequiredMultiple", |
|
|
|
expected: "RequiredMultiple为必填字段", |
|
|
|
expected: "RequiredMultiple为必填字段", |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
ns: "Test.RequiredIf", |
|
|
|
|
|
|
|
expected: "RequiredIf为必填字段", |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
ns: "Test.RequiredUnless", |
|
|
|
|
|
|
|
expected: "RequiredUnless为必填字段", |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
ns: "Test.RequiredWith", |
|
|
|
|
|
|
|
expected: "RequiredWith为必填字段", |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
ns: "Test.RequiredWithAll", |
|
|
|
|
|
|
|
expected: "RequiredWithAll为必填字段", |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
ns: "Test.RequiredWithout", |
|
|
|
|
|
|
|
expected: "RequiredWithout为必填字段", |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
ns: "Test.RequiredWithoutAll", |
|
|
|
|
|
|
|
expected: "RequiredWithoutAll为必填字段", |
|
|
|
|
|
|
|
}, |
|
|
|
{ |
|
|
|
{ |
|
|
|
ns: "Test.StrPtrMinLen", |
|
|
|
ns: "Test.StrPtrMinLen", |
|
|
|
expected: "StrPtrMinLen长度必须至少为10个字符", |
|
|
|
expected: "StrPtrMinLen长度必须至少为10个字符", |
|
|
|