From b65b3a4b7f3ad1c73744b90b7c3950bc89425794 Mon Sep 17 00:00:00 2001 From: Oscar van Leusen Date: Thu, 25 Aug 2022 15:40:22 +0100 Subject: [PATCH 1/4] make attempt to fix issue --- data_source.go | 26 ++++++++++++++++---------- messages_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/data_source.go b/data_source.go index d622e21..b30c6cd 100644 --- a/data_source.go +++ b/data_source.go @@ -431,6 +431,7 @@ func (d *StructData) parseRulesFromTag(v *Validation) { // eg: `message:"required:name is required|minLen:name min len is %d"` func (d *StructData) loadMessagesFromTag(trans *Translator, field, vRule, vMsg string) { var msgKey, vName string + var vNames []string // only one message, use for first validator. // eg: `message:"name is required"` @@ -439,32 +440,37 @@ func (d *StructData) loadMessagesFromTag(trans *Translator, field, vRule, vMsg s if strings.ContainsRune(vMsg, ':') { nodes := strings.SplitN(vMsg, ":", 2) vName = strings.TrimSpace(nodes[0]) + vNames = []string{vName} // first is validator name vMsg = strings.TrimSpace(nodes[1]) } if vName == "" { // eg `validate:"required|date"` - vName = vRule + vNames = append(vNames, vRule) + if strings.ContainsRune(vRule, '|') { - nodes := strings.SplitN(vRule, "|", 2) - vName = nodes[0] + vNames = strings.Split(vRule, "|") } - // has params for validator: "minLen:5" - if strings.ContainsRune(vName, ':') { - nodes := strings.SplitN(vRule, ":", 2) - vName = nodes[0] + for i, node := range vNames { + // has params for validator: "minLen:5" + if strings.ContainsRune(node, ':') { + tmp := strings.SplitN(node, ":", 2) + vNames[i] = tmp[0] + } } } // if rName, has := validatorAliases[validator]; has { // msgKey = field + "." + rName // } else { - msgKey = field + "." + vName - // } - trans.AddMessage(msgKey, vMsg) + for _, name := range vNames { + msgKey = field + "." + name + trans.AddMessage(msgKey, vMsg) + } + return } diff --git a/messages_test.go b/messages_test.go index de3fc26..73780a4 100644 --- a/messages_test.go +++ b/messages_test.go @@ -155,6 +155,32 @@ func TestMessageOnStruct(t *testing.T) { v = Struct(s4) is.False(v.Validate()) is.Equal("出生日期有误", v.Errors.One()) + + // Ensure message with no specified field applies to all validation errors on a single field if SkipOnError=false + // Set custom global options + Config(func(opt *GlobalOption) { + opt.SkipOnEmpty = false + opt.StopOnError = false + }) + + s5 := &struct { + Name string `validate:"string"` + BirthDay string `validate:"max_len:1|date" message:"出生日期有误"` + }{ + "tom", + "ff", + } + + v = Struct(s5) + + is.False(v.Validate()) + is.Equal("BirthDay:\n max_len: 出生日期有误\n date: 出生日期有误", v.Errors.String()) + + // Restore original global options + Config(func(opt *GlobalOption) { + opt.SkipOnEmpty = true + opt.StopOnError = true + }) } // with field tag: json From 0750547fc678664a5e0600b9a39cd8f992957a26 Mon Sep 17 00:00:00 2001 From: Oscar van Leusen Date: Thu, 25 Aug 2022 15:45:07 +0100 Subject: [PATCH 2/4] improve test to account for map order randomness --- data_source.go | 2 +- messages_test.go | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/data_source.go b/data_source.go index b30c6cd..67a28fa 100644 --- a/data_source.go +++ b/data_source.go @@ -447,7 +447,7 @@ func (d *StructData) loadMessagesFromTag(trans *Translator, field, vRule, vMsg s if vName == "" { // eg `validate:"required|date"` - vNames = append(vNames, vRule) + vNames = []string{vRule} if strings.ContainsRune(vRule, '|') { vNames = strings.Split(vRule, "|") diff --git a/messages_test.go b/messages_test.go index 73780a4..482d4da 100644 --- a/messages_test.go +++ b/messages_test.go @@ -174,7 +174,9 @@ func TestMessageOnStruct(t *testing.T) { v = Struct(s5) is.False(v.Validate()) - is.Equal("BirthDay:\n max_len: 出生日期有误\n date: 出生日期有误", v.Errors.String()) + is.Contains(v.Errors.String(), "BirthDay") + is.Contains(v.Errors.String(), "max_len: 出生日期有误") + is.Contains(v.Errors.String(), "date: 出生日期有误") // Restore original global options Config(func(opt *GlobalOption) { From f82e6e11c1b36dc1fb419831b6aa52070a2b9585 Mon Sep 17 00:00:00 2001 From: Oscar van Leusen Date: Thu, 25 Aug 2022 15:46:16 +0100 Subject: [PATCH 3/4] remove unnecessary globaloption --- messages_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/messages_test.go b/messages_test.go index 482d4da..6141c9b 100644 --- a/messages_test.go +++ b/messages_test.go @@ -159,7 +159,6 @@ func TestMessageOnStruct(t *testing.T) { // Ensure message with no specified field applies to all validation errors on a single field if SkipOnError=false // Set custom global options Config(func(opt *GlobalOption) { - opt.SkipOnEmpty = false opt.StopOnError = false }) @@ -180,7 +179,6 @@ func TestMessageOnStruct(t *testing.T) { // Restore original global options Config(func(opt *GlobalOption) { - opt.SkipOnEmpty = true opt.StopOnError = true }) } From 59bcae352c2d9a0ee77354161b0683e9cb4cdae8 Mon Sep 17 00:00:00 2001 From: Oscar van Leusen Date: Thu, 25 Aug 2022 15:49:29 +0100 Subject: [PATCH 4/4] improve comment --- messages_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/messages_test.go b/messages_test.go index 6141c9b..5b205b4 100644 --- a/messages_test.go +++ b/messages_test.go @@ -156,8 +156,8 @@ func TestMessageOnStruct(t *testing.T) { is.False(v.Validate()) is.Equal("出生日期有误", v.Errors.One()) - // Ensure message with no specified field applies to all validation errors on a single field if SkipOnError=false - // Set custom global options + // Ensure message override with no specified field applies to all validation errors if SkipOnError=false is set in + // global options Config(func(opt *GlobalOption) { opt.StopOnError = false })