fix wrong variable used in checking for "[" char

for #85
pull/89/head
joeybloggs 10 years ago
parent 7d55bfddde
commit 4d57165562
  1. 32
      validator.go
  2. 5
      validator_test.go

@ -248,7 +248,6 @@ func (e *FieldError) Error() string {
// This is now needed because of the new dive functionality
func (e *FieldError) Flatten() map[string]*FieldError {
// return e.flatten(false)
errs := map[string]*FieldError{}
if e.IsPlaceholderErr {
@ -276,18 +275,31 @@ func (e *FieldError) Flatten() map[string]*FieldError {
}
}
}
}
if e.IsMap {
// for _, err := range e.MapErrs {
for key, err := range e.MapErrs {
fe, ok := err.(*FieldError)
if ok {
if flat := fe.Flatten(); flat != nil && len(flat) > 0 {
for k, v := range flat {
errs[fmt.Sprintf("[%#v]%s", key, k)] = v
}
}
} else {
se := err.(*StructErrors)
// if flat := err.Flatten(); flat != nil && len(flat) > 0 {
// for k, v := range flat {
// errs[k] = v
// }
// }
// }
if flat := se.flatten(false); flat != nil && len(flat) > 0 {
for k, v := range flat {
errs[fmt.Sprintf("[%#v].%s.%s", key, se.Struct, k)] = v
}
}
}
}
}
return errs
@ -340,7 +352,7 @@ func (e *StructErrors) flatten(isFromStruct bool) map[string]*FieldError {
for k, fe := range flat {
if isFromStruct && f.Field[0:1] == "[" {
if isFromStruct && k[0:1] == "[" {
errs[f.Field+k] = fe
} else {
errs[k] = fe

@ -266,6 +266,11 @@ func TestFlattenValidation(t *testing.T) {
Equal(t, flatFieldErr.Field, "Name")
Equal(t, flatFieldErr.Tag, "required")
structErrFlatten, ok := errs.Flatten()["Errs[0][1].Inner.Name"]
Equal(t, ok, true)
Equal(t, structErrFlatten.Field, "Name")
Equal(t, structErrFlatten.Tag, "required")
// expect Errs[0][1].Inner.Name = error
// fmt.Println((fieldErr.SliceOrArrayErrs[0].(*FieldError)).Field)
// fmt.Println((fieldErr.SliceOrArrayErrs[0].(*FieldError)).IsPlaceholderErr)

Loading…
Cancel
Save