-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuilder_test.go
139 lines (126 loc) · 3.48 KB
/
builder_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package valigo
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/insei/valigo/shared"
)
func TestBuilderWhen(t *testing.T) {
type TestStruct struct {
Field string
}
obj := &TestStruct{
Field: "test",
}
validator = New()
bld := configure[TestStruct](validator, obj, func(ctx context.Context, obj any) bool {
return true
})
bld.When(func(ctx context.Context, obj *TestStruct) bool {
return (*obj).Field == "test"
}).String(&obj.Field).Required()
assert.Equal(t, 1, len(validator.storage.validators))
}
func TestBuilderCustom(t *testing.T) {
type TestStruct struct {
Field string
}
obj := &TestStruct{
Field: "test",
}
vld := New()
bld := configure[*TestStruct](vld, obj, func(ctx context.Context, obj any) bool {
return true
})
bld.Custom(func(ctx context.Context, h shared.StructCustomHelper, obj **TestStruct) []shared.Error {
if (*obj).Field != "test" {
return []shared.Error{h.ErrorT(ctx, &(*obj).Field, (*obj).Field, "validation:string:Field is not 'test'")}
}
return nil
})
assert.Equal(t, 1, len(vld.storage.validators))
}
func TestBuilderWhenAndCustomMultipleConditions(t *testing.T) {
type TestStruct struct {
Field1 string
Field2 string
}
obj := &TestStruct{
Field1: "test1",
Field2: "test2",
}
vld := New()
bld := configure[TestStruct](vld, obj, func(ctx context.Context, obj any) bool {
return true
})
bld.When(func(ctx context.Context, obj *TestStruct) bool {
return (*obj).Field1 == "test1"
}).When(func(ctx context.Context, obj *TestStruct) bool {
return (*obj).Field2 == "test2"
}).Custom(func(ctx context.Context, h shared.StructCustomHelper, obj *TestStruct) []shared.Error {
if (*obj).Field1 != "test1" {
return []shared.Error{h.ErrorT(ctx, &(*obj).Field1, (*obj).Field1, "validation:string:Field1 is not 'test1'")}
}
if (*obj).Field2 != "test2" {
return []shared.Error{h.ErrorT(ctx, &(*obj).Field2, (*obj).Field2, "validation:string:Field2 is not 'test2'")}
}
return nil
})
assert.Equal(t, 1, len(vld.storage.validators))
}
func TestBuilderCustomMultipleErrors(t *testing.T) {
type TestStruct struct {
Field1 string
Field2 string
}
obj := &TestStruct{
Field1: "test1",
Field2: "test2",
}
vld := New()
bld := configure[*TestStruct](vld, obj, func(ctx context.Context, obj any) bool {
return true
})
bld.Custom(func(ctx context.Context, h shared.StructCustomHelper, obj **TestStruct) []shared.Error {
errs := make([]shared.Error, 0)
if (*obj).Field1 != "test1" {
errs = append(errs, h.ErrorT(ctx, &(*obj).Field1, (*obj).Field1, "validation:string:Field1 is not 'test1'"))
}
if (*obj).Field2 != "test2" {
errs = append(errs, h.ErrorT(ctx, &(*obj).Field2, (*obj).Field2, "validation:string:Field2 is not 'test2'"))
}
return errs
})
assert.Equal(t, 1, len(vld.storage.validators))
}
func TestConfigurePanic(t *testing.T) {
type TestStruct struct {
Field1 string
}
obj := &TestStruct{
Field1: "test1",
}
vld := New()
defer func() {
if r := recover(); r == nil {
t.Errorf("configure did not panic when GetFrom returned an error")
}
}()
configure[TestStruct](vld, &obj, nil)
}
func TestBuilder_Slice(t *testing.T) {
type TestStruct struct {
Slice []string
PtrInt float32
}
obj := &TestStruct{
Slice: []string{" test1 ", " test2 "},
PtrInt: 44,
}
vld := New()
bld := configure[*TestStruct](vld, obj, nil)
bld.Slice(&obj.Slice).MaxLen(1)
bld.Number(&obj.PtrInt).Max(float32(22.22))
err := vld.Validate(context.Background(), obj)
_ = err
}