remove the double isStruct/isStructPtr check
Showing
1 changed file
with
13 additions
and
13 deletions
| ... | @@ -341,6 +341,7 @@ func (v *Validation) Valid(obj interface{}) (b bool, err error) { | ... | @@ -341,6 +341,7 @@ func (v *Validation) Valid(obj interface{}) (b bool, err error) { |
| 341 | // Anonymous fields will be ignored | 341 | // Anonymous fields will be ignored |
| 342 | func (v *Validation) RecursiveValid(objc interface{}) (bool, error) { | 342 | func (v *Validation) RecursiveValid(objc interface{}) (bool, error) { |
| 343 | //Step 1: validate obj itself firstly | 343 | //Step 1: validate obj itself firstly |
| 344 | // fails if objc is not struct | ||
| 344 | pass, err := v.Valid(objc) | 345 | pass, err := v.Valid(objc) |
| 345 | if err != nil || false == pass { | 346 | if err != nil || false == pass { |
| 346 | return pass, err // Stop recursive validation | 347 | return pass, err // Stop recursive validation |
| ... | @@ -348,23 +349,22 @@ func (v *Validation) RecursiveValid(objc interface{}) (bool, error) { | ... | @@ -348,23 +349,22 @@ func (v *Validation) RecursiveValid(objc interface{}) (bool, error) { |
| 348 | // Step 2: Validate struct's struct fields | 349 | // Step 2: Validate struct's struct fields |
| 349 | objT := reflect.TypeOf(objc) | 350 | objT := reflect.TypeOf(objc) |
| 350 | objV := reflect.ValueOf(objc) | 351 | objV := reflect.ValueOf(objc) |
| 351 | if isStruct(objT) || isStructPtr(objT) { | ||
| 352 | 352 | ||
| 353 | if isStructPtr(objT) { | 353 | if isStructPtr(objT) { |
| 354 | objT = objT.Elem() | 354 | objT = objT.Elem() |
| 355 | objV = objV.Elem() | 355 | objV = objV.Elem() |
| 356 | } | 356 | } |
| 357 | 357 | ||
| 358 | for i := 0; i < objT.NumField(); i++ { | 358 | for i := 0; i < objT.NumField(); i++ { |
| 359 | 359 | ||
| 360 | t := objT.Field(i).Type | 360 | t := objT.Field(i).Type |
| 361 | 361 | ||
| 362 | if isStruct(t) || isStructPtr(t) { | 362 | // Recursive applies to struct or pointer to structs fields |
| 363 | // Step 3: do the recursive validation | 363 | if isStruct(t) || isStructPtr(t) { |
| 364 | // Only valid the Public field recursively | 364 | // Step 3: do the recursive validation |
| 365 | if objV.Field(i).CanInterface() { | 365 | // Only valid the Public field recursively |
| 366 | pass, err = v.RecursiveValid(objV.Field(i).Interface()) | 366 | if objV.Field(i).CanInterface() { |
| 367 | } | 367 | pass, err = v.RecursiveValid(objV.Field(i).Interface()) |
| 368 | } | 368 | } |
| 369 | } | 369 | } |
| 370 | } | 370 | } | ... | ... |
-
Please register or sign in to post a comment