Skip to content

Commit 883a9e0

Browse files
author
Dean Karn
authored
Merge pull request #523 from go-playground/fixes
Fixes
2 parents 51fcc30 + 9593a0f commit 883a9e0

File tree

13 files changed

+413
-431
lines changed

13 files changed

+413
-431
lines changed

Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
GOCMD=go
22

33
linters-install:
4-
$(GOCMD) get -u github.com/alecthomas/gometalinter
5-
gometalinter --install
4+
@golangci-lint --version >/dev/null 2>&1 || { \
5+
echo "installing linting tools..."; \
6+
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s v1.19.1; \
7+
}
68

79
lint: linters-install
8-
gometalinter --vendor --disable-all --enable=vet --enable=vetshadow --enable=golint --enable=maligned --enable=megacheck --enable=ineffassign --enable=misspell --enable=errcheck --enable=goconst ./...
10+
golangci-lint run
911

1012
test:
1113
$(GOCMD) test -cover -race ./...

baked_in.go

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,103 +1301,85 @@ func isDefault(fl FieldLevel) bool {
13011301

13021302
// HasValue is the validation function for validating if the current field's value is not the default static value.
13031303
func hasValue(fl FieldLevel) bool {
1304-
return requireCheckFieldKind(fl, "")
1304+
field := fl.Field()
1305+
switch field.Kind() {
1306+
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
1307+
return !field.IsNil()
1308+
default:
1309+
if fl.(*validate).fldIsPointer && field.Interface() != nil {
1310+
return true
1311+
}
1312+
return field.IsValid() && field.Interface() != reflect.Zero(field.Type()).Interface()
1313+
}
13051314
}
13061315

13071316
// requireCheckField is a func for check field kind
1308-
func requireCheckFieldKind(fl FieldLevel, param string) bool {
1317+
func requireCheckFieldKind(fl FieldLevel, param string, defaultNotFoundValue bool) bool {
13091318
field := fl.Field()
1319+
var ok bool
1320+
kind := field.Kind()
13101321
if len(param) > 0 {
1311-
if fl.Parent().Kind() == reflect.Ptr {
1312-
field = fl.Parent().Elem().FieldByName(param)
1313-
} else {
1314-
field = fl.Parent().FieldByName(param)
1322+
field, kind, ok = fl.GetStructFieldOKAdvanced(fl.Parent(), param)
1323+
if !ok {
1324+
return defaultNotFoundValue
13151325
}
13161326
}
1317-
switch field.Kind() {
1327+
switch kind {
1328+
case reflect.Invalid:
1329+
return defaultNotFoundValue
13181330
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
13191331
return !field.IsNil()
13201332
default:
1321-
if fl.(*validate).fldIsPointer && field.Interface() != nil {
1322-
return true
1323-
}
13241333
return field.IsValid() && field.Interface() != reflect.Zero(field.Type()).Interface()
13251334
}
13261335
}
13271336

13281337
// RequiredWith is the validation function
13291338
// The field under validation must be present and not empty only if any of the other specified fields are present.
13301339
func requiredWith(fl FieldLevel) bool {
1331-
13321340
params := parseOneOfParam2(fl.Param())
13331341
for _, param := range params {
1334-
1335-
if requireCheckFieldKind(fl, param) {
1336-
return requireCheckFieldKind(fl, "")
1342+
if requireCheckFieldKind(fl, param, false) {
1343+
return hasValue(fl)
13371344
}
13381345
}
1339-
13401346
return true
13411347
}
13421348

13431349
// RequiredWithAll is the validation function
13441350
// The field under validation must be present and not empty only if all of the other specified fields are present.
13451351
func requiredWithAll(fl FieldLevel) bool {
1346-
1347-
isValidateCurrentField := true
13481352
params := parseOneOfParam2(fl.Param())
13491353
for _, param := range params {
1350-
1351-
if !requireCheckFieldKind(fl, param) {
1352-
isValidateCurrentField = false
1354+
if !requireCheckFieldKind(fl, param, false) {
1355+
return true
13531356
}
13541357
}
1355-
1356-
if isValidateCurrentField {
1357-
return requireCheckFieldKind(fl, "")
1358-
}
1359-
1360-
return true
1358+
return hasValue(fl)
13611359
}
13621360

13631361
// RequiredWithout is the validation function
13641362
// The field under validation must be present and not empty only when any of the other specified fields are not present.
13651363
func requiredWithout(fl FieldLevel) bool {
1366-
1367-
isValidateCurrentField := false
13681364
params := parseOneOfParam2(fl.Param())
13691365
for _, param := range params {
1370-
1371-
if requireCheckFieldKind(fl, param) {
1372-
isValidateCurrentField = true
1366+
if !requireCheckFieldKind(fl, param, true) {
1367+
return hasValue(fl)
13731368
}
13741369
}
1375-
1376-
if !isValidateCurrentField {
1377-
return requireCheckFieldKind(fl, "")
1378-
}
1379-
13801370
return true
13811371
}
13821372

13831373
// RequiredWithoutAll is the validation function
13841374
// The field under validation must be present and not empty only when all of the other specified fields are not present.
13851375
func requiredWithoutAll(fl FieldLevel) bool {
1386-
1387-
isValidateCurrentField := true
13881376
params := parseOneOfParam2(fl.Param())
13891377
for _, param := range params {
1390-
1391-
if requireCheckFieldKind(fl, param) {
1392-
isValidateCurrentField = false
1378+
if requireCheckFieldKind(fl, param, true) {
1379+
return true
13931380
}
13941381
}
1395-
1396-
if isValidateCurrentField {
1397-
return requireCheckFieldKind(fl, "")
1398-
}
1399-
1400-
return true
1382+
return hasValue(fl)
14011383
}
14021384

14031385
// IsGteField is the validation function for validating if the current field's value is greater than or equal to the field specified by the param's value.

0 commit comments

Comments
 (0)