add ne and nefield functions to validator

pull/39/head
joeybloggs 10 years ago
parent 74d0e52ab3
commit 23c3fa6b63
  1. 10
      baked_in.go
  2. 116
      validator_test.go

@ -17,11 +17,13 @@ var BakedInValidators = map[string]Func{
"min": hasMinOf, "min": hasMinOf,
"max": hasMaxOf, "max": hasMaxOf,
"eq": isEq, "eq": isEq,
"ne": isNe,
"lt": isLt, "lt": isLt,
"lte": isLte, "lte": isLte,
"gt": isGt, "gt": isGt,
"gte": isGte, "gte": isGte,
"eqfield": isEqField, "eqfield": isEqField,
"nefield": isNeField,
"gtefield": isGteField, "gtefield": isGteField,
"gtfield": isGtField, "gtfield": isGtField,
"ltefield": isLteField, "ltefield": isLteField,
@ -42,6 +44,14 @@ var BakedInValidators = map[string]Func{
"base64": isBase64, "base64": isBase64,
} }
func isNeField(top interface{}, current interface{}, field interface{}, param string) bool {
return !isEqField(top, current, field, param)
}
func isNe(top interface{}, current interface{}, field interface{}, param string) bool {
return !isEq(top, current, field, param)
}
func isEqField(top interface{}, current interface{}, field interface{}, param string) bool { func isEqField(top interface{}, current interface{}, field interface{}, param string) bool {
if current == nil { if current == nil {

@ -142,6 +142,122 @@ func isEqualFunc(val interface{}, current interface{}, field interface{}, param
return current.(string) == field.(string) return current.(string) == field.(string)
} }
func (ms *MySuite) TestIsNeFieldValidation(c *C) {
var j uint64
var k float64
s := "abcd"
i := 1
j = 1
k = 1.543
arr := []string{"test"}
now := time.Now().UTC()
var j2 uint64
var k2 float64
s2 := "abcdef"
i2 := 3
j2 = 2
k2 = 1.5434456
arr2 := []string{"test", "test2"}
arr3 := []string{"test"}
now2 := now
err := validate.FieldWithValue(s, s2, "nefield")
c.Assert(err, IsNil)
err = validate.FieldWithValue(i2, i, "nefield")
c.Assert(err, IsNil)
err = validate.FieldWithValue(j2, j, "nefield")
c.Assert(err, IsNil)
err = validate.FieldWithValue(k2, k, "nefield")
c.Assert(err, IsNil)
err = validate.FieldWithValue(arr2, arr, "nefield")
c.Assert(err, IsNil)
err = validate.FieldWithValue(now2, now, "nefield")
c.Assert(err, NotNil)
err = validate.FieldWithValue(arr3, arr, "nefield")
c.Assert(err, NotNil)
type Test struct {
Start *time.Time `validate:"nefield=End"`
End *time.Time
}
sv := &Test{
Start: &now,
End: &now,
}
errs := validate.Struct(sv)
c.Assert(errs, NotNil)
now3 := time.Now().UTC()
sv = &Test{
Start: &now,
End: &now3,
}
errs = validate.Struct(sv)
c.Assert(errs, IsNil)
channel := make(chan string)
c.Assert(func() { validate.FieldWithValue(nil, 1, "nefield") }, PanicMatches, "struct not passed for cross validation")
c.Assert(func() { validate.FieldWithValue(5, channel, "nefield") }, PanicMatches, "Bad field type chan string")
c.Assert(func() { validate.FieldWithValue(5, now, "nefield") }, PanicMatches, "Bad Top Level field type")
type Test2 struct {
Start *time.Time `validate:"nefield=NonExistantField"`
End *time.Time
}
sv2 := &Test2{
Start: &now,
End: &now,
}
c.Assert(func() { validate.Struct(sv2) }, PanicMatches, "Field \"NonExistantField\" not found in struct")
}
func (ms *MySuite) TestIsNeValidation(c *C) {
var j uint64
var k float64
s := "abcdef"
i := 3
j = 2
k = 1.5434
arr := []string{"test"}
now := time.Now().UTC()
err := validate.Field(s, "ne=abcd")
c.Assert(err, IsNil)
err = validate.Field(i, "ne=1")
c.Assert(err, IsNil)
err = validate.Field(j, "ne=1")
c.Assert(err, IsNil)
err = validate.Field(k, "ne=1.543")
c.Assert(err, IsNil)
err = validate.Field(arr, "ne=2")
c.Assert(err, IsNil)
err = validate.Field(arr, "ne=1")
c.Assert(err, NotNil)
c.Assert(func() { validate.Field(now, "ne=now") }, PanicMatches, "Bad field type time.Time")
}
func (ms *MySuite) TestIsEqFieldValidation(c *C) { func (ms *MySuite) TestIsEqFieldValidation(c *C) {
var j uint64 var j uint64

Loading…
Cancel
Save