complete test coverage for IsGteField after conversion

pull/161/head
joeybloggs 9 years ago
parent 8aea478060
commit 5989727cf7
  1. 92
      baked_in.go
  2. 10
      validator_test.go

@ -460,109 +460,47 @@ func hasValue(v *Validate, topStruct reflect.Value, currentStruct reflect.Value,
}
}
func isGteField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
func isGteField(v *Validate, topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
if !current.IsValid() {
panic("struct not passed for cross validation")
}
if current.Kind() == reflect.Ptr && !current.IsNil() {
current = current.Elem()
}
switch current.Kind() {
case reflect.Struct:
if current.Type() == timeType || current.Type() == timePtrType {
break
}
current = current.FieldByName(param)
if current.Kind() == reflect.Invalid {
panic(fmt.Sprintf("Field \"%s\" not found in struct", param))
}
}
if current.Kind() == reflect.Ptr && !current.IsNil() {
current = current.Elem()
currentField, currentKind, ok := v.getStructFieldOK(currentStruct, param)
if !ok || currentKind != fieldKind {
return false
}
switch fieldKind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return field.Int() >= current.Int()
return field.Int() >= currentField.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return field.Uint() >= current.Uint()
return field.Uint() >= currentField.Uint()
case reflect.Float32, reflect.Float64:
return field.Float() >= current.Float()
return field.Float() >= currentField.Float()
case reflect.Struct:
if field.Type() == timeType || field.Type() == timePtrType {
// Not Same underlying type i.e. struct and time
if fieldType != currentField.Type() {
return false
}
if current.Type() != timeType && current.Type() != timePtrType {
panic("Bad Top Level field type")
}
if fieldType == timeType {
t := current.Interface().(time.Time)
t := currentField.Interface().(time.Time)
fieldTime := field.Interface().(time.Time)
return fieldTime.After(t) || fieldTime.Equal(t)
}
}
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
// default reflect.String
return len(field.String()) >= len(currentField.String())
}
// func isEqField(v *Validate, topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
// currentField, currentKind, ok := v.getStructFieldOK(currentStruct, param)
// if !ok || currentKind != fieldKind {
// return false
// }
// switch fieldKind {
// case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
// return field.Int() == currentField.Int()
// case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
// return field.Uint() == currentField.Uint()
// case reflect.Float32, reflect.Float64:
// return field.Float() == currentField.Float()
// case reflect.Slice, reflect.Map, reflect.Array:
// return int64(field.Len()) == int64(currentField.Len())
// case reflect.Struct:
// // Not Same underlying type i.e. struct and time
// if fieldType != currentField.Type() {
// return false
// }
// if fieldType == timeType {
// t := currentField.Interface().(time.Time)
// fieldTime := field.Interface().(time.Time)
// return fieldTime.Equal(t)
// }
// }
// // default reflect.String:
// return field.String() == currentField.String()
// }
func isGtField(v *Validate, topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
currentField, currentKind, ok := v.getStructFieldOK(currentStruct, param)

@ -2882,6 +2882,16 @@ func TestGteField(t *testing.T) {
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "gtefield")
errs = validate.FieldWithValue(timeTest, &start, "gtefield")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "gtefield")
errs = validate.FieldWithValue("test", "test", "gtefield")
Equal(t, errs, nil)
errs = validate.FieldWithValue("test", "test bigger", "gtefield")
Equal(t, errs, nil)
type IntTest struct {
Val1 int `validate:"required"`
Val2 int `validate:"required,gtefield=Val1"`

Loading…
Cancel
Save