@ -306,7 +306,7 @@ func (v *Validate) structRecursive(top interface{}, current interface{}, s inter
cField . isTime = true
cField . isTime = true
if fieldError := v . fieldWithNameAndValue ( top , current , valueField . Interface ( ) , cField . tag , cField . name , cField ) ; fieldError != nil {
if fieldError := v . fieldWithNameAndValue ( top , current , valueField . Interface ( ) , cField . tag , cField . name , false , cField ) ; fieldError != nil {
validationErrors . Errors [ fieldError . Field ] = fieldError
validationErrors . Errors [ fieldError . Field ] = fieldError
// free up memory reference
// free up memory reference
fieldError = nil
fieldError = nil
@ -331,7 +331,7 @@ func (v *Validate) structRecursive(top interface{}, current interface{}, s inter
default :
default :
if fieldError := v . fieldWithNameAndValue ( top , current , valueField . Interface ( ) , cField . tag , cField . name , cField ) ; fieldError != nil {
if fieldError := v . fieldWithNameAndValue ( top , current , valueField . Interface ( ) , cField . tag , cField . name , false , cField ) ; fieldError != nil {
validationErrors . Errors [ fieldError . Field ] = fieldError
validationErrors . Errors [ fieldError . Field ] = fieldError
// free up memory reference
// free up memory reference
fieldError = nil
fieldError = nil
@ -361,14 +361,17 @@ func (v *Validate) Field(f interface{}, tag string) *FieldError {
// FieldWithValue allows validation of a single field, possibly even against another fields value, still using tag style validation to check multiple errors
// FieldWithValue allows validation of a single field, possibly even against another fields value, still using tag style validation to check multiple errors
func ( v * Validate ) FieldWithValue ( val interface { } , f interface { } , tag string ) * FieldError {
func ( v * Validate ) FieldWithValue ( val interface { } , f interface { } , tag string ) * FieldError {
return v . fieldWithNameAndValue ( nil , val , f , tag , "" , nil )
return v . fieldWithNameAndValue ( nil , val , f , tag , "" , true , nil )
}
}
func ( v * Validate ) fieldWithNameAndValue ( val interface { } , current interface { } , f interface { } , tag string , name string , cacheField * cachedField ) * FieldError {
var cacheFields = map [ string ] [ ] * cacheTags { }
func ( v * Validate ) fieldWithNameAndValue ( val interface { } , current interface { } , f interface { } , tag string , name string , isSingleField bool , cacheField * cachedField ) * FieldError {
// var fieldType reflect.Type
// var fieldType reflect.Type
// var fieldKind reflect.Kind
// var fieldKind reflect.Kind
var cField * cachedField
var cField * cachedField
var ok bool
// This is a double check if coming from validate.Struct but need to be here in case function is called directly
// This is a double check if coming from validate.Struct but need to be here in case function is called directly
if tag == noValidationTag {
if tag == noValidationTag {
@ -382,14 +385,30 @@ func (v *Validate) fieldWithNameAndValue(val interface{}, current interface{}, f
if cacheField == nil {
if cacheField == nil {
valueField := reflect . ValueOf ( f )
valueField := reflect . ValueOf ( f )
cField = & cachedField { name : name , kind : valueField . Kind ( ) }
if valueField . Kind ( ) == reflect . Ptr && ! valueField . IsNil ( ) {
// valueField = valueField.Elem()
// f = valueField.Interface()
return v . fieldWithNameAndValue ( val , current , valueField . Elem ( ) . Interface ( ) , tag , name , isSingleField , cacheField )
}
// if !ok {
// cacheFields[cField.tag] = cField
// valueField = valueField.Elem()
cField = & cachedField { name : name , kind : valueField . Kind ( ) , tag : tag , typ : valueField . Type ( ) }
// fieldKind = valueField.Kind()
// fieldKind = valueField.Kind()
if cField . kind == reflect . Ptr && ! valueField . IsNil ( ) {
// if cField.kind == reflect.Ptr && !valueField.IsNil() {
return v . fieldWithNameAndValue ( val , current , valueField . Elem ( ) . Interface ( ) , tag , name , cacheField )
// return v.fieldWithNameAndValue(val, current, valueField.Elem().Interface(), tag, name, isSingleField, cacheField)
}
// }
cField . typ = valueField . Type ( )
// cField.typ = valueField.Type()
// cField.tag = tag
// if isSingleField {
// cacheFields[cField.tag] = cField
// }
// cField.tags = make([][]string, 0)
// cField.tags = make([][]string, 0)
// fieldType = valueField.Type()
// fieldType = valueField.Type()
// for _, t := range strings.Split(tag, tagSeparator) {
// for _, t := range strings.Split(tag, tagSeparator) {
@ -413,6 +432,7 @@ func (v *Validate) fieldWithNameAndValue(val interface{}, current interface{}, f
// // vals := strings.Split(valTag, tagKeySeparator)
// // vals := strings.Split(valTag, tagKeySeparator)
// // key := strings.TrimSpace(vals[0])
// // key := strings.TrimSpace(vals[0])
// }
// }
// }
} else {
} else {
cField = cacheField
cField = cacheField
// fieldType = cacheField.typ
// fieldType = cacheField.typ
@ -429,6 +449,13 @@ func (v *Validate) fieldWithNameAndValue(val interface{}, current interface{}, f
}
}
if len ( cField . tags ) == 0 {
if len ( cField . tags ) == 0 {
if isSingleField {
cField . tags , ok = cacheFields [ tag ]
}
if ! ok {
for _ , t := range strings . Split ( tag , tagSeparator ) {
for _ , t := range strings . Split ( tag , tagSeparator ) {
orVals := strings . Split ( t , orSeparator )
orVals := strings . Split ( t , orSeparator )
@ -463,6 +490,12 @@ func (v *Validate) fieldWithNameAndValue(val interface{}, current interface{}, f
// vals := strings.Split(valTag, tagKeySeparator)
// vals := strings.Split(valTag, tagKeySeparator)
// key := strings.TrimSpace(vals[0])
// key := strings.TrimSpace(vals[0])
}
}
if isSingleField && ! ok {
// fmt.Println(cField.tag)
cacheFields [ cField . tag ] = cField . tags
}
}
}
}
// fmt.Println(fieldKind, cacheField.kind)
// fmt.Println(fieldKind, cacheField.kind)