Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolving "Validating unexported fields #417" #1234

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
32 changes: 28 additions & 4 deletions validator.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"reflect"
"strconv"
"unsafe"
)

// per validate construct
Expand Down Expand Up @@ -156,7 +157,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
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: current.Type(),
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,26 @@ OUTER:
}

}

func getValue(val reflect.Value) interface{} {
if val.CanInterface() {
return val.Interface()
}

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

switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return val.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return val.Uint()
case reflect.Complex64, reflect.Complex128:
return val.Complex()
case reflect.Float32, reflect.Float64:
return val.Float()
default:
return val.String()
}
}
111 changes: 108 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,110 @@ 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 TestPrivateFieldsStruct(t *testing.T) {
type tc struct {
stct interface{}
errorNum int
}

tcs := []tc{
{
stct: &struct {
f1 int8 `validate:"required"`
f2 int16 `validate:"required"`
f3 int32 `validate:"required"`
f4 int64 `validate:"required"`
}{},
errorNum: 4,
},
{
stct: &struct {
f1 uint8 `validate:"required"`
f2 uint16 `validate:"required"`
f3 uint32 `validate:"required"`
f4 uint64 `validate:"required"`
}{},
errorNum: 4,
},
{
stct: &struct {
f1 complex64 `validate:"required"`
f2 complex128 `validate:"required"`
}{},
errorNum: 2,
},
{
stct: &struct {
f1 float32 `validate:"required"`
f2 float64 `validate:"required"`
}{},
errorNum: 2,
},
{
stct: struct {
f1 int8 `validate:"required"`
f2 int16 `validate:"required"`
f3 int32 `validate:"required"`
f4 int64 `validate:"required"`
}{},
errorNum: 4,
},
{
stct: struct {
f1 uint8 `validate:"required"`
f2 uint16 `validate:"required"`
f3 uint32 `validate:"required"`
f4 uint64 `validate:"required"`
}{},
errorNum: 4,
},
{
stct: struct {
f1 complex64 `validate:"required"`
f2 complex128 `validate:"required"`
}{},
errorNum: 2,
},
{
stct: struct {
f1 float32 `validate:"required"`
f2 float64 `validate:"required"`
}{},
errorNum: 2,
},
{
stct: struct {
f1 *int `validate:"required"`
f2 struct {
f3 int `validate:"required"`
}
}{},
errorNum: 2,
},
{
stct: &struct {
f1 *int `validate:"required"`
f2 struct {
f3 int `validate:"required"`
}
}{},
errorNum: 2,
},
}

validate := New()

for _, tc := range tcs {
err := validate.Struct(tc.stct)
NotEqual(t, err, nil)

errs := err.(ValidationErrors)
Equal(t, len(errs), tc.errorNum)
}
}

func TestAnonymousSameStructDifferentTags(t *testing.T) {
Expand Down Expand Up @@ -7449,7 +7553,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