configurable field name support

pull/187/head
İlker Göktuğ Öztürk 9 years ago
parent 92ddaeb7d8
commit 2b8758ad05
  1. 17
      util.go
  2. 42
      validator.go
  3. 14
      validator_test.go

@ -240,15 +240,20 @@ func panicIf(err error) {
}
}
func (v *Validate) parseTags(tag, fieldName string) *cachedTag {
func (v *Validate) parseTags(tag, name, fieldNameTag string) *cachedTag {
cTag := &cachedTag{}
fieldName := strings.Split(fieldNameTag, ",")[0]
if fieldName == "" {
fieldName = name
}
cTag := &cachedTag{name: fieldName}
v.parseTagsRecursive(cTag, tag, fieldName, blank, false)
v.parseTagsRecursive(cTag, tag, name, blank, false)
return cTag
}
func (v *Validate) parseTagsRecursive(cTag *cachedTag, tag, fieldName, alias string, isAlias bool) bool {
func (v *Validate) parseTagsRecursive(cTag *cachedTag, tag, name, alias string, isAlias bool) bool {
if len(tag) == 0 {
return true
@ -260,7 +265,7 @@ func (v *Validate) parseTagsRecursive(cTag *cachedTag, tag, fieldName, alias str
// check map for alias and process new tags, otherwise process as usual
if tagsVal, ok := v.aliasValidators[t]; ok {
leave := v.parseTagsRecursive(cTag, tagsVal, fieldName, t, true)
leave := v.parseTagsRecursive(cTag, tagsVal, name, t, true)
if leave {
return leave
@ -300,7 +305,7 @@ func (v *Validate) parseTagsRecursive(cTag *cachedTag, tag, fieldName, alias str
}
if len(key) == 0 {
panic(strings.TrimSpace(fmt.Sprintf(invalidValidation, fieldName)))
panic(strings.TrimSpace(fmt.Sprintf(invalidValidation, name)))
}
if len(vals) > 1 {

@ -45,6 +45,7 @@ var (
)
type cachedTag struct {
name string
isOmitEmpty bool
diveTag string
tags []*tagVals
@ -78,6 +79,7 @@ func (s *tagCacheMap) Set(key string, value *cachedTag) {
// Validate contains the validator settings passed in using the Config struct
type Validate struct {
tagName string
fieldNameTagName string
validationFuncs map[string]Func
customTypeFuncs map[reflect.Type]CustomTypeFunc
aliasValidators map[string]string
@ -96,7 +98,8 @@ func (v *Validate) initCheck() {
// Config contains the options that a Validator instance will use.
// It is passed to the New() function
type Config struct {
TagName string
TagName string
FieldNameTagName string
}
// CustomTypeFunc allows for overriding or adding custom field type handler functions
@ -137,6 +140,7 @@ func (ve ValidationErrors) Error() string {
// with other properties that may be needed for error message creation
type FieldError struct {
Field string
FieldName string
Tag string
ActualTag string
Kind reflect.Kind
@ -149,8 +153,9 @@ type FieldError struct {
func New(config *Config) *Validate {
v := &Validate{
tagName: config.TagName,
tagsCache: &tagCacheMap{m: map[string]*cachedTag{}},
tagName: config.TagName,
fieldNameTagName: config.FieldNameTagName,
tagsCache: &tagCacheMap{m: map[string]*cachedTag{}},
errsPool: &sync.Pool{New: func() interface{} {
return ValidationErrors{}
}}}
@ -245,7 +250,7 @@ func (v *Validate) Field(field interface{}, tag string) error {
errs := v.errsPool.Get().(ValidationErrors)
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 {
v.errsPool.Put(errs)
@ -265,7 +270,7 @@ func (v *Validate) FieldWithValue(val interface{}, field interface{}, tag string
errs := v.errsPool.Get().(ValidationErrors)
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 {
v.errsPool.Put(errs)
@ -417,12 +422,12 @@ 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)
v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, errs, true, fld.Tag.Get(v.tagName), fld.Name, fld.Tag.Get(v.fieldNameTagName), 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
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, fieldNameTag string, partial bool, exclude bool, includeExclude map[string]*struct{}) {
if tag == skipValidationTag {
return
@ -431,7 +436,7 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.
cTag, isCached := v.tagsCache.Get(tag)
if !isCached {
cTag = v.parseTags(tag, name)
cTag = v.parseTags(tag, name, fieldNameTag)
v.tagsCache.Set(tag, cTag)
}
@ -449,6 +454,7 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.
if kind == reflect.Invalid {
errs[errPrefix+name] = &FieldError{
Field: name,
FieldName: cTag.name,
Tag: cTag.tags[0].tag,
ActualTag: cTag.tags[0].tagVals[0][0],
Param: cTag.tags[0].tagVals[0][1],
@ -459,6 +465,7 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.
errs[errPrefix+name] = &FieldError{
Field: name,
FieldName: cTag.name,
Tag: cTag.tags[0].tag,
ActualTag: cTag.tags[0].tagVals[0][0],
Param: cTag.tags[0].tagVals[0][1],
@ -520,7 +527,7 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.
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, cTag.name) {
return
}
}
@ -530,9 +537,9 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.
// or panic ;)
switch kind {
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, fieldNameTag, partial, exclude, includeExclude)
case reflect.Map:
v.traverseMap(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name, partial, exclude, includeExclude)
v.traverseMap(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name, fieldNameTag, partial, exclude, includeExclude)
default:
// throw error, if not a slice or map then should not have gotten here
// bad dive tag
@ -542,23 +549,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
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, fieldNameTag string, partial bool, exclude bool, includeExclude map[string]*struct{}) {
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), fieldNameTag, partial, exclude, includeExclude)
}
}
// 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, fieldNameTag string, partial bool, exclude bool, includeExclude map[string]*struct{}) {
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()), fieldNameTag, 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
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, fieldName string) bool {
var valFunc Func
var ok bool
@ -584,6 +591,7 @@ func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect.
if valTag.isAlias {
errs[errPrefix+name] = &FieldError{
Field: name,
FieldName: fieldName,
Tag: valTag.tag,
ActualTag: errTag[1:],
Value: current.Interface(),
@ -593,6 +601,7 @@ func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect.
} else {
errs[errPrefix+name] = &FieldError{
Field: name,
FieldName: fieldName,
Tag: errTag[1:],
ActualTag: errTag[1:],
Value: current.Interface(),
@ -615,6 +624,7 @@ func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect.
errs[errPrefix+name] = &FieldError{
Field: name,
FieldName: fieldName,
Tag: valTag.tag,
ActualTag: valTag.tagVals[0][0],
Value: current.Interface(),

@ -4930,3 +4930,17 @@ func TestInvalidValidatorFunction(t *testing.T) {
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:"min=3"`
}
a := &A{}
errs := New(&Config{TagName: "validate", FieldNameTagName: "schema"}).Struct(a).(ValidationErrors)
Equal(t, errs["A.B"].FieldName, "b")
errs = New(&Config{TagName: "validate"}).Struct(a).(ValidationErrors)
Equal(t, errs["A.B"].FieldName, "B")
}

Loading…
Cancel
Save