Skip to content

Commit a01bfab

Browse files
committed
bench test added, minor improvements
1 parent 76d9c48 commit a01bfab

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

bench_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package validator
2+
3+
import "testing"
4+
5+
type BenchTestStruct struct {
6+
Name string `json:"name" constraints:"required=true,nillable=true,min-length=5"`
7+
Age int `json:"age" constraints:"required=true,nillable=true,min=21"`
8+
Description string `json:"description" constraints:"required=true,nillable=true,max-length=50"`
9+
Cost float64 `json:"cost" constraints:"required=true,nillable=true,exclusiveMin=200"`
10+
ItemCount int `json:"itemCount" constraints:"required=true,nillable=true,multipleOf=5"`
11+
}
12+
13+
func BenchmarkValidator(b *testing.B) {
14+
msg := BenchTestStruct{
15+
Name: "BenchTest",
16+
Age: 25,
17+
Description: "this is bench testing",
18+
Cost: 299.9,
19+
ItemCount: 2000,
20+
}
21+
sv := NewStructValidator()
22+
for i := 0; i < b.N; i++ {
23+
if err := sv.Validate(msg); err != nil {
24+
b.Errorf("Error in validation: %s", err)
25+
}
26+
}
27+
}

validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (sv *StructValidator) deepFields(itr interface{}) error {
7171
default:
7272
tag := v.Tag.Get("constraints")
7373
if tag == "" {
74-
logger.InfoF("constraint not present, skip to next field")
74+
logger.InfoF("constraint not present for field : %s, skip to next field", v.Name)
7575
continue
7676
}
7777
constraints := parseTag(tag)

validate_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ func TestNested(t *testing.T) {
289289

290290
/**
291291
Empty Struct Validation
292+
// TODO
292293
*/
293294

294295
type EmptyExample struct {
@@ -303,3 +304,19 @@ func TestEmptyStruct(t *testing.T) {
303304
}
304305

305306
}
307+
308+
type ConstExample struct {
309+
Summary string `json:"summary"`
310+
Description string `json:"description" constraints:"required=false,nillable=false"`
311+
}
312+
313+
func TestConstStruct(t *testing.T) {
314+
msg := ConstExample{
315+
Summary: "testing",
316+
Description: "this is testing",
317+
}
318+
sv := validator.NewStructValidator()
319+
if err := sv.Validate(msg); err != nil {
320+
t.Errorf("Error in validation: %s", err)
321+
}
322+
}

0 commit comments

Comments
 (0)