Przeglądaj źródła

Merge pull request #224 from zazab/bind-check-sub-structs

Add validating sub structures
Javier Provecho Fernandez 10 lat temu
rodzic
commit
0f46ae2b81
1 zmienionych plików z 16 dodań i 0 usunięć
  1. 16 0
      binding/binding.go

+ 16 - 0
binding/binding.go

@@ -199,6 +199,22 @@ func Validate(obj interface{}, parents ...string) error {
 						return err
 						return err
 					}
 					}
 				}
 				}
+			} else {
+				fieldType := field.Type.Kind()
+				if fieldType == reflect.Struct {
+					if reflect.DeepEqual(zero, fieldValue) {
+						continue
+					}
+					err := Validate(fieldValue, field.Name)
+					if err != nil {
+						return err
+					}
+				} else if fieldType == reflect.Slice && field.Type.Elem().Kind() == reflect.Struct {
+					err := Validate(fieldValue, field.Name)
+					if err != nil {
+						return err
+					}
+				}
 			}
 			}
 		}
 		}
 	case reflect.Slice:
 	case reflect.Slice: