Skip to content

Commit bb80e92

Browse files
Dean KarnDean Karn
authored andcommitted
Merge branch 'v2-development' into v2
2 parents 70fa985 + c0c54e6 commit bb80e92

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

validator.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type FieldValidationError struct {
2929
Field string
3030
ErrorTag string
3131
Kind reflect.Kind
32+
Type reflect.Type
3233
Param string
3334
Value interface{}
3435
}
@@ -168,12 +169,6 @@ func ValidateStruct(s interface{}) *StructValidationErrors {
168169
// ValidateStruct validates a struct and returns a struct containing the errors
169170
func (v *Validator) ValidateStruct(s interface{}) *StructValidationErrors {
170171

171-
structValue := reflect.ValueOf(s)
172-
173-
if structValue.Kind() == reflect.Ptr && !structValue.IsNil() {
174-
return v.ValidateStruct(structValue.Elem().Interface())
175-
}
176-
177172
return v.validateStructRecursive(s, s)
178173
}
179174

@@ -298,16 +293,19 @@ func (v *Validator) validateFieldByNameAndTagAndValue(val interface{}, f interfa
298293
}
299294

300295
valueField := reflect.ValueOf(f)
296+
fieldKind := valueField.Kind()
301297

302-
if valueField.Kind() == reflect.Ptr && !valueField.IsNil() {
298+
if fieldKind == reflect.Ptr && !valueField.IsNil() {
303299
return v.validateFieldByNameAndTagAndValue(val, valueField.Elem().Interface(), name, tag)
304300
}
305301

306-
switch valueField.Kind() {
302+
fieldType := valueField.Type()
303+
304+
switch fieldKind {
307305

308306
case reflect.Struct, reflect.Interface, reflect.Invalid:
309307

310-
if valueField.Type() != reflect.TypeOf(time.Time{}) {
308+
if fieldType != reflect.TypeOf(time.Time{}) {
311309
panic("Invalid field passed to ValidateFieldWithTag")
312310
}
313311
}
@@ -339,13 +337,15 @@ func (v *Validator) validateFieldByNameAndTagAndValue(val interface{}, f interfa
339337
errTag = strings.TrimLeft(errTag, "|")
340338

341339
valErr.ErrorTag = errTag
342-
valErr.Kind = valueField.Kind()
340+
valErr.Kind = fieldKind
343341

344342
return valErr
345343
}
346344

347345
if valErr, err = v.validateFieldByNameAndSingleTag(val, f, name, valTag); err != nil {
346+
348347
valErr.Kind = valueField.Kind()
348+
valErr.Type = fieldType
349349

350350
return valErr
351351
}

validator_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package validator_test
22

33
import (
44
"fmt"
5+
"reflect"
56
"testing"
67
"time"
78

@@ -197,6 +198,7 @@ func (ms *MySuite) TestUnexposedStruct(c *C) {
197198
A string `validate:"required"`
198199
}
199200
}
201+
200202
s := &Test{
201203
Name: "TEST",
202204
}
@@ -290,6 +292,7 @@ func (ms *MySuite) TestIsGte(c *C) {
290292
err = validator.ValidateFieldByTag(t2, "gte")
291293
c.Assert(err, NotNil)
292294
c.Assert(err.ErrorTag, Equals, "gte")
295+
c.Assert(err.Type, Equals, reflect.TypeOf(time.Time{}))
293296

294297
type Test struct {
295298
Now *time.Time `validate:"gte"`

0 commit comments

Comments
 (0)