add return of variable Type as well as Kind
pull/16/head
Dean Karn 10 years ago
parent dfab3dff09
commit c0c54e6ddf
  1. 20
      validator.go
  2. 3
      validator_test.go

@ -29,6 +29,7 @@ type FieldValidationError struct {
Field string
ErrorTag string
Kind reflect.Kind
Type reflect.Type
Param string
Value interface{}
}
@ -168,12 +169,6 @@ func ValidateStruct(s interface{}) *StructValidationErrors {
// ValidateStruct validates a struct and returns a struct containing the errors
func (v *Validator) ValidateStruct(s interface{}) *StructValidationErrors {
structValue := reflect.ValueOf(s)
if structValue.Kind() == reflect.Ptr && !structValue.IsNil() {
return v.ValidateStruct(structValue.Elem().Interface())
}
return v.validateStructRecursive(s, s)
}
@ -298,16 +293,19 @@ func (v *Validator) validateFieldByNameAndTagAndValue(val interface{}, f interfa
}
valueField := reflect.ValueOf(f)
fieldKind := valueField.Kind()
if valueField.Kind() == reflect.Ptr && !valueField.IsNil() {
if fieldKind == reflect.Ptr && !valueField.IsNil() {
return v.validateFieldByNameAndTagAndValue(val, valueField.Elem().Interface(), name, tag)
}
switch valueField.Kind() {
fieldType := valueField.Type()
switch fieldKind {
case reflect.Struct, reflect.Interface, reflect.Invalid:
if valueField.Type() != reflect.TypeOf(time.Time{}) {
if fieldType != reflect.TypeOf(time.Time{}) {
panic("Invalid field passed to ValidateFieldWithTag")
}
}
@ -339,13 +337,15 @@ func (v *Validator) validateFieldByNameAndTagAndValue(val interface{}, f interfa
errTag = strings.TrimLeft(errTag, "|")
valErr.ErrorTag = errTag
valErr.Kind = valueField.Kind()
valErr.Kind = fieldKind
return valErr
}
if valErr, err = v.validateFieldByNameAndSingleTag(val, f, name, valTag); err != nil {
valErr.Kind = valueField.Kind()
valErr.Type = fieldType
return valErr
}

@ -2,6 +2,7 @@ package validator_test
import (
"fmt"
"reflect"
"testing"
"time"
@ -197,6 +198,7 @@ func (ms *MySuite) TestUnexposedStruct(c *C) {
A string `validate:"required"`
}
}
s := &Test{
Name: "TEST",
}
@ -290,6 +292,7 @@ func (ms *MySuite) TestIsGte(c *C) {
err = validator.ValidateFieldByTag(t2, "gte")
c.Assert(err, NotNil)
c.Assert(err.ErrorTag, Equals, "gte")
c.Assert(err.Type, Equals, reflect.TypeOf(time.Time{}))
type Test struct {
Now *time.Time `validate:"gte"`

Loading…
Cancel
Save