97d99fce by miraclesu

Change tag valid func default key

1 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) {
120 if err != nil { 120 if err != nil {
121 return 121 return
122 } 122 }
123 vfs = []ValidFunc{ValidFunc{"Match", []interface{}{reg, key}}} 123 vfs = []ValidFunc{ValidFunc{"Match", []interface{}{reg, key + ".Match"}}}
124 str = strings.TrimSpace(tag[:index]) + strings.TrimSpace(tag[end+len("/)"):]) 124 str = strings.TrimSpace(tag[:index]) + strings.TrimSpace(tag[end+len("/)"):])
125 return 125 return
126 } 126 }
...@@ -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) {
145 err = fmt.Errorf("%s require %d parameters", vfunc, num) 145 err = fmt.Errorf("%s require %d parameters", vfunc, num)
146 return 146 return
147 } 147 }
148 v = ValidFunc{vfunc, []interface{}{key}} 148 v = ValidFunc{vfunc, []interface{}{key + "." + vfunc}}
149 return 149 return
150 } 150 }
151 151
...@@ -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) {
167 return 167 return
168 } 168 }
169 169
170 tParams, err := trim(name, key, params) 170 tParams, err := trim(name, key+"."+name, params)
171 if err != nil { 171 if err != nil {
172 return 172 return
173 } 173 }
......
...@@ -298,6 +298,7 @@ func TestValid(t *testing.T) { ...@@ -298,6 +298,7 @@ func TestValid(t *testing.T) {
298 } 298 }
299 299
300 uptr := &user{Name: "test", Age: 40} 300 uptr := &user{Name: "test", Age: 40}
301 valid.Clear()
301 b, err = valid.Valid(uptr) 302 b, err = valid.Valid(uptr)
302 if err != nil { 303 if err != nil {
303 t.Fatal(err) 304 t.Fatal(err)
...@@ -305,8 +306,15 @@ func TestValid(t *testing.T) { ...@@ -305,8 +306,15 @@ func TestValid(t *testing.T) {
305 if b { 306 if b {
306 t.Error("validation should not be passed") 307 t.Error("validation should not be passed")
307 } 308 }
309 if len(valid.Errors) != 1 {
310 t.Fatalf("valid errors len should be 1 but got %d", len(valid.Errors))
311 }
312 if valid.Errors[0].Key != "Name.Match" {
313 t.Errorf("Message key should be `Name.Match` but got %s", valid.Errors[0].Key)
314 }
308 315
309 u = user{Name: "test@/test/;com", Age: 180} 316 u = user{Name: "test@/test/;com", Age: 180}
317 valid.Clear()
310 b, err = valid.Valid(u) 318 b, err = valid.Valid(u)
311 if err != nil { 319 if err != nil {
312 t.Fatal(err) 320 t.Fatal(err)
...@@ -314,4 +322,10 @@ func TestValid(t *testing.T) { ...@@ -314,4 +322,10 @@ func TestValid(t *testing.T) {
314 if b { 322 if b {
315 t.Error("validation should not be passed") 323 t.Error("validation should not be passed")
316 } 324 }
325 if len(valid.Errors) != 1 {
326 t.Fatalf("valid errors len should be 1 but got %d", len(valid.Errors))
327 }
328 if valid.Errors[0].Key != "Age.Range" {
329 t.Errorf("Message key should be `Name.Match` but got %s", valid.Errors[0].Key)
330 }
317 } 331 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!