Merge branch 'v2-development' into v2

pull/16/head v2.2
Dean Karn 10 years ago
commit bb80e920d4
  1. 20
      validator.go
  2. 3
      validator_test.go

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

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

Loading…
Cancel
Save