-
Notifications
You must be signed in to change notification settings - Fork 7
/
custom_test.go
185 lines (175 loc) · 6.19 KB
/
custom_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package validator
import (
"reflect"
"testing"
"github.com/stretchr/testify/require"
"go.uber.org/multierr"
)
type testCustomTagStruct struct {
E164NoPlus string `json:"e164_no_plus" validate:"e164noplus"`
EIN string `json:"ein" validate:"ein"`
EINB string `json:"ein_b" validate:"ein"`
USZIPCode string `json:"zip" validate:"zipcode"`
USZIPCodeB string `json:"zip_b" validate:"zipcode"`
Country string `json:"country" validate:"iso3166_1_alpha2"`
State string `json:"state" validate:"usstate"`
StateB string `json:"state_b" validate:"falseif=Country|usstate"`
StateC string `json:"state_c" validate:"falseif=Country US|usstate"`
StateD string `json:"state_d" validate:"falseif|usstate"`
StateE string `json:"state_e" validate:"falseif=Country US|usterritory"`
FalseIfMissing string `json:"falseif_string" validate:"falseif=MissingField"`
FieldArray []int `json:"field_array" validate:"required"`
FieldInt int `json:"field_int" validate:"required"`
FieldUint uint `json:"field_uint" validate:"required"`
FieldFloat float32 `json:"field_float" validate:"required"`
FieldBool bool `json:"field_bool" validate:"required"`
FieldInterface interface{} `json:"field_interface"`
FalseIfEmpty string `json:"falseif_empty" validate:"falseif"`
FalseIfArray string `json:"falseif_array" validate:"falseif=FieldArray 3|alpha"`
FalseIfInt string `json:"falseif_int" validate:"falseif=FieldInt -123|alpha"`
FalseIfUint string `json:"falseif_uint" validate:"falseif=FieldUint 123|alpha"`
FalseIfFloat string `json:"falseif_float" validate:"falseif=FieldFloat 1.23|alpha"`
FalseIfBool string `json:"falseif_bool" validate:"falseif=FieldBool true|alpha"`
FalseIfReqArray string `json:"falseif_req_array" validate:"falseif=FieldArray|alpha"`
FalseIfInterface string `json:"falseif_interface" validate:"falseif=FieldInterface 1|alpha"`
FieldOrTest string `json:"field_or_test" validate:"max=3|alpha"`
}
func getTestCustomTagData() testCustomTagStruct {
return testCustomTagStruct{
E164NoPlus: "123456789012345",
EIN: "12-3456789",
EINB: "123456789",
USZIPCode: "12345",
USZIPCodeB: "12345-1234",
Country: "US",
State: "NY",
StateB: "AL",
StateC: "WI",
StateD: "AK",
StateE: "VI",
FalseIfMissing: "hello",
FieldArray: []int{1, 2, 3},
FieldInt: -123,
FieldUint: 123,
FieldFloat: 1.23,
FieldBool: true,
FalseIfEmpty: "X",
FalseIfArray: "A",
FalseIfInt: "B",
FalseIfUint: "C",
FalseIfFloat: "D",
FalseIfBool: "E",
FalseIfReqArray: "F",
FalseIfInterface: "G",
FieldOrTest: "123",
}
}
func TestCustomTags(t *testing.T) {
t.Parallel()
tests := []struct {
name string
fobj func(obj testCustomTagStruct) testCustomTagStruct
wantErr bool
wantErrCount int
}{
{
name: "success",
fobj: func(obj testCustomTagStruct) testCustomTagStruct { return obj },
wantErr: false,
wantErrCount: 0,
},
{
name: "fail with invalid e164noplus",
fobj: func(obj testCustomTagStruct) testCustomTagStruct { obj.E164NoPlus = "+123456789012345"; return obj },
wantErr: true,
wantErrCount: 1,
},
{
name: "fail with invalid ein",
fobj: func(obj testCustomTagStruct) testCustomTagStruct { obj.EIN = "12-345-56789"; return obj },
wantErr: true,
wantErrCount: 1,
},
{
name: "fail with invalid zip code",
fobj: func(obj testCustomTagStruct) testCustomTagStruct { obj.USZIPCode = "1234"; return obj },
wantErr: true,
wantErrCount: 1,
},
{
name: "fail with invalid US state",
fobj: func(obj testCustomTagStruct) testCustomTagStruct { obj.State = "XX"; return obj },
wantErr: true,
wantErrCount: 1,
},
{
name: "fail with invalid required US state",
fobj: func(obj testCustomTagStruct) testCustomTagStruct { obj.StateB = "XX"; return obj },
wantErr: true,
wantErrCount: 1,
},
{
name: "fail with invalid US state when country is not set",
fobj: func(obj testCustomTagStruct) testCustomTagStruct { obj.Country = ""; obj.StateB = "XX"; return obj },
wantErr: true,
wantErrCount: 1,
},
{
name: "fail with invalid US territory",
fobj: func(obj testCustomTagStruct) testCustomTagStruct { obj.StateE = "XX"; return obj },
wantErr: true,
wantErrCount: 1,
},
{
name: "pass with non US state",
fobj: func(obj testCustomTagStruct) testCustomTagStruct {
obj.Country = "GB"
obj.StateC = "England"
return obj
},
wantErr: false,
wantErrCount: 0,
},
{
name: "pass with US state and non-US country",
fobj: func(obj testCustomTagStruct) testCustomTagStruct {
obj.Country = "GB"
obj.StateC = "NY"
return obj
},
wantErr: false,
wantErrCount: 0,
},
{
name: "fail with or tags",
fobj: func(obj testCustomTagStruct) testCustomTagStruct { obj.FieldOrTest = "1234"; return obj },
wantErr: true,
wantErrCount: 2,
},
}
opts := []Option{
WithFieldNameTag("json"),
WithCustomValidationTags(CustomValidationTags()),
WithErrorTemplates(ErrorTemplates()),
}
v, err := New(opts...)
require.NoError(t, err, "New() unexpected error = %v", err)
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
s := tt.fobj(getTestCustomTagData())
err := v.ValidateStruct(s)
require.Equal(t, tt.wantErr, err != nil, "error = %v, wantErr %v", err, tt.wantErr)
errs := multierr.Errors(err)
require.Equal(t, tt.wantErrCount, len(errs), "errors: %+v", errs)
})
}
}
func Test_hasDefaultValue_invalid(t *testing.T) {
t.Parallel()
var i interface{}
vi := reflect.ValueOf(i)
got := hasDefaultValue(vi, vi.Kind(), true)
require.True(t, got, "Expecting true value")
}