Merge pull request #53 from bluesuncorp/v5-development

V5 development
pull/54/head v5.5.1
Dean Karn 9 years ago
commit 1d61bf3148
  1. 2
      .gitignore
  2. 9
      validator.go
  3. 59
      validator_test.go

2
.gitignore vendored

@ -22,3 +22,5 @@ _testmain.go
*.exe
*.test
*.prof
*.test
*.out

@ -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())
@ -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{},
@ -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))
@ -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 {

@ -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
@ -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)
}
}

Loading…
Cancel
Save