-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalidators_test.go
52 lines (48 loc) · 984 Bytes
/
validators_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
package goconf
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestStructValidator(t *testing.T) {
type config struct {
Name string `validate:"required"`
Age int `validate:"gte=0,lte=130"`
}
tests := map[string]struct {
config config
expectedErr string
}{
"required field available": {
config: config{
Name: "XX",
},
expectedErr: "",
},
"required field not available": {
config: config{},
expectedErr: "Error:Field validation",
},
"validation condition successful": {
config: config{
Name: "some name",
Age: 10,
},
expectedErr: "",
},
"validation condition failed": {
config: config{
Name: "some name",
Age: -5,
},
expectedErr: "Error:Field validation",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
err := StructValidator(test.config)
if test.expectedErr != "" {
require.ErrorContains(t, err, test.expectedErr)
}
})
}
}