forked from urfave/sflags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgovalidator_test.go
159 lines (151 loc) · 3.43 KB
/
govalidator_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package govalidator
import (
"flag"
"fmt"
"io"
"log"
"testing"
"github.com/stretchr/testify/assert"
"github.com/urfave/sflags"
"github.com/urfave/sflags/gen/gflag"
)
func ExampleNew() {
type config struct {
Host string `valid:"host"`
Port int `valid:"port"`
}
cfg := &config{
Host: "127.0.0.1",
Port: 6000,
}
// Use gflags.ParseToDef if you want default `flag.CommandLine`
fs, err := gflag.Parse(cfg, sflags.Validator(New()))
if err != nil {
log.Fatalf("err: %v", err)
}
fs.Init("text", flag.ContinueOnError)
fs.SetOutput(io.Discard)
// if we pass a wrong domain to the host flag, we'll get a error.
if err = fs.Parse([]string{"-host", "wrong domain"}); err != nil {
fmt.Printf("err: %v\n", err)
}
// if we pass a wrong port to the port flag, we'll get a error.
if err = fs.Parse([]string{"-port", "800000"}); err != nil {
fmt.Printf("err: %v\n", err)
}
// Output:
// err: invalid value "wrong domain" for flag -host: `wrong domain` does not validate as host
// err: invalid value "800000" for flag -port: `800000` does not validate as port
}
func Test_isValidTag(t *testing.T) {
tests := []struct {
arg string
want bool
}{
{"simple", true},
{"", false},
{"!#$%&()*+-./:<=>?@[]^_{|}~ ", true},
{"абв", true},
{"`", false},
}
for _, tt := range tests {
assert.Equal(t, tt.want, isValidTag(tt.arg), "for %v", tt.arg)
}
}
func Test_parseTagIntoMap(t *testing.T) {
tests := []struct {
tag string
want tagOptionsMap
}{
{
tag: "required~Some error message,length(2|3)",
want: tagOptionsMap{
"required": "Some error message",
"length(2|3)": "",
},
},
{
tag: "required~Some error message~other",
want: tagOptionsMap{
"required": "",
},
},
{
tag: "bad`tag,good_tag",
want: tagOptionsMap{
"good_tag": "",
},
},
}
for _, tt := range tests {
assert.Equal(t, tt.want, parseTagIntoMap(tt.tag), "for %v", tt.tag)
}
}
func Test_validateFunc(t *testing.T) {
tests := []struct {
val string
options tagOptionsMap
expErr string
}{
{
val: "not a host",
options: tagOptionsMap{"host": ""},
expErr: "`not a host` does not validate as host",
},
{
val: "localhost",
options: tagOptionsMap{"host": ""},
expErr: "",
},
{
val: "localhost",
options: tagOptionsMap{"!host": ""},
expErr: "`localhost` does validate as host",
},
{
val: "not a host",
options: tagOptionsMap{"host": "wrong host value"},
expErr: "wrong host value",
},
{
val: "localhost",
options: tagOptionsMap{"!host": "shouldn't be a host"},
expErr: "shouldn't be a host",
},
{
val: "localhost",
options: tagOptionsMap{"length(2|10)": ""},
expErr: "",
},
{
val: "localhostlong",
options: tagOptionsMap{"length(2|10)": ""},
expErr: "`localhostlong` does not validate as length(2|10)",
},
{
val: "localhostlong",
options: tagOptionsMap{"length(2|10)": "too long!"},
expErr: "too long!",
},
{
val: "localhost",
options: tagOptionsMap{"!length(2|10)": ""},
expErr: "`localhost` does validate as length(2|10)",
},
{
val: "localhost",
options: tagOptionsMap{"!length(2|10)": "should be longer"},
expErr: "should be longer",
},
}
for _, tt := range tests {
err := validateFunc(tt.val, tt.options)
if tt.expErr != "" {
if assert.Error(t, err) {
assert.EqualError(t, err, tt.expErr)
}
} else {
assert.NoError(t, err)
}
}
}