Empty field in validator.Error when label struct tag is not declared #4222

This commit is contained in:
l00427301 2020-09-14 16:18:24 +08:00
parent 5973ef107c
commit 5618df8c76
2 changed files with 30 additions and 0 deletions

View File

@ -269,6 +269,11 @@ func (v *Validation) apply(chk Validator, obj interface{}) *Result {
Field := ""
Label := ""
parts := strings.Split(key, ".")
if len(parts) == 2 {
Field = parts[0]
Name = parts[1]
Label = Field
}
if len(parts) == 3 {
Field = parts[0]
Name = parts[1]

View File

@ -607,3 +607,28 @@ func TestCanSkipAlso(t *testing.T) {
}
}
func TestFieldNoEmpty(t *testing.T) {
type User struct {
Name string `json:"name" valid:"Match(/^[a-zA-Z][a-zA-Z0-9._-]{0,31}$/)"`
}
u := User{
Name: "*",
}
valid := Validation{}
b, err := valid.Valid(u)
if err != nil {
t.Fatal(err)
}
if b {
t.Fatal("validation should be passed")
}
if len(valid.Errors) == 0 {
t.Fatal("validation should be passed")
}
validErr := valid.Errors[0]
if len(validErr.Field) == 0 {
t.Fatal("validation should be passed")
}
}