Codebase list golang-github-go-playground-validator-v10 / a7e8a12
Merge pull request #128 from joeybloggs/v6-development Add ip, ipv4, ipv6 and mac validators Dean Karn 8 years ago
3 changed file(s) with 197 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
11
22 import (
33 "fmt"
4 "net"
45 "net/url"
56 "reflect"
67 "strconv"
6364 "latitude": isLatitude,
6465 "longitude": isLongitude,
6566 "ssn": isSSN,
67 "ipv4": isIPv4,
68 "ipv6": isIPv6,
69 "ip": isIP,
70 "mac": isMac,
71 }
72
73 func isMac(topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
74 _, err := net.ParseMAC(field.String())
75 return err == nil
76 }
77
78 func isIPv4(topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
79
80 ip := net.ParseIP(field.String())
81
82 return ip != nil && ip.To4() != nil
83 }
84
85 func isIPv6(topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
86 ip := net.ParseIP(field.String())
87
88 return ip != nil && ip.To4() == nil
89 }
90
91 func isIP(topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
92
93 ip := net.ParseIP(field.String())
94
95 return ip != nil
6696 }
6797
6898 func isSSN(topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
371371 This validates that a string value contains a valid U.S. Social Security Number.
372372 (Usage: ssn)
373373
374 ip
375 This validates that a string value contains a valid IP Adress.
376 (Usage: ip)
377
378 ipv4
379 This validates that a string value contains a valid v4 IP Adress.
380 (Usage: ipv4)
381
382 ipv6
383 This validates that a string value contains a valid v6 IP Adress.
384 (Usage: ipv6)
385
386 mac
387 This validates that a string value contains a valid MAC Adress defined
388 by go's ParseMAC accepted formats and types see:
389 http://golang.org/src/net/mac.go?s=866:918#L29
390 (Usage: mac)
391
374392 Validator notes:
375393
376394 regex
116116 NotEqualSkip(t, 2, val, nil)
117117 EqualSkip(t, 2, val.Field, field)
118118 EqualSkip(t, 2, val.Tag, expectedTag)
119 }
120
121 func TestMACValidation(t *testing.T) {
122 tests := []struct {
123 param string
124 expected bool
125 }{
126 {"3D:F2:C9:A6:B3:4F", true},
127 {"3D-F2-C9-A6-B3:4F", false},
128 {"123", false},
129 {"", false},
130 {"abacaba", false},
131 {"00:25:96:FF:FE:12:34:56", true},
132 {"0025:96FF:FE12:3456", false},
133 }
134
135 for i, test := range tests {
136
137 errs := validate.Field(test.param, "mac")
138
139 if test.expected == true {
140 if !IsEqual(errs, nil) {
141 t.Fatalf("Index: %d mac failed Error: %s", i, errs)
142 }
143 } else {
144 if IsEqual(errs, nil) {
145 t.Fatalf("Index: %d mac failed Error: %s", i, errs)
146 } else {
147 val := errs[""]
148 if val.Tag != "mac" {
149 t.Fatalf("Index: %d mac failed Error: %s", i, errs)
150 }
151 }
152 }
153 }
154 }
155
156 func TestIPValidation(t *testing.T) {
157 tests := []struct {
158 param string
159 expected bool
160 }{
161 {"10.0.0.1", true},
162 {"172.16.0.1", true},
163 {"192.168.0.1", true},
164 {"192.168.255.254", true},
165 {"192.168.255.256", false},
166 {"172.16.255.254", true},
167 {"172.16.256.255", false},
168 {"2001:cdba:0000:0000:0000:0000:3257:9652", true},
169 {"2001:cdba:0:0:0:0:3257:9652", true},
170 {"2001:cdba::3257:9652", true},
171 }
172
173 for i, test := range tests {
174
175 errs := validate.Field(test.param, "ip")
176
177 if test.expected == true {
178 if !IsEqual(errs, nil) {
179 t.Fatalf("Index: %d ip failed Error: %s", i, errs)
180 }
181 } else {
182 if IsEqual(errs, nil) {
183 t.Fatalf("Index: %d ip failed Error: %s", i, errs)
184 } else {
185 val := errs[""]
186 if val.Tag != "ip" {
187 t.Fatalf("Index: %d ip failed Error: %s", i, errs)
188 }
189 }
190 }
191 }
192 }
193
194 func TestIPv6Validation(t *testing.T) {
195 tests := []struct {
196 param string
197 expected bool
198 }{
199 {"10.0.0.1", false},
200 {"172.16.0.1", false},
201 {"192.168.0.1", false},
202 {"192.168.255.254", false},
203 {"192.168.255.256", false},
204 {"172.16.255.254", false},
205 {"172.16.256.255", false},
206 {"2001:cdba:0000:0000:0000:0000:3257:9652", true},
207 {"2001:cdba:0:0:0:0:3257:9652", true},
208 {"2001:cdba::3257:9652", true},
209 }
210
211 for i, test := range tests {
212
213 errs := validate.Field(test.param, "ipv6")
214
215 if test.expected == true {
216 if !IsEqual(errs, nil) {
217 t.Fatalf("Index: %d ipv6 failed Error: %s", i, errs)
218 }
219 } else {
220 if IsEqual(errs, nil) {
221 t.Fatalf("Index: %d ipv6 failed Error: %s", i, errs)
222 } else {
223 val := errs[""]
224 if val.Tag != "ipv6" {
225 t.Fatalf("Index: %d ipv6 failed Error: %s", i, errs)
226 }
227 }
228 }
229 }
230 }
231
232 func TestIPv4Validation(t *testing.T) {
233 tests := []struct {
234 param string
235 expected bool
236 }{
237 {"10.0.0.1", true},
238 {"172.16.0.1", true},
239 {"192.168.0.1", true},
240 {"192.168.255.254", true},
241 {"192.168.255.256", false},
242 {"172.16.255.254", true},
243 {"172.16.256.255", false},
244 {"2001:cdba:0000:0000:0000:0000:3257:9652", false},
245 {"2001:cdba:0:0:0:0:3257:9652", false},
246 {"2001:cdba::3257:9652", false},
247 }
248
249 for i, test := range tests {
250
251 errs := validate.Field(test.param, "ipv4")
252
253 if test.expected == true {
254 if !IsEqual(errs, nil) {
255 t.Fatalf("Index: %d ipv4 failed Error: %s", i, errs)
256 }
257 } else {
258 if IsEqual(errs, nil) {
259 t.Fatalf("Index: %d ipv4 failed Error: %s", i, errs)
260 } else {
261 val := errs[""]
262 if val.Tag != "ipv4" {
263 t.Fatalf("Index: %d ipv4 failed Error: %s", i, errs)
264 }
265 }
266 }
267 }
119268 }
120269
121270 func TestSliceMapArrayChanFuncPtrInterfaceRequiredValidation(t *testing.T) {