Merge pull request #5 from ilgooz/v8

merge
pull/189/head
İlker Göktuğ 9 years ago
commit 95a8867bee
  1. 47
      validator.go
  2. 18
      validator_test.go

@ -78,6 +78,7 @@ func (s *tagCacheMap) Set(key string, value *cachedTag) {
// Validate contains the validator settings passed in using the Config struct // Validate contains the validator settings passed in using the Config struct
type Validate struct { type Validate struct {
tagName string tagName string
fieldNameTag string
validationFuncs map[string]Func validationFuncs map[string]Func
customTypeFuncs map[reflect.Type]CustomTypeFunc customTypeFuncs map[reflect.Type]CustomTypeFunc
aliasValidators map[string]string aliasValidators map[string]string
@ -96,7 +97,8 @@ func (v *Validate) initCheck() {
// Config contains the options that a Validator instance will use. // Config contains the options that a Validator instance will use.
// It is passed to the New() function // It is passed to the New() function
type Config struct { type Config struct {
TagName string TagName string
FieldNameTag string
} }
// CustomTypeFunc allows for overriding or adding custom field type handler functions // CustomTypeFunc allows for overriding or adding custom field type handler functions
@ -137,6 +139,7 @@ func (ve ValidationErrors) Error() string {
// with other properties that may be needed for error message creation // with other properties that may be needed for error message creation
type FieldError struct { type FieldError struct {
Field string Field string
Name string
Tag string Tag string
ActualTag string ActualTag string
Kind reflect.Kind Kind reflect.Kind
@ -149,8 +152,9 @@ type FieldError struct {
func New(config *Config) *Validate { func New(config *Config) *Validate {
v := &Validate{ v := &Validate{
tagName: config.TagName, tagName: config.TagName,
tagsCache: &tagCacheMap{m: map[string]*cachedTag{}}, fieldNameTag: config.FieldNameTag,
tagsCache: &tagCacheMap{m: map[string]*cachedTag{}},
errsPool: &sync.Pool{New: func() interface{} { errsPool: &sync.Pool{New: func() interface{} {
return ValidationErrors{} return ValidationErrors{}
}}} }}}
@ -245,7 +249,7 @@ func (v *Validate) Field(field interface{}, tag string) error {
errs := v.errsPool.Get().(ValidationErrors) errs := v.errsPool.Get().(ValidationErrors)
fieldVal := reflect.ValueOf(field) fieldVal := reflect.ValueOf(field)
v.traverseField(fieldVal, fieldVal, fieldVal, blank, errs, false, tag, blank, false, false, nil) v.traverseField(fieldVal, fieldVal, fieldVal, blank, errs, false, tag, blank, blank, false, false, nil)
if len(errs) == 0 { if len(errs) == 0 {
v.errsPool.Put(errs) v.errsPool.Put(errs)
@ -265,7 +269,7 @@ func (v *Validate) FieldWithValue(val interface{}, field interface{}, tag string
errs := v.errsPool.Get().(ValidationErrors) errs := v.errsPool.Get().(ValidationErrors)
topVal := reflect.ValueOf(val) topVal := reflect.ValueOf(val)
v.traverseField(topVal, topVal, reflect.ValueOf(field), blank, errs, false, tag, blank, false, false, nil) v.traverseField(topVal, topVal, reflect.ValueOf(field), blank, errs, false, tag, blank, blank, false, false, nil)
if len(errs) == 0 { if len(errs) == 0 {
v.errsPool.Put(errs) v.errsPool.Put(errs)
@ -417,12 +421,20 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec
} }
} }
v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, errs, true, fld.Tag.Get(v.tagName), fld.Name, partial, exclude, includeExclude) customName := fld.Name
if v.fieldNameTag != "" {
name := strings.Split(fld.Tag.Get(v.fieldNameTag), ",")[0]
if name != "" {
customName = name
}
}
v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, errs, true, fld.Tag.Get(v.tagName), fld.Name, customName, partial, exclude, includeExclude)
} }
} }
// traverseField validates any field, be it a struct or single field, ensures it's validity and passes it along to be validated via it's tag options // traverseField validates any field, be it a struct or single field, ensures it's validity and passes it along to be validated via it's tag options
func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, isStructField bool, tag string, name string, partial bool, exclude bool, includeExclude map[string]*struct{}) { func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, isStructField bool, tag, name, customName string, partial bool, exclude bool, includeExclude map[string]*struct{}) {
if tag == skipValidationTag { if tag == skipValidationTag {
return return
@ -448,6 +460,7 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.
if kind == reflect.Invalid { if kind == reflect.Invalid {
errs[errPrefix+name] = &FieldError{ errs[errPrefix+name] = &FieldError{
Name: customName,
Field: name, Field: name,
Tag: cTag.tags[0].tag, Tag: cTag.tags[0].tag,
ActualTag: cTag.tags[0].tagVals[0][0], ActualTag: cTag.tags[0].tagVals[0][0],
@ -458,6 +471,7 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.
} }
errs[errPrefix+name] = &FieldError{ errs[errPrefix+name] = &FieldError{
Name: customName,
Field: name, Field: name,
Tag: cTag.tags[0].tag, Tag: cTag.tags[0].tag,
ActualTag: cTag.tags[0].tagVals[0][0], ActualTag: cTag.tags[0].tagVals[0][0],
@ -520,7 +534,7 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.
continue continue
} }
if v.validateField(topStruct, currentStruct, current, typ, kind, errPrefix, errs, valTag, name) { if v.validateField(topStruct, currentStruct, current, typ, kind, errPrefix, errs, valTag, name, customName) {
return return
} }
} }
@ -530,9 +544,9 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.
// or panic ;) // or panic ;)
switch kind { switch kind {
case reflect.Slice, reflect.Array: case reflect.Slice, reflect.Array:
v.traverseSlice(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name, partial, exclude, includeExclude) v.traverseSlice(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name, customName, partial, exclude, includeExclude)
case reflect.Map: case reflect.Map:
v.traverseMap(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name, partial, exclude, includeExclude) v.traverseMap(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name, customName, partial, exclude, includeExclude)
default: default:
// throw error, if not a slice or map then should not have gotten here // throw error, if not a slice or map then should not have gotten here
// bad dive tag // bad dive tag
@ -542,23 +556,23 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.
} }
// traverseSlice traverses a Slice or Array's elements and passes them to traverseField for validation // traverseSlice traverses a Slice or Array's elements and passes them to traverseField for validation
func (v *Validate) traverseSlice(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, tag string, name string, partial bool, exclude bool, includeExclude map[string]*struct{}) { func (v *Validate) traverseSlice(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, tag, name, customName string, partial bool, exclude bool, includeExclude map[string]*struct{}) {
for i := 0; i < current.Len(); i++ { for i := 0; i < current.Len(); i++ {
v.traverseField(topStruct, currentStruct, current.Index(i), errPrefix, errs, false, tag, fmt.Sprintf(arrayIndexFieldName, name, i), partial, exclude, includeExclude) v.traverseField(topStruct, currentStruct, current.Index(i), errPrefix, errs, false, tag, fmt.Sprintf(arrayIndexFieldName, name, i), fmt.Sprintf(arrayIndexFieldName, customName, i), partial, exclude, includeExclude)
} }
} }
// traverseMap traverses a map's elements and passes them to traverseField for validation // traverseMap traverses a map's elements and passes them to traverseField for validation
func (v *Validate) traverseMap(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, tag string, name string, partial bool, exclude bool, includeExclude map[string]*struct{}) { func (v *Validate) traverseMap(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, tag, name, customName string, partial bool, exclude bool, includeExclude map[string]*struct{}) {
for _, key := range current.MapKeys() { for _, key := range current.MapKeys() {
v.traverseField(topStruct, currentStruct, current.MapIndex(key), errPrefix, errs, false, tag, fmt.Sprintf(mapIndexFieldName, name, key.Interface()), partial, exclude, includeExclude) v.traverseField(topStruct, currentStruct, current.MapIndex(key), errPrefix, errs, false, tag, fmt.Sprintf(mapIndexFieldName, name, key.Interface()), fmt.Sprintf(mapIndexFieldName, customName, key.Interface()), partial, exclude, includeExclude)
} }
} }
// validateField validates a field based on the provided tag's key and param values and returns true if there is an error or false if all ok // validateField validates a field based on the provided tag's key and param values and returns true if there is an error or false if all ok
func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, currentType reflect.Type, currentKind reflect.Kind, errPrefix string, errs ValidationErrors, valTag *tagVals, name string) bool { func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, currentType reflect.Type, currentKind reflect.Kind, errPrefix string, errs ValidationErrors, valTag *tagVals, name, customName string) bool {
var valFunc Func var valFunc Func
var ok bool var ok bool
@ -583,6 +597,7 @@ func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect.
if valTag.isAlias { if valTag.isAlias {
errs[errPrefix+name] = &FieldError{ errs[errPrefix+name] = &FieldError{
Name: customName,
Field: name, Field: name,
Tag: valTag.tag, Tag: valTag.tag,
ActualTag: errTag[1:], ActualTag: errTag[1:],
@ -592,6 +607,7 @@ func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect.
} }
} else { } else {
errs[errPrefix+name] = &FieldError{ errs[errPrefix+name] = &FieldError{
Name: customName,
Field: name, Field: name,
Tag: errTag[1:], Tag: errTag[1:],
ActualTag: errTag[1:], ActualTag: errTag[1:],
@ -614,6 +630,7 @@ func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect.
} }
errs[errPrefix+name] = &FieldError{ errs[errPrefix+name] = &FieldError{
Name: customName,
Field: name, Field: name,
Tag: valTag.tag, Tag: valTag.tag,
ActualTag: valTag.tagVals[0][0], ActualTag: valTag.tagVals[0][0],

@ -4930,3 +4930,21 @@ func TestInvalidValidatorFunction(t *testing.T) {
PanicMatches(t, func() { validate.Field(s.Test, "zzxxBadFunction") }, "Undefined validation function on field") PanicMatches(t, func() { validate.Field(s.Test, "zzxxBadFunction") }, "Undefined validation function on field")
} }
func TestCustomFieldName(t *testing.T) {
type A struct {
B string `schema:"b" validate:"required"`
C string `schema:"c" validate:"required"`
D []bool `schema:"d" validate:"required"`
}
a := &A{}
errs := New(&Config{TagName: "validate", FieldNameTag: "schema"}).Struct(a).(ValidationErrors)
Equal(t, errs["A.B"].Name, "b")
Equal(t, errs["A.C"].Name, "c")
Equal(t, errs["A.D"].Name, "d")
errs = New(&Config{TagName: "validate"}).Struct(a).(ValidationErrors)
Equal(t, errs["A.B"].Name, "B")
}

Loading…
Cancel
Save