Skip to content

Commit

Permalink
Resolving Validating unexported fields #417
Browse files Browse the repository at this point in the history
  • Loading branch information
nerzulk committed Feb 22, 2024
1 parent b328f72 commit 38b9a9a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -15,4 +15,4 @@ test:
bench:
$(GOCMD) test -run=NONE -bench=. -benchmem ./...

.PHONY: test lint linters-install
.PHONY: test lint linters-install
4 changes: 0 additions & 4 deletions cache.go
Expand Up @@ -126,10 +126,6 @@ func (v *Validate) extractStructCache(current reflect.Value, sName string) *cStr

fld = typ.Field(i)

if !fld.Anonymous && len(fld.PkgPath) > 0 {
continue
}

if rtag, ok := rules[fld.Name]; ok {
tag = rtag
} else {
Expand Down
35 changes: 32 additions & 3 deletions validator.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"reflect"
"strconv"
"unsafe"
)

// per validate construct
Expand Down Expand Up @@ -410,7 +411,7 @@ OUTER:
structNs: v.str2,
fieldLen: uint8(len(cf.altName)),
structfieldLen: uint8(len(cf.name)),
value: current.Interface(),
value: getValue(current),
param: ct.param,
kind: kind,
typ: typ,
Expand All @@ -430,7 +431,7 @@ OUTER:
structNs: v.str2,
fieldLen: uint8(len(cf.altName)),
structfieldLen: uint8(len(cf.name)),
value: current.Interface(),
value: getValue(current),
param: ct.param,
kind: kind,
typ: typ,
Expand Down Expand Up @@ -470,7 +471,7 @@ OUTER:
structNs: v.str2,
fieldLen: uint8(len(cf.altName)),
structfieldLen: uint8(len(cf.name)),
value: current.Interface(),
value: getValue(current),
param: ct.param,
kind: kind,
typ: typ,
Expand All @@ -484,3 +485,31 @@ OUTER:
}

}

func getValue(val reflect.Value) any {

Check failure on line 489 in validator.go

View workflow job for this annotation

GitHub Actions / test (1.17.x, ubuntu-latest)

undefined: any
if val.CanInterface() {
return val.Interface()
}

if val.CanAddr() {
return reflect.NewAt(val.Type(), unsafe.Pointer(val.UnsafeAddr())).Elem().Interface()
}

if val.CanInt() {

Check failure on line 498 in validator.go

View workflow job for this annotation

GitHub Actions / test (1.17.x, ubuntu-latest)

val.CanInt undefined (type reflect.Value has no field or method CanInt)
return val.Int()
}

if val.CanUint() {

Check failure on line 502 in validator.go

View workflow job for this annotation

GitHub Actions / test (1.17.x, ubuntu-latest)

val.CanUint undefined (type reflect.Value has no field or method CanUint)
return val.Uint()
}

if val.CanComplex() {

Check failure on line 506 in validator.go

View workflow job for this annotation

GitHub Actions / test (1.17.x, ubuntu-latest)

val.CanComplex undefined (type reflect.Value has no field or method CanComplex)
return val.Complex()
}

if val.CanFloat() {

Check failure on line 510 in validator.go

View workflow job for this annotation

GitHub Actions / test (1.17.x, ubuntu-latest)

val.CanFloat undefined (type reflect.Value has no field or method CanFloat)
return val.Float()
}

return val.String()
}
9 changes: 6 additions & 3 deletions validator_test.go
Expand Up @@ -470,8 +470,9 @@ func TestAnonymous(t *testing.T) {

errs := err.(ValidationErrors)

Equal(t, len(errs), 1)
Equal(t, len(errs), 2)
AssertError(t, errs, "Test.AnonymousB.BEE", "Test.AnonymousB.B", "BEE", "B", "required")
AssertError(t, errs, "Test.anonymousC.c", "Test.anonymousC.c", "c", "c", "required")

fe := getError(errs, "Test.AnonymousB.BEE", "Test.AnonymousB.B")
NotEqual(t, fe, nil)
Expand All @@ -485,7 +486,8 @@ func TestAnonymous(t *testing.T) {
}

err = validate.Struct(s)
Equal(t, err, nil)
NotEqual(t, err, nil)
AssertError(t, err, "c", "c", "c", "c", "required")
}

func TestAnonymousSameStructDifferentTags(t *testing.T) {
Expand Down Expand Up @@ -7449,7 +7451,8 @@ func TestUnexposedStruct(t *testing.T) {
Equal(t, s.unexposed.A, "")

errs := validate.Struct(s)
Equal(t, errs, nil)
NotEqual(t, errs, nil)
AssertError(t, errs, "Test.unexposed.A", "Test.unexposed.A", "A", "A", "required")
}

func TestBadParams(t *testing.T) {
Expand Down

0 comments on commit 38b9a9a

Please sign in to comment.