forked from go-playground/validator
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
break out benchmarks into separate file
create examples file for better godocs
- Loading branch information
joeybloggs
authored and
joeybloggs
committed
Jun 11, 2015
1 parent
df95f9d
commit 9ff11ae
Showing
4 changed files
with
259 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package validator | ||
|
||
import "testing" | ||
|
||
func BenchmarkValidateField(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
validate.Field("1", "len=1") | ||
} | ||
} | ||
|
||
func BenchmarkValidateStructSimple(b *testing.B) { | ||
|
||
type Foo struct { | ||
StringValue string `validate:"min=5,max=10"` | ||
IntValue int `validate:"min=5,max=10"` | ||
} | ||
|
||
validFoo := &Foo{StringValue: "Foobar", IntValue: 7} | ||
invalidFoo := &Foo{StringValue: "Fo", IntValue: 3} | ||
|
||
for n := 0; n < b.N; n++ { | ||
validate.Struct(validFoo) | ||
validate.Struct(invalidFoo) | ||
} | ||
} | ||
|
||
// func BenchmarkTemplateParallelSimple(b *testing.B) { | ||
|
||
// type Foo struct { | ||
// StringValue string `validate:"min=5,max=10"` | ||
// IntValue int `validate:"min=5,max=10"` | ||
// } | ||
|
||
// validFoo := &Foo{StringValue: "Foobar", IntValue: 7} | ||
// invalidFoo := &Foo{StringValue: "Fo", IntValue: 3} | ||
|
||
// b.RunParallel(func(pb *testing.PB) { | ||
// for pb.Next() { | ||
// validate.Struct(validFoo) | ||
// validate.Struct(invalidFoo) | ||
// } | ||
// }) | ||
// } | ||
|
||
func BenchmarkValidateStructLarge(b *testing.B) { | ||
|
||
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", | ||
}, | ||
} | ||
|
||
tSuccess := &TestString{ | ||
Required: "Required", | ||
Len: "length==10", | ||
Min: "min=1", | ||
Max: "1234567890", | ||
MinMax: "12345", | ||
Lt: "012345678", | ||
Lte: "0123456789", | ||
Gt: "01234567890", | ||
Gte: "0123456789", | ||
OmitEmpty: "", | ||
Sub: &SubTest{ | ||
Test: "1", | ||
}, | ||
SubIgnore: &SubTest{ | ||
Test: "", | ||
}, | ||
Anonymous: struct { | ||
A string `validate:"required"` | ||
}{ | ||
A: "1", | ||
}, | ||
Iface: &Impl{ | ||
F: "123", | ||
}, | ||
} | ||
|
||
for n := 0; n < b.N; n++ { | ||
validate.Struct(tSuccess) | ||
validate.Struct(tFail) | ||
} | ||
} | ||
|
||
// func BenchmarkTemplateParallelLarge(b *testing.B) { | ||
|
||
// 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", | ||
// }, | ||
// } | ||
|
||
// tSuccess := &TestString{ | ||
// Required: "Required", | ||
// Len: "length==10", | ||
// Min: "min=1", | ||
// Max: "1234567890", | ||
// MinMax: "12345", | ||
// Lt: "012345678", | ||
// Lte: "0123456789", | ||
// Gt: "01234567890", | ||
// Gte: "0123456789", | ||
// OmitEmpty: "", | ||
// Sub: &SubTest{ | ||
// Test: "1", | ||
// }, | ||
// SubIgnore: &SubTest{ | ||
// Test: "", | ||
// }, | ||
// Anonymous: struct { | ||
// A string `validate:"required"` | ||
// }{ | ||
// A: "1", | ||
// }, | ||
// Iface: &Impl{ | ||
// F: "123", | ||
// }, | ||
// } | ||
|
||
// b.RunParallel(func(pb *testing.PB) { | ||
// for pb.Next() { | ||
// validate.Struct(tSuccess) | ||
// validate.Struct(tFail) | ||
// } | ||
// }) | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package validator_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"../validator" | ||
) | ||
|
||
func ExampleValidate_new() { | ||
validator.New("validate", validator.BakedInValidators) | ||
} | ||
|
||
func ExampleValidate_addFunction() { | ||
// This should be stored somewhere globally | ||
var validate *validator.Validate | ||
|
||
validate = validator.New("validate", validator.BakedInValidators) | ||
|
||
fn := func(top interface{}, current interface{}, field interface{}, param string) bool { | ||
return field.(string) == "hello" | ||
} | ||
|
||
validate.AddFunction("valueishello", fn) | ||
|
||
message := "hello" | ||
err := validate.Field(message, "valueishello") | ||
fmt.Println(err) | ||
//Output: | ||
//<nil> | ||
} | ||
|
||
func ExampleValidate_field() { | ||
// This should be stored somewhere globally | ||
var validate *validator.Validate | ||
|
||
validate = validator.New("validate", validator.BakedInValidators) | ||
|
||
i := 0 | ||
err := validate.Field(i, "gt=1,lte=10") | ||
fmt.Println(err.Field) | ||
fmt.Println(err.Tag) | ||
fmt.Println(err.Kind) // NOTE: Kind and Type can be different i.e. time Kind=struct and Type=time.Time | ||
fmt.Println(err.Type) | ||
fmt.Println(err.Param) | ||
fmt.Println(err.Value) | ||
//Output: | ||
// | ||
//gt | ||
//int | ||
//int | ||
//1 | ||
//0 | ||
} | ||
|
||
func ExampleValidate_struct() { | ||
// This should be stored somewhere globally | ||
var validate *validator.Validate | ||
|
||
validate = validator.New("validate", validator.BakedInValidators) | ||
|
||
type ContactInformation struct { | ||
Phone string `validate:"required"` | ||
Street string `validate:"required"` | ||
City string `validate:"required"` | ||
} | ||
|
||
type User struct { | ||
Name string `validate:"required,excludesall=!@#$%^&*()_+-=:;?/0x2C"` // 0x2C = comma (,) | ||
Age int8 `validate:"required,gt=0,lt=150"` | ||
Email string `validate:"email"` | ||
ContactInformation []*ContactInformation | ||
} | ||
|
||
contactInfo := &ContactInformation{ | ||
Street: "26 Here Blvd.", | ||
City: "Paradeso", | ||
} | ||
|
||
user := &User{ | ||
Name: "Joey Bloggs", | ||
Age: 31, | ||
Email: "joeybloggs@gmail.com", | ||
ContactInformation: []*ContactInformation{contactInfo}, | ||
} | ||
|
||
structError := validate.Struct(user) | ||
for _, fieldError := range structError.Errors { | ||
fmt.Println(fieldError.Field) // Phone | ||
fmt.Println(fieldError.Tag) // required | ||
//... and so forth | ||
//Output: | ||
//Phone | ||
//required | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.