Skip to content

Commit 4b62444

Browse files
Dean KarnDean Karn
authored andcommitted
Merge pull request #132 from bluesuncorp/v5-development
Porting Back fixes from v6
2 parents 9a83683 + c6b1274 commit 4b62444

File tree

4 files changed

+100
-11
lines changed

4 files changed

+100
-11
lines changed

baked_in.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,12 @@ func hasValue(top interface{}, current interface{}, field interface{}, param str
424424
st := reflect.ValueOf(field)
425425

426426
switch st.Kind() {
427-
428-
case reflect.Slice, reflect.Map, reflect.Array:
429-
return field != nil && int64(st.Len()) > 0
430-
427+
case reflect.Invalid:
428+
return false
429+
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
430+
return !st.IsNil()
431+
case reflect.Array:
432+
return field != reflect.Zero(reflect.TypeOf(field)).Interface()
431433
default:
432434
return field != nil && field != reflect.Zero(reflect.TypeOf(field)).Interface()
433435
}

doc.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,10 @@ Here is a list of the current built in validators:
193193
within its SliceOrArrayErrs or MapErrs fields.
194194
195195
required
196-
This validates that the value is not the data types default value.
196+
This validates that the value is not the data types default zero value.
197197
For numbers ensures value is not zero. For strings ensures value is
198-
not "". For slices, arrays, and maps, ensures the length is not zero.
198+
not "". For slices, maps, pointers, interfaces, channels and functions
199+
ensures the value is not nil.
199200
(Usage: required)
200201
201202
len

validator.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,11 @@ func (v *Validate) fieldWithNameAndValue(val interface{}, current interface{}, f
627627
f = valueField.Interface()
628628
}
629629

630-
cField = &cachedField{name: name, kind: valueField.Kind(), tag: tag, typ: valueField.Type()}
630+
cField = &cachedField{name: name, kind: valueField.Kind(), tag: tag}
631+
632+
if cField.kind != reflect.Invalid {
633+
cField.typ = valueField.Type()
634+
}
631635

632636
switch cField.kind {
633637
case reflect.Slice, reflect.Array:
@@ -648,8 +652,14 @@ func (v *Validate) fieldWithNameAndValue(val interface{}, current interface{}, f
648652
}
649653

650654
switch cField.kind {
655+
case reflect.Invalid:
656+
return &FieldError{
657+
Field: cField.name,
658+
Tag: cField.tag,
659+
Kind: cField.kind,
660+
}
651661

652-
case reflect.Struct, reflect.Interface, reflect.Invalid:
662+
case reflect.Struct, reflect.Interface:
653663

654664
if cField.typ != reflect.TypeOf(time.Time{}) {
655665
panic("Invalid field passed to fieldWithNameAndValue")

validator_test.go

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,82 @@ func AssertMapFieldError(t *testing.T, s map[string]*FieldError, field string, e
231231
EqualSkip(t, 2, val.Tag, expectedTag)
232232
}
233233

234+
func TestSliceMapArrayChanFuncPtrInterfaceRequiredValidation(t *testing.T) {
235+
236+
var m map[string]string
237+
238+
errs := validate.Field(m, "required")
239+
NotEqual(t, errs, nil)
240+
// AssertError(t, errs, "", "", "required")
241+
242+
m = map[string]string{}
243+
errs = validate.Field(m, "required")
244+
Equal(t, errs, nil)
245+
246+
var arr [5]string
247+
errs = validate.Field(arr, "required")
248+
NotEqual(t, errs, nil)
249+
// AssertError(t, errs, "", "", "required")
250+
251+
arr[0] = "ok"
252+
errs = validate.Field(arr, "required")
253+
Equal(t, errs, nil)
254+
255+
var s []string
256+
errs = validate.Field(s, "required")
257+
NotEqual(t, errs, nil)
258+
// AssertError(t, errs, "", "", "required")
259+
260+
s = []string{}
261+
errs = validate.Field(s, "required")
262+
Equal(t, errs, nil)
263+
264+
var c chan string
265+
errs = validate.Field(c, "required")
266+
NotEqual(t, errs, nil)
267+
// AssertError(t, errs, "", "", "required")
268+
269+
c = make(chan string)
270+
errs = validate.Field(c, "required")
271+
Equal(t, errs, nil)
272+
273+
var tst *int
274+
errs = validate.Field(tst, "required")
275+
NotEqual(t, errs, nil)
276+
// AssertError(t, errs, "", "", "required")
277+
278+
one := 1
279+
tst = &one
280+
errs = validate.Field(tst, "required")
281+
Equal(t, errs, nil)
282+
283+
var iface interface{}
284+
285+
errs = validate.Field(iface, "required")
286+
NotEqual(t, errs, nil)
287+
// AssertError(t, errs, "", "", "required")
288+
289+
errs = validate.Field(iface, "omitempty,required")
290+
Equal(t, errs, nil)
291+
292+
errs = validate.Field(iface, "")
293+
Equal(t, errs, nil)
294+
295+
errs = validate.Field(iface, "len=1")
296+
NotEqual(t, errs, nil)
297+
298+
var f func(string)
299+
300+
errs = validate.Field(f, "required")
301+
NotEqual(t, errs, nil)
302+
// AssertError(t, errs, "", "", "required")
303+
304+
f = func(name string) {}
305+
306+
errs = validate.Field(f, "required")
307+
Equal(t, errs, nil)
308+
}
309+
234310
func TestBadKeyValidation(t *testing.T) {
235311
type Test struct {
236312
Name string `validate:"required, "`
@@ -3735,14 +3811,14 @@ func TestStructSliceValidation(t *testing.T) {
37353811
Min: []int{1, 2},
37363812
Max: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0},
37373813
MinMax: []int{1, 2, 3, 4, 5},
3738-
OmitEmpty: []int{},
3814+
OmitEmpty: nil,
37393815
}
37403816

37413817
err := validate.Struct(tSuccess)
37423818
Equal(t, err, nil)
37433819

37443820
tFail := &TestSlice{
3745-
Required: []int{},
3821+
Required: nil,
37463822
Len: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1},
37473823
Min: []int{},
37483824
Max: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1},
@@ -3810,7 +3886,7 @@ func TestPoolObjectMaxSizeValidation(t *testing.T) {
38103886
Min: []int{1, 2},
38113887
Max: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0},
38123888
MinMax: []int{1, 2, 3, 4, 5},
3813-
OmitEmpty: []int{},
3889+
OmitEmpty: nil,
38143890
}
38153891

38163892
for i := 0; i < 2; i++ {

0 commit comments

Comments
 (0)