Added reference to parent structs

This commit is contained in:
Jimmy Pettersson 2014-08-22 15:54:26 +02:00
parent 06e9f94970
commit d7f4db4ad3
1 changed files with 7 additions and 4 deletions

View File

@ -151,7 +151,7 @@ func ensureNotPointer(obj interface{}) {
} }
} }
func Validate(obj interface{}) error { func Validate(obj interface{}, parents ...string) error {
typ := reflect.TypeOf(obj) typ := reflect.TypeOf(obj)
val := reflect.ValueOf(obj) val := reflect.ValueOf(obj)
@ -179,13 +179,16 @@ func Validate(obj interface{}) error {
if reflect.DeepEqual(zero, fieldValue) { if reflect.DeepEqual(zero, fieldValue) {
return errors.New("Required " + field.Name) return errors.New("Required " + field.Name)
} }
err := Validate(fieldValue, field.Name)
err := Validate(fieldValue)
if err != nil { if err != nil {
return err return err
} }
} else if reflect.DeepEqual(zero, fieldValue) { } else if reflect.DeepEqual(zero, fieldValue) {
if len(parents) > 0 {
return errors.New("Required " + field.Name + " on " + parents[0])
} else {
return errors.New("Required " + field.Name) return errors.New("Required " + field.Name)
}
} else if fieldType == reflect.Slice && field.Type.Elem().Kind() == reflect.Struct { } else if fieldType == reflect.Slice && field.Type.Elem().Kind() == reflect.Struct {
err := Validate(fieldValue) err := Validate(fieldValue)
if err != nil { if err != nil {