code cleanup

for #88
pull/89/head
joeybloggs 10 years ago
parent f604b6cc96
commit 92bd6b335a
  1. 2
      doc.go
  2. 74
      validator.go

@ -176,7 +176,7 @@ Here is a list of the current built in validators:
dive dive
This tells the validator to dive into a slice, array or map and validate that This tells the validator to dive into a slice, array or map and validate that
level of the slice, array or map with the validation tags that follow. level of the slice, array or map with the validation tags that follow.
Multidimensional nesting is also supported, each level you with to dive will Multidimensional nesting is also supported, each level you wish to dive will
require another dive tag. (Usage: dive) require another dive tag. (Usage: dive)
Example: [][]string with validation tag "gt=0,dive,len=1,dive,required" Example: [][]string with validation tag "gt=0,dive,len=1,dive,required"
gt=0 will be applied to [] gt=0 will be applied to []

@ -192,57 +192,6 @@ func (e *FieldError) Error() string {
return fmt.Sprintf(fieldErrMsg, e.Field, e.Tag) return fmt.Sprintf(fieldErrMsg, e.Field, e.Tag)
} }
// func (e *FieldError) flatten(isFromStruct bool) map[string]*FieldError {
// errs := map[string]*FieldError{}
// if e.IsPlaceholderErr {
// if e.IsSliceOrArray {
// for key, err := range e.SliceOrArrayErrs {
// fe, ok := err.(*FieldError)
// if ok {
// if flat := fe.flatten(isFromStruct); flat != nil && len(flat) > 0 {
// for k, v := range flat {
// errs[fmt.Sprintf("[%#v]%s", key, k)] = v
// }
// }
// } else {
// se := err.(*StructErrors)
// if flat := se.flatten(isFromStruct); flat != nil && len(flat) > 0 {
// for k, v := range flat {
// errs[fmt.Sprintf("[%#v].%s.%s", key, se.Struct, k)] = v
// }
// }
// }
// }
// }
// if e.IsMap {
// // for _, err := range e.MapErrs {
// // if flat := err.Flatten(); flat != nil && len(flat) > 0 {
// // for k, v := range flat {
// // errs[k] = v
// // }
// // }
// // }
// }
// return errs
// }
// errs[e.Field] = e
// return errs
// }
// Flatten flattens the FieldError hierarchical structure into a flat namespace style field name // Flatten flattens the FieldError hierarchical structure into a flat namespace style field name
// for those that want/need it. // for those that want/need it.
// This is now needed because of the new dive functionality // This is now needed because of the new dive functionality
@ -273,7 +222,7 @@ func (e *FieldError) Flatten() map[string]*FieldError {
se := err.(*StructErrors) se := err.(*StructErrors)
if flat := se.flatten(false); flat != nil && len(flat) > 0 { if flat := se.Flatten(); flat != nil && len(flat) > 0 {
for k, v := range flat { for k, v := range flat {
errs[fmt.Sprintf("[%#v].%s.%s", key, se.Struct, k)] = v errs[fmt.Sprintf("[%#v].%s.%s", key, se.Struct, k)] = v
} }
@ -302,7 +251,7 @@ func (e *FieldError) Flatten() map[string]*FieldError {
se := err.(*StructErrors) se := err.(*StructErrors)
if flat := se.flatten(false); flat != nil && len(flat) > 0 { if flat := se.Flatten(); flat != nil && len(flat) > 0 {
for k, v := range flat { for k, v := range flat {
errs[fmt.Sprintf("[%#v].%s.%s", key, se.Struct, k)] = v errs[fmt.Sprintf("[%#v].%s.%s", key, se.Struct, k)] = v
} }
@ -348,7 +297,10 @@ func (e *StructErrors) Error() string {
return strings.TrimSpace(buff.String()) return strings.TrimSpace(buff.String())
} }
func (e *StructErrors) flatten(isFromStruct bool) map[string]*FieldError { // Flatten flattens the StructErrors hierarchical structure into a flat namespace style field name
// for those that want/need it
func (e *StructErrors) Flatten() map[string]*FieldError {
if e == nil { if e == nil {
return nil return nil
} }
@ -361,12 +313,6 @@ func (e *StructErrors) flatten(isFromStruct bool) map[string]*FieldError {
for k, fe := range flat { for k, fe := range flat {
// fmt.Println(k)
// if isFromStruct && k[0:1] == "[" {
// errs[f.Field+k] = fe
// } else {
// errs[k] = fe
// }
if f.IsPlaceholderErr { if f.IsPlaceholderErr {
errs[f.Field+k] = fe errs[f.Field+k] = fe
} else { } else {
@ -378,7 +324,7 @@ func (e *StructErrors) flatten(isFromStruct bool) map[string]*FieldError {
for key, val := range e.StructErrors { for key, val := range e.StructErrors {
otherErrs := val.flatten(isFromStruct) otherErrs := val.Flatten()
for _, f2 := range otherErrs { for _, f2 := range otherErrs {
@ -390,12 +336,6 @@ func (e *StructErrors) flatten(isFromStruct bool) map[string]*FieldError {
return errs return errs
} }
// Flatten flattens the StructErrors hierarchical structure into a flat namespace style field name
// for those that want/need it
func (e *StructErrors) Flatten() map[string]*FieldError {
return e.flatten(true)
}
// Func accepts all values needed for file and cross field validation // Func accepts all values needed for file and cross field validation
// top = top level struct when validating by struct otherwise nil // top = top level struct when validating by struct otherwise nil
// current = current level struct when validating by struct otherwise optional comparison value // current = current level struct when validating by struct otherwise optional comparison value

Loading…
Cancel
Save