diff --git a/cache.go b/cache.go index 314beda..78e2006 100644 --- a/cache.go +++ b/cache.go @@ -163,6 +163,13 @@ func (v *Validate) extractStructCache(current reflect.Value, sName string) *cStr ctname := v.customTags[i] 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{ idx: i, diff --git a/go.mod b/go.mod index 53c4820..2bd235d 100644 --- a/go.mod +++ b/go.mod @@ -8,5 +8,5 @@ require ( github.com/go-playground/universal-translator v0.17.0 github.com/leodido/go-urn v1.2.0 golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 - golang.org/x/text v0.3.2 // indirect + golang.org/x/text v0.3.2 ) diff --git a/validator_test.go b/validator_test.go index 5dcd00e..f30fed0 100644 --- a/validator_test.go +++ b/validator_test.go @@ -11280,3 +11280,67 @@ func TestPostCodeByIso3166Alpha2Field_InvalidKind(t *testing.T) { _ = New().Struct(test{"ABC", 123, false}) 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") + } +}