|
|
|
@ -16,7 +16,6 @@ import ( |
|
|
|
|
"strings" |
|
|
|
|
"sync" |
|
|
|
|
"time" |
|
|
|
|
"unicode" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
@ -196,7 +195,7 @@ func New(config *Config) *Validate { |
|
|
|
|
tagName: config.TagName, |
|
|
|
|
fieldNameTag: config.FieldNameTag, |
|
|
|
|
tagCache: &tagCacheMap{m: map[string]*cachedTag{}}, |
|
|
|
|
structCache: &structCacheMap{m: map[string]*cachedStruct{}}, |
|
|
|
|
structCache: &structCacheMap{m: map[reflect.Type]*cachedStruct{}}, |
|
|
|
|
errsPool: &sync.Pool{New: func() interface{} { |
|
|
|
|
return ValidationErrors{} |
|
|
|
|
}}} |
|
|
|
@ -307,7 +306,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, blank, false, false, nil) |
|
|
|
|
v.traverseField(fieldVal, fieldVal, fieldVal, blank, errs, false, tag, blank, blank, false, false, nil, nil) |
|
|
|
|
|
|
|
|
|
if len(errs) == 0 { |
|
|
|
|
v.errsPool.Put(errs) |
|
|
|
@ -327,7 +326,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, blank, false, false, nil) |
|
|
|
|
v.traverseField(topVal, topVal, reflect.ValueOf(field), blank, errs, false, tag, blank, blank, false, false, nil, nil) |
|
|
|
|
|
|
|
|
|
if len(errs) == 0 { |
|
|
|
|
v.errsPool.Put(errs) |
|
|
|
@ -452,26 +451,35 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec |
|
|
|
|
panic("value passed for validation is not a struct") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var ok bool |
|
|
|
|
// var ok bool
|
|
|
|
|
typ := current.Type() |
|
|
|
|
|
|
|
|
|
sName := typ.Name() |
|
|
|
|
|
|
|
|
|
if useStructName { |
|
|
|
|
errPrefix += typ.Name() + "." |
|
|
|
|
errPrefix += sName + "." |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// structonly tag present don't tranverseFields
|
|
|
|
|
// but must still check and run below struct level validation
|
|
|
|
|
// if present
|
|
|
|
|
if !isStructOnly { |
|
|
|
|
numFields := current.NumField() |
|
|
|
|
|
|
|
|
|
var fld reflect.StructField |
|
|
|
|
|
|
|
|
|
// is anonymous struct, cannot parse or cache as
|
|
|
|
|
// it has no name to index by
|
|
|
|
|
if len(sName) == 0 { |
|
|
|
|
|
|
|
|
|
var customName string |
|
|
|
|
var ok bool |
|
|
|
|
numFields := current.NumField() |
|
|
|
|
|
|
|
|
|
for i := 0; i < numFields; i++ { |
|
|
|
|
|
|
|
|
|
fld = typ.Field(i) |
|
|
|
|
|
|
|
|
|
if !unicode.IsUpper(rune(fld.Name[0])) { |
|
|
|
|
if len(fld.PkgPath) != 0 { |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -495,7 +503,28 @@ 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, customName, partial, exclude, includeExclude) |
|
|
|
|
v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, errs, true, fld.Tag.Get(v.tagName), fld.Name, customName, partial, exclude, includeExclude, nil) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
s, ok := v.structCache.Get(typ) |
|
|
|
|
if !ok { |
|
|
|
|
s = v.parseStruct(current, sName) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for i, f := range s.fields { |
|
|
|
|
|
|
|
|
|
if partial { |
|
|
|
|
|
|
|
|
|
_, ok = includeExclude[errPrefix+f.Name] |
|
|
|
|
|
|
|
|
|
if (ok && exclude) || (!ok && !exclude) { |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
fld = typ.Field(i) |
|
|
|
|
|
|
|
|
|
v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, errs, true, f.CachedTag.tag, fld.Name, f.AltName, partial, exclude, includeExclude, f.CachedTag) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -508,17 +537,20 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 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, name, customName 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{}, cTag *cachedTag) { |
|
|
|
|
|
|
|
|
|
if tag == skipValidationTag { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cTag, isCached := v.tagCache.Get(tag) |
|
|
|
|
if cTag == nil { |
|
|
|
|
var isCached bool |
|
|
|
|
cTag, isCached = v.tagCache.Get(tag) |
|
|
|
|
|
|
|
|
|
if !isCached { |
|
|
|
|
cTag = v.parseTags(tag, name) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
current, kind := v.ExtractType(current) |
|
|
|
|
var typ reflect.Type |
|
|
|
@ -615,9 +647,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, customName, partial, exclude, includeExclude) |
|
|
|
|
v.traverseSlice(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name, customName, partial, exclude, includeExclude, nil) |
|
|
|
|
case reflect.Map: |
|
|
|
|
v.traverseMap(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name, customName, partial, exclude, includeExclude) |
|
|
|
|
v.traverseMap(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name, customName, partial, exclude, includeExclude, nil) |
|
|
|
|
default: |
|
|
|
|
// throw error, if not a slice or map then should not have gotten here
|
|
|
|
|
// bad dive tag
|
|
|
|
@ -627,18 +659,18 @@ 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, name, customName 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{}, cTag *cachedTag) { |
|
|
|
|
|
|
|
|
|
for i := 0; i < current.Len(); i++ { |
|
|
|
|
v.traverseField(topStruct, currentStruct, current.Index(i), errPrefix, errs, false, tag, fmt.Sprintf(arrayIndexFieldName, name, i), fmt.Sprintf(arrayIndexFieldName, customName, 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, cTag) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 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, name, customName 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{}, cTag *cachedTag) { |
|
|
|
|
|
|
|
|
|
for _, key := range current.MapKeys() { |
|
|
|
|
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) |
|
|
|
|
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, cTag) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|