Add gtcsfield, gtecsfield, ltcsfield and ltecsfield

pull/161/head
joeybloggs 9 years ago
parent cd50c5e085
commit 327aa2e826
  1. 166
      baked_in.go
  2. 4
      validator_test.go

@ -254,6 +254,170 @@ func isNe(v *Validate, topStruct reflect.Value, currentStruct reflect.Value, fie
return !isEq(v, topStruct, currentStruct, field, fieldType, fieldKind, param) return !isEq(v, topStruct, currentStruct, field, fieldType, fieldKind, param)
} }
func isLteCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
topField, topKind, ok := v.getStructFieldOK(topStruct, param)
if !ok || topKind != fieldKind {
return false
}
switch fieldKind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return field.Int() <= topField.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return field.Uint() <= topField.Uint()
case reflect.Float32, reflect.Float64:
return field.Float() <= topField.Float()
case reflect.Slice, reflect.Map, reflect.Array:
return int64(field.Len()) <= int64(topField.Len())
case reflect.Struct:
// Not Same underlying type i.e. struct and time
if fieldType != topField.Type() {
return false
}
if fieldType == timeType {
fieldTime := field.Interface().(time.Time)
topTime := topField.Interface().(time.Time)
return fieldTime.Before(topTime) || fieldTime.Equal(topTime)
}
}
// default reflect.String:
return topField.String() <= field.String()
}
func isLtCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
topField, topKind, ok := v.getStructFieldOK(topStruct, param)
if !ok || topKind != fieldKind {
return false
}
switch fieldKind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return field.Int() < topField.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return field.Uint() < topField.Uint()
case reflect.Float32, reflect.Float64:
return field.Float() < topField.Float()
case reflect.Slice, reflect.Map, reflect.Array:
return int64(field.Len()) < int64(topField.Len())
case reflect.Struct:
// Not Same underlying type i.e. struct and time
if fieldType != topField.Type() {
return false
}
if fieldType == timeType {
fieldTime := field.Interface().(time.Time)
topTime := topField.Interface().(time.Time)
return fieldTime.Before(topTime)
}
}
// default reflect.String:
return topField.String() < field.String()
}
func isGteCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
topField, topKind, ok := v.getStructFieldOK(topStruct, param)
if !ok || topKind != fieldKind {
return false
}
switch fieldKind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return field.Int() >= topField.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return field.Uint() >= topField.Uint()
case reflect.Float32, reflect.Float64:
return field.Float() >= topField.Float()
case reflect.Slice, reflect.Map, reflect.Array:
return int64(field.Len()) >= int64(topField.Len())
case reflect.Struct:
// Not Same underlying type i.e. struct and time
if fieldType != topField.Type() {
return false
}
if fieldType == timeType {
fieldTime := field.Interface().(time.Time)
topTime := topField.Interface().(time.Time)
return fieldTime.After(topTime) || fieldTime.Equal(topTime)
}
}
// default reflect.String:
return topField.String() >= field.String()
}
func isGtCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
topField, topKind, ok := v.getStructFieldOK(topStruct, param)
if !ok || topKind != fieldKind {
return false
}
switch fieldKind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return field.Int() > topField.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return field.Uint() > topField.Uint()
case reflect.Float32, reflect.Float64:
return field.Float() > topField.Float()
case reflect.Slice, reflect.Map, reflect.Array:
return int64(field.Len()) > int64(topField.Len())
case reflect.Struct:
// Not Same underlying type i.e. struct and time
if fieldType != topField.Type() {
return false
}
if fieldType == timeType {
fieldTime := field.Interface().(time.Time)
topTime := topField.Interface().(time.Time)
return fieldTime.After(topTime)
}
}
// default reflect.String:
return topField.String() > field.String()
}
func isNeCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool { func isNeCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
return !isEqCrossStructField(v, topStruct, current, field, fieldType, fieldKind, param) return !isEqCrossStructField(v, topStruct, current, field, fieldType, fieldKind, param)
@ -297,7 +461,7 @@ func isEqCrossStructField(v *Validate, topStruct reflect.Value, current reflect.
} }
// default reflect.String: // default reflect.String:
return topField.String() == current.String() return topField.String() == field.String()
} }
func isEqField(v *Validate, topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool { func isEqField(v *Validate, topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {

@ -224,16 +224,12 @@ func TestCrossStructNeFieldValidation(t *testing.T) {
NotEqual(t, errs, nil) NotEqual(t, errs, nil)
AssertError(t, errs, "Test.CreatedAt", "CreatedAt", "necsfield") AssertError(t, errs, "Test.CreatedAt", "CreatedAt", "necsfield")
// var j uint64
// var k float64
s := "abcd" s := "abcd"
i := 1 i := 1
j := 1 j := 1
k := 1.543 k := 1.543
arr := []string{"test"} arr := []string{"test"}
// var j2 uint64
// var k2 float64
s2 := "abcd" s2 := "abcd"
i2 := 1 i2 := 1
j2 := 1 j2 := 1

Loading…
Cancel
Save