Commit 97d99fce authored by miraclesu's avatar miraclesu

Change tag valid func default key

parent 2fa534ff
...@@ -120,7 +120,7 @@ func getRegFuncs(tag, key string) (vfs []ValidFunc, str string, err error) { ...@@ -120,7 +120,7 @@ func getRegFuncs(tag, key string) (vfs []ValidFunc, str string, err error) {
if err != nil { if err != nil {
return return
} }
vfs = []ValidFunc{ValidFunc{"Match", []interface{}{reg, key}}} vfs = []ValidFunc{ValidFunc{"Match", []interface{}{reg, key + ".Match"}}}
str = strings.TrimSpace(tag[:index]) + strings.TrimSpace(tag[end+len("/)"):]) str = strings.TrimSpace(tag[:index]) + strings.TrimSpace(tag[end+len("/)"):])
return return
} }
...@@ -145,7 +145,7 @@ func parseFunc(vfunc, key string) (v ValidFunc, err error) { ...@@ -145,7 +145,7 @@ func parseFunc(vfunc, key string) (v ValidFunc, err error) {
err = fmt.Errorf("%s require %d parameters", vfunc, num) err = fmt.Errorf("%s require %d parameters", vfunc, num)
return return
} }
v = ValidFunc{vfunc, []interface{}{key}} v = ValidFunc{vfunc, []interface{}{key + "." + vfunc}}
return return
} }
...@@ -167,7 +167,7 @@ func parseFunc(vfunc, key string) (v ValidFunc, err error) { ...@@ -167,7 +167,7 @@ func parseFunc(vfunc, key string) (v ValidFunc, err error) {
return return
} }
tParams, err := trim(name, key, params) tParams, err := trim(name, key+"."+name, params)
if err != nil { if err != nil {
return return
} }
......
...@@ -298,6 +298,7 @@ func TestValid(t *testing.T) { ...@@ -298,6 +298,7 @@ func TestValid(t *testing.T) {
} }
uptr := &user{Name: "test", Age: 40} uptr := &user{Name: "test", Age: 40}
valid.Clear()
b, err = valid.Valid(uptr) b, err = valid.Valid(uptr)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -305,8 +306,15 @@ func TestValid(t *testing.T) { ...@@ -305,8 +306,15 @@ func TestValid(t *testing.T) {
if b { if b {
t.Error("validation should not be passed") t.Error("validation should not be passed")
} }
if len(valid.Errors) != 1 {
t.Fatalf("valid errors len should be 1 but got %d", len(valid.Errors))
}
if valid.Errors[0].Key != "Name.Match" {
t.Errorf("Message key should be `Name.Match` but got %s", valid.Errors[0].Key)
}
u = user{Name: "test@/test/;com", Age: 180} u = user{Name: "test@/test/;com", Age: 180}
valid.Clear()
b, err = valid.Valid(u) b, err = valid.Valid(u)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -314,4 +322,10 @@ func TestValid(t *testing.T) { ...@@ -314,4 +322,10 @@ func TestValid(t *testing.T) {
if b { if b {
t.Error("validation should not be passed") t.Error("validation should not be passed")
} }
if len(valid.Errors) != 1 {
t.Fatalf("valid errors len should be 1 but got %d", len(valid.Errors))
}
if valid.Errors[0].Key != "Age.Range" {
t.Errorf("Message key should be `Name.Match` but got %s", valid.Errors[0].Key)
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment