Merge pull request #4225 from HITWHTigerLiu/develop-2.0

Empty field in validator.Error when label struct tag is not declared #4222
This commit is contained in:
Ming Deng 2020-09-17 21:27:18 +08:00 committed by GitHub
commit df043f22fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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")
}
}