Skip to content

Commit d780b12

Browse files
Dean KarnDean Karn
Dean Karn
authored and
Dean Karn
committed
Merge pull request #40 from bluesuncorp/v5-development
V5 development
2 parents 74d0e52 + ee35a11 commit d780b12

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

baked_in.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ var BakedInValidators = map[string]Func{
1717
"min": hasMinOf,
1818
"max": hasMaxOf,
1919
"eq": isEq,
20+
"ne": isNe,
2021
"lt": isLt,
2122
"lte": isLte,
2223
"gt": isGt,
2324
"gte": isGte,
2425
"eqfield": isEqField,
26+
"nefield": isNeField,
2527
"gtefield": isGteField,
2628
"gtfield": isGtField,
2729
"ltefield": isLteField,
@@ -42,6 +44,14 @@ var BakedInValidators = map[string]Func{
4244
"base64": isBase64,
4345
}
4446

47+
func isNeField(top interface{}, current interface{}, field interface{}, param string) bool {
48+
return !isEqField(top, current, field, param)
49+
}
50+
51+
func isNe(top interface{}, current interface{}, field interface{}, param string) bool {
52+
return !isEq(top, current, field, param)
53+
}
54+
4555
func isEqField(top interface{}, current interface{}, field interface{}, param string) bool {
4656

4757
if current == nil {

doc.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ Here is a list of the current built in validators:
197197
equal to the parameter given. For slices, arrays, and maps,
198198
validates the number of items. (Usage: eq=10)
199199
200+
ne
201+
For strings & numbers, eq will ensure that the value is not
202+
equal to the parameter given. For slices, arrays, and maps,
203+
validates the number of items. (Usage: eq=10)
204+
200205
gt
201206
For numbers, this will ensure that the value is greater than the
202207
parameter given. For strings, it checks that the string length
@@ -233,6 +238,13 @@ Here is a list of the current built in validators:
233238
Validation on Password field using validate.Struct Usage(eqfield=ConfirmPassword)
234239
Validating by field validate.FieldWithValue(password, confirmpassword, "eqfield")
235240
241+
nefield
242+
This will validate the field value against another fields value either within
243+
a struct or passed in field.
244+
usage examples are for ensuring two colors are not the same:
245+
Validation on Color field using validate.Struct Usage(nefield=Color2)
246+
Validating by field validate.FieldWithValue(color1, color2, "nefield")
247+
236248
gtfield
237249
Only valid for Numbers and time.Time types, this will validate the field value
238250
against another fields value either within a struct or passed in field.

validator_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,122 @@ func isEqualFunc(val interface{}, current interface{}, field interface{}, param
142142
return current.(string) == field.(string)
143143
}
144144

145+
func (ms *MySuite) TestIsNeFieldValidation(c *C) {
146+
147+
var j uint64
148+
var k float64
149+
s := "abcd"
150+
i := 1
151+
j = 1
152+
k = 1.543
153+
arr := []string{"test"}
154+
now := time.Now().UTC()
155+
156+
var j2 uint64
157+
var k2 float64
158+
s2 := "abcdef"
159+
i2 := 3
160+
j2 = 2
161+
k2 = 1.5434456
162+
arr2 := []string{"test", "test2"}
163+
arr3 := []string{"test"}
164+
now2 := now
165+
166+
err := validate.FieldWithValue(s, s2, "nefield")
167+
c.Assert(err, IsNil)
168+
169+
err = validate.FieldWithValue(i2, i, "nefield")
170+
c.Assert(err, IsNil)
171+
172+
err = validate.FieldWithValue(j2, j, "nefield")
173+
c.Assert(err, IsNil)
174+
175+
err = validate.FieldWithValue(k2, k, "nefield")
176+
c.Assert(err, IsNil)
177+
178+
err = validate.FieldWithValue(arr2, arr, "nefield")
179+
c.Assert(err, IsNil)
180+
181+
err = validate.FieldWithValue(now2, now, "nefield")
182+
c.Assert(err, NotNil)
183+
184+
err = validate.FieldWithValue(arr3, arr, "nefield")
185+
c.Assert(err, NotNil)
186+
187+
type Test struct {
188+
Start *time.Time `validate:"nefield=End"`
189+
End *time.Time
190+
}
191+
192+
sv := &Test{
193+
Start: &now,
194+
End: &now,
195+
}
196+
197+
errs := validate.Struct(sv)
198+
c.Assert(errs, NotNil)
199+
200+
now3 := time.Now().UTC()
201+
202+
sv = &Test{
203+
Start: &now,
204+
End: &now3,
205+
}
206+
207+
errs = validate.Struct(sv)
208+
c.Assert(errs, IsNil)
209+
210+
channel := make(chan string)
211+
212+
c.Assert(func() { validate.FieldWithValue(nil, 1, "nefield") }, PanicMatches, "struct not passed for cross validation")
213+
c.Assert(func() { validate.FieldWithValue(5, channel, "nefield") }, PanicMatches, "Bad field type chan string")
214+
c.Assert(func() { validate.FieldWithValue(5, now, "nefield") }, PanicMatches, "Bad Top Level field type")
215+
216+
type Test2 struct {
217+
Start *time.Time `validate:"nefield=NonExistantField"`
218+
End *time.Time
219+
}
220+
221+
sv2 := &Test2{
222+
Start: &now,
223+
End: &now,
224+
}
225+
226+
c.Assert(func() { validate.Struct(sv2) }, PanicMatches, "Field \"NonExistantField\" not found in struct")
227+
}
228+
229+
func (ms *MySuite) TestIsNeValidation(c *C) {
230+
231+
var j uint64
232+
var k float64
233+
s := "abcdef"
234+
i := 3
235+
j = 2
236+
k = 1.5434
237+
arr := []string{"test"}
238+
now := time.Now().UTC()
239+
240+
err := validate.Field(s, "ne=abcd")
241+
c.Assert(err, IsNil)
242+
243+
err = validate.Field(i, "ne=1")
244+
c.Assert(err, IsNil)
245+
246+
err = validate.Field(j, "ne=1")
247+
c.Assert(err, IsNil)
248+
249+
err = validate.Field(k, "ne=1.543")
250+
c.Assert(err, IsNil)
251+
252+
err = validate.Field(arr, "ne=2")
253+
c.Assert(err, IsNil)
254+
255+
err = validate.Field(arr, "ne=1")
256+
c.Assert(err, NotNil)
257+
258+
c.Assert(func() { validate.Field(now, "ne=now") }, PanicMatches, "Bad field type time.Time")
259+
}
260+
145261
func (ms *MySuite) TestIsEqFieldValidation(c *C) {
146262

147263
var j uint64

0 commit comments

Comments
 (0)