Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ _testmain.go
*.exe
*.test
*.prof
*.test
*.out
9 changes: 5 additions & 4 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ func (v *Validate) Struct(s interface{}) *StructErrors {
func (v *Validate) structRecursive(top interface{}, current interface{}, s interface{}) *StructErrors {

structValue := reflect.ValueOf(s)
structType := reflect.TypeOf(s)
structName := structType.Name()

if structValue.Kind() == reflect.Ptr && !structValue.IsNil() {
return v.structRecursive(top, current, structValue.Elem().Interface())
Expand All @@ -177,6 +175,9 @@ func (v *Validate) structRecursive(top interface{}, current interface{}, s inter
panic("interface passed for validation is not a struct")
}

structType := reflect.TypeOf(s)
structName := structType.Name()

validationErrors := &StructErrors{
Struct: structName,
Errors: map[string]*FieldError{},
Expand Down Expand Up @@ -339,7 +340,7 @@ func (v *Validate) fieldWithNameAndValue(val interface{}, current interface{}, f
func (v *Validate) fieldWithNameAndSingleTag(val interface{}, current interface{}, f interface{}, name string, valTag string) (*FieldError, error) {

vals := strings.Split(valTag, tagKeySeparator)
key := strings.Trim(vals[0], " ")
key := strings.TrimSpace(vals[0])

if len(key) == 0 {
panic(fmt.Sprintf("Invalid validation tag on field %s", name))
Expand All @@ -364,7 +365,7 @@ func (v *Validate) fieldWithNameAndSingleTag(val interface{}, current interface{

param := ""
if len(vals) > 1 {
param = strings.Trim(vals[1], " ")
param = strings.TrimSpace(vals[1])
}

if err := valFunc(val, current, f, param); !err {
Expand Down
59 changes: 59 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ import (
// - Run "go test" to run tests
// - Run "gocov test | gocov report" to report on test converage by file
// - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called
//
//
// go test -cpuprofile cpu.out
// ./validator.test -test.bench=. -test.cpuprofile=cpu.prof
// go tool pprof validator.test cpu.prof
//
//
// go test -memprofile mem.out

type I interface {
Foo() string
Expand Down Expand Up @@ -2310,3 +2318,54 @@ func TestInvalidValidatorFunction(t *testing.T) {

PanicMatches(t, func() { validate.Field(s.Test, "zzxxBadFunction") }, fmt.Sprintf("Undefined validation function on field %s", ""))
}

func BenchmarkValidateField(b *testing.B) {
for n := 0; n < b.N; n++ {
validate.Field("1", "len=1")
}
}

func BenchmarkValidateStruct(b *testing.B) {

// type Inner struct {

// }

// type Test struct {
// StringVal string `bson:"required,lt=10"`
// Int64Val int64 `bson:"gt=0,lt=10"`
// }

tFail := &TestString{
Required: "",
Len: "",
Min: "",
Max: "12345678901",
MinMax: "",
Lt: "0123456789",
Lte: "01234567890",
Gt: "1",
Gte: "1",
OmitEmpty: "12345678901",
Sub: &SubTest{
Test: "",
},
Anonymous: struct {
A string `validate:"required"`
}{
A: "",
},
Iface: &Impl{
F: "12",
},
}

// t := &Test{
// StringVal: "test",
// Int64Val: 5,
// }

for n := 0; n < b.N; n++ {
validate.Struct(tFail)
}
}