mirror of
https://github.com/Fishwaldo/validator.git
synced 2025-03-15 19:51:38 +00:00
parent
f604b6cc96
commit
92bd6b335a
2 changed files with 8 additions and 68 deletions
2
doc.go
2
doc.go
|
@ -176,7 +176,7 @@ Here is a list of the current built in validators:
|
|||
dive
|
||||
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.
|
||||
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)
|
||||
Example: [][]string with validation tag "gt=0,dive,len=1,dive,required"
|
||||
gt=0 will be applied to []
|
||||
|
|
74
validator.go
74
validator.go
|
@ -192,57 +192,6 @@ func (e *FieldError) Error() string {
|
|||
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
|
||||
// for those that want/need it.
|
||||
// This is now needed because of the new dive functionality
|
||||
|
@ -273,7 +222,7 @@ func (e *FieldError) Flatten() map[string]*FieldError {
|
|||
|
||||
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 {
|
||||
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)
|
||||
|
||||
if flat := se.flatten(false); flat != nil && len(flat) > 0 {
|
||||
if flat := se.Flatten(); flat != nil && len(flat) > 0 {
|
||||
for k, v := range flat {
|
||||
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())
|
||||
}
|
||||
|
||||
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 {
|
||||
return nil
|
||||
}
|
||||
|
@ -361,12 +313,6 @@ func (e *StructErrors) flatten(isFromStruct bool) map[string]*FieldError {
|
|||
|
||||
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 {
|
||||
errs[f.Field+k] = fe
|
||||
} else {
|
||||
|
@ -378,7 +324,7 @@ func (e *StructErrors) flatten(isFromStruct bool) map[string]*FieldError {
|
|||
|
||||
for key, val := range e.StructErrors {
|
||||
|
||||
otherErrs := val.flatten(isFromStruct)
|
||||
otherErrs := val.Flatten()
|
||||
|
||||
for _, f2 := range otherErrs {
|
||||
|
||||
|
@ -390,12 +336,6 @@ func (e *StructErrors) flatten(isFromStruct bool) map[string]*FieldError {
|
|||
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
|
||||
// top = top level struct when validating by struct otherwise nil
|
||||
// current = current level struct when validating by struct otherwise optional comparison value
|
||||
|
|
Loading…
Add table
Reference in a new issue