add test content

pull/787/head
qm012 3 years ago
parent 0b0f695dc5
commit 6ad1659689
  1. 7
      cache.go
  2. 2
      go.mod
  3. 64
      validator_test.go

@ -163,6 +163,13 @@ func (v *Validate) extractStructCache(current reflect.Value, sName string) *cStr
ctname := v.customTags[i] ctname := v.customTags[i]
tagValues[ctname] = strings.TrimSpace(fld.Tag.Get(ctname)) tagValues[ctname] = strings.TrimSpace(fld.Tag.Get(ctname))
} }
// The priority of RegisterTagNameFunc is higher than the custom tag value
if v.hasTagNameFunc {
value := v.tagNameFunc(fld)
if len(value) != 0 {
tagValues[defaultErrTagName] = value
}
}
cs.fields = append(cs.fields, &cField{ cs.fields = append(cs.fields, &cField{
idx: i, idx: i,

@ -8,5 +8,5 @@ require (
github.com/go-playground/universal-translator v0.17.0 github.com/go-playground/universal-translator v0.17.0
github.com/leodido/go-urn v1.2.0 github.com/leodido/go-urn v1.2.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/text v0.3.2 // indirect golang.org/x/text v0.3.2
) )

@ -11280,3 +11280,67 @@ func TestPostCodeByIso3166Alpha2Field_InvalidKind(t *testing.T) {
_ = New().Struct(test{"ABC", 123, false}) _ = New().Struct(test{"ABC", 123, false})
t.Errorf("Didn't panic as expected") t.Errorf("Didn't panic as expected")
} }
func TestCustomMessage(t *testing.T) {
type test struct {
Min int `validate:"min=1" message:"test int min"`
Max string `validate:"max=10" message:"test string max"`
Required []int `validate:"required" message:"test slice required"`
}
err := New().Struct(&test{Max: "xxxxxxxxxxxxxxxxxx"})
if err == nil {
t.Fatal("struct validator err")
}
if !strings.Contains(err.Error(), "test int min") ||
!strings.Contains(err.Error(), "test string max") ||
!strings.Contains(err.Error(), "test slice required") {
t.Error("Unable to match custom err info")
}
}
func TestCustomTagMessage(t *testing.T) {
type test struct {
Min int `validate:"min=1" msg:"中文"`
Max string `validate:"max=10" msg:"한글"`
Required []int `validate:"required" msg:"日本語"`
}
validate := New()
validate.SetErrTagName("msg")
err := validate.Struct(&test{Max: "xxxxxxxxxxxxxxxxxx"})
if err == nil {
t.Fatal("struct validator err")
}
if !strings.Contains(err.Error(), "中文") ||
!strings.Contains(err.Error(), "한글") ||
!strings.Contains(err.Error(), "日本語") {
t.Error("Unable to match custom err info")
}
}
func TestCustomRegisterTagMessage(t *testing.T) {
type test struct {
Min int `validate:"min=1" msg:"中文" schema:"schema int error"`
Max string `validate:"max=10" msg:"한글" schema:"schema string error"`
Required []int `validate:"required" msg:"日本語"`
}
validate := New()
validate.RegisterTagNameFunc(func(field reflect.StructField) string {
return field.Tag.Get("schema")
})
validate.SetErrTagName("msg")
err := validate.Struct(&test{Max: "xxxxxxxxxxxxxxxxxx"})
if err == nil {
t.Fatal("struct validator err")
}
if !strings.Contains(err.Error(), "schema int error") ||
!strings.Contains(err.Error(), "schema string error") ||
!strings.Contains(err.Error(), "日本語") {
t.Error("Unable to match custom err info")
}
}

Loading…
Cancel
Save