Skip to content

Commit bec732e

Browse files
author
Dean Karn
authored
Merge pull request #543 from go-playground/fix-required-without
fix required_*
2 parents fe89c74 + a346f2e commit bec732e

File tree

5 files changed

+130
-48
lines changed

5 files changed

+130
-48
lines changed

baked_in.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,21 +1316,24 @@ func hasValue(fl FieldLevel) bool {
13161316
// requireCheckField is a func for check field kind
13171317
func requireCheckFieldKind(fl FieldLevel, param string, defaultNotFoundValue bool) bool {
13181318
field := fl.Field()
1319-
var ok bool
13201319
kind := field.Kind()
1320+
var nullable, found bool
13211321
if len(param) > 0 {
1322-
field, kind, ok = fl.GetStructFieldOKAdvanced(fl.Parent(), param)
1323-
if !ok {
1322+
field, kind, nullable, found = fl.GetStructFieldOKAdvanced2(fl.Parent(), param)
1323+
if !found {
13241324
return defaultNotFoundValue
13251325
}
13261326
}
13271327
switch kind {
13281328
case reflect.Invalid:
13291329
return defaultNotFoundValue
13301330
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
1331-
return !field.IsNil()
1331+
return field.IsNil()
13321332
default:
1333-
return field.IsValid() && field.Interface() != reflect.Zero(field.Type()).Interface()
1333+
if nullable && field.Interface() != nil {
1334+
return false
1335+
}
1336+
return field.IsValid() && field.Interface() == reflect.Zero(field.Type()).Interface()
13341337
}
13351338
}
13361339

@@ -1339,7 +1342,7 @@ func requireCheckFieldKind(fl FieldLevel, param string, defaultNotFoundValue boo
13391342
func requiredWith(fl FieldLevel) bool {
13401343
params := parseOneOfParam2(fl.Param())
13411344
for _, param := range params {
1342-
if requireCheckFieldKind(fl, param, false) {
1345+
if !requireCheckFieldKind(fl, param, true) {
13431346
return hasValue(fl)
13441347
}
13451348
}
@@ -1351,7 +1354,7 @@ func requiredWith(fl FieldLevel) bool {
13511354
func requiredWithAll(fl FieldLevel) bool {
13521355
params := parseOneOfParam2(fl.Param())
13531356
for _, param := range params {
1354-
if !requireCheckFieldKind(fl, param, false) {
1357+
if requireCheckFieldKind(fl, param, true) {
13551358
return true
13561359
}
13571360
}
@@ -1361,11 +1364,8 @@ func requiredWithAll(fl FieldLevel) bool {
13611364
// RequiredWithout is the validation function
13621365
// The field under validation must be present and not empty only when any of the other specified fields are not present.
13631366
func requiredWithout(fl FieldLevel) bool {
1364-
params := parseOneOfParam2(fl.Param())
1365-
for _, param := range params {
1366-
if !requireCheckFieldKind(fl, param, true) {
1367-
return hasValue(fl)
1368-
}
1367+
if requireCheckFieldKind(fl, strings.TrimSpace(fl.Param()), true) {
1368+
return hasValue(fl)
13691369
}
13701370
return true
13711371
}
@@ -1375,7 +1375,7 @@ func requiredWithout(fl FieldLevel) bool {
13751375
func requiredWithoutAll(fl FieldLevel) bool {
13761376
params := parseOneOfParam2(fl.Param())
13771377
for _, param := range params {
1378-
if requireCheckFieldKind(fl, param, true) {
1378+
if !requireCheckFieldKind(fl, param, true) {
13791379
return true
13801380
}
13811381
}

field_level.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,27 @@ type FieldLevel interface {
3737
//
3838
// NOTE: when not successful ok will be false, this can happen when a nested struct is nil and so the field
3939
// could not be retrieved because it didn't exist.
40+
//
41+
// Deprecated: Use GetStructFieldOK2() instead which also return if the value is nullable.
4042
GetStructFieldOK() (reflect.Value, reflect.Kind, bool)
4143

4244
// GetStructFieldOKAdvanced is the same as GetStructFieldOK except that it accepts the parent struct to start looking for
4345
// the field and namespace allowing more extensibility for validators.
46+
//
47+
// Deprecated: Use GetStructFieldOKAdvanced2() instead which also return if the value is nullable.
4448
GetStructFieldOKAdvanced(val reflect.Value, namespace string) (reflect.Value, reflect.Kind, bool)
49+
50+
// traverses the parent struct to retrieve a specific field denoted by the provided namespace
51+
// in the param and returns the field, field kind, if it's a nullable type and whether is was successful in retrieving
52+
// the field at all.
53+
//
54+
// NOTE: when not successful ok will be false, this can happen when a nested struct is nil and so the field
55+
// could not be retrieved because it didn't exist.
56+
GetStructFieldOK2() (reflect.Value, reflect.Kind, bool, bool)
57+
58+
// GetStructFieldOKAdvanced is the same as GetStructFieldOK except that it accepts the parent struct to start looking for
59+
// the field and namespace allowing more extensibility for validators.
60+
GetStructFieldOKAdvanced2(val reflect.Value, namespace string) (reflect.Value, reflect.Kind, bool, bool)
4561
}
4662

4763
var _ FieldLevel = new(validate)
@@ -52,7 +68,7 @@ func (v *validate) Field() reflect.Value {
5268
}
5369

5470
// FieldName returns the field's name with the tag
55-
// name takeing precedence over the fields actual name.
71+
// name taking precedence over the fields actual name.
5672
func (v *validate) FieldName() string {
5773
return v.cf.altName
5874
}
@@ -68,12 +84,29 @@ func (v *validate) Param() string {
6884
}
6985

7086
// GetStructFieldOK returns Param returns param for validation against current field
87+
//
88+
// Deprecated: Use GetStructFieldOK2() instead which also return if the value is nullable.
7189
func (v *validate) GetStructFieldOK() (reflect.Value, reflect.Kind, bool) {
72-
return v.getStructFieldOKInternal(v.slflParent, v.ct.param)
90+
current, kind, _, found := v.getStructFieldOKInternal(v.slflParent, v.ct.param)
91+
return current, kind, found
7392
}
7493

7594
// GetStructFieldOKAdvanced is the same as GetStructFieldOK except that it accepts the parent struct to start looking for
7695
// the field and namespace allowing more extensibility for validators.
96+
//
97+
// Deprecated: Use GetStructFieldOKAdvanced2() instead which also return if the value is nullable.
7798
func (v *validate) GetStructFieldOKAdvanced(val reflect.Value, namespace string) (reflect.Value, reflect.Kind, bool) {
99+
current, kind, _, found := v.GetStructFieldOKAdvanced2(val, namespace)
100+
return current, kind, found
101+
}
102+
103+
// GetStructFieldOK returns Param returns param for validation against current field
104+
func (v *validate) GetStructFieldOK2() (reflect.Value, reflect.Kind, bool, bool) {
105+
return v.getStructFieldOKInternal(v.slflParent, v.ct.param)
106+
}
107+
108+
// GetStructFieldOKAdvanced is the same as GetStructFieldOK except that it accepts the parent struct to start looking for
109+
// the field and namespace allowing more extensibility for validators.
110+
func (v *validate) GetStructFieldOKAdvanced2(val reflect.Value, namespace string) (reflect.Value, reflect.Kind, bool, bool) {
78111
return v.getStructFieldOKInternal(val, namespace)
79112
}

util.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,10 @@ BEGIN:
5757
//
5858
// NOTE: when not successful ok will be false, this can happen when a nested struct is nil and so the field
5959
// could not be retrieved because it didn't exist.
60-
func (v *validate) getStructFieldOKInternal(val reflect.Value, namespace string) (current reflect.Value, kind reflect.Kind, found bool) {
60+
func (v *validate) getStructFieldOKInternal(val reflect.Value, namespace string) (current reflect.Value, kind reflect.Kind, nullable bool, found bool) {
6161

6262
BEGIN:
63-
current, kind, _ = v.ExtractType(val)
64-
63+
current, kind, nullable = v.ExtractType(val)
6564
if kind == reflect.Invalid {
6665
return
6766
}
@@ -112,7 +111,7 @@ BEGIN:
112111
arrIdx, _ := strconv.Atoi(namespace[idx+1 : idx2])
113112

114113
if arrIdx >= current.Len() {
115-
return current, kind, false
114+
return
116115
}
117116

118117
startIdx := idx2 + 1

validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"strconv"
88
)
99

10-
// per validate contruct
10+
// per validate construct
1111
type validate struct {
1212
v *Validate
1313
top reflect.Value

0 commit comments

Comments
 (0)