Codebase list golang-github-go-playground-validator-v10 / 4c79e69
Merge pull request #234 from go-playground/krhubert Add validation functions for tcp/udp/ip/unix addresses Dean Karn 8 years ago
3 changed file(s) with 538 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
8383 "cidrv4": IsCIDRv4,
8484 "cidrv6": IsCIDRv6,
8585 "cidr": IsCIDR,
86 "tcp4_addr": IsTCP4AddrResolvable,
87 "tcp6_addr": IsTCP6AddrResolvable,
88 "tcp_addr": IsTCPAddrResolvable,
89 "udp4_addr": IsUDP4AddrResolvable,
90 "udp6_addr": IsUDP6AddrResolvable,
91 "udp_addr": IsUDPAddrResolvable,
92 "ip4_addr": IsIP4AddrResolvable,
93 "ip6_addr": IsIP6AddrResolvable,
94 "ip_addr": IsIPAddrResolvable,
95 "unix_addr": IsUnixAddrResolvable,
8696 "mac": IsMAC,
8797 }
8898
12351245 func HasMaxOf(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
12361246 return IsLte(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
12371247 }
1248
1249 // IsTCP4AddrResolvable is the validation function for validating if the field's value is a resolvable tcp4 address.
1250 // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
1251 func IsTCP4AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
1252
1253 if !isIP4Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
1254 return false
1255 }
1256
1257 _, err := net.ResolveTCPAddr("tcp4", field.String())
1258 return err == nil
1259 }
1260
1261 // IsTCP6AddrResolvable is the validation function for validating if the field's value is a resolvable tcp6 address.
1262 // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
1263 func IsTCP6AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
1264
1265 if !isIP6Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
1266 return false
1267 }
1268
1269 _, err := net.ResolveTCPAddr("tcp6", field.String())
1270 return err == nil
1271 }
1272
1273 // IsTCPAddrResolvable is the validation function for validating if the field's value is a resolvable tcp address.
1274 // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
1275 func IsTCPAddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
1276
1277 if !isIP4Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) &&
1278 !isIP6Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
1279 return false
1280 }
1281
1282 _, err := net.ResolveTCPAddr("tcp", field.String())
1283 return err == nil
1284 }
1285
1286 // IsUDP4AddrResolvable is the validation function for validating if the field's value is a resolvable udp4 address.
1287 // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
1288 func IsUDP4AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
1289
1290 if !isIP4Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
1291 return false
1292 }
1293
1294 _, err := net.ResolveUDPAddr("udp4", field.String())
1295 return err == nil
1296 }
1297
1298 // IsUDP6AddrResolvable is the validation function for validating if the field's value is a resolvable udp6 address.
1299 // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
1300 func IsUDP6AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
1301
1302 if !isIP6Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
1303 return false
1304 }
1305
1306 _, err := net.ResolveUDPAddr("udp6", field.String())
1307 return err == nil
1308 }
1309
1310 // IsUDPAddrResolvable is the validation function for validating if the field's value is a resolvable udp address.
1311 // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
1312 func IsUDPAddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
1313
1314 if !isIP4Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) &&
1315 !isIP6Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
1316 return false
1317 }
1318
1319 _, err := net.ResolveUDPAddr("udp", field.String())
1320 return err == nil
1321 }
1322
1323 // IsIP4AddrResolvable is the validation function for validating if the field's value is a resolvable ip4 address.
1324 // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
1325 func IsIP4AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
1326
1327 if !IsIPv4(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
1328 return false
1329 }
1330
1331 _, err := net.ResolveIPAddr("ip4", field.String())
1332 return err == nil
1333 }
1334
1335 // IsIP6AddrResolvable is the validation function for validating if the field's value is a resolvable ip6 address.
1336 // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
1337 func IsIP6AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
1338
1339 if !IsIPv6(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
1340 return false
1341 }
1342
1343 _, err := net.ResolveIPAddr("ip6", field.String())
1344 return err == nil
1345 }
1346
1347 // IsIPAddrResolvable is the validation function for validating if the field's value is a resolvable ip address.
1348 // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
1349 func IsIPAddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
1350
1351 if !IsIP(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
1352 return false
1353 }
1354
1355 _, err := net.ResolveIPAddr("ip", field.String())
1356 return err == nil
1357 }
1358
1359 // IsUnixAddrResolvable is the validation function for validating if the field's value is a resolvable unix address.
1360 // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
1361 func IsUnixAddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
1362 _, err := net.ResolveUnixAddr("unix", field.String())
1363 return err == nil
1364 }
1365
1366 func isIP4Addr(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
1367 val := field.String()
1368
1369 if idx := strings.LastIndex(val, ":"); idx != -1 {
1370 val = val[0:idx]
1371 }
1372
1373 if !IsIPv4(v, topStruct, currentStructOrField, reflect.ValueOf(val), fieldType, fieldKind, param) {
1374 return false
1375 }
1376
1377 return true
1378 }
1379
1380 func isIP6Addr(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
1381 val := field.String()
1382
1383 if idx := strings.LastIndex(val, ":"); idx != -1 {
1384 if idx != 0 && val[idx-1:idx] == "]" {
1385 val = val[1 : idx-1]
1386 }
1387 }
1388
1389 if !IsIPv6(v, topStruct, currentStructOrField, reflect.ValueOf(val), fieldType, fieldKind, param) {
1390 return false
1391 }
1392
1393 return true
1394 }
734734
735735 Usage: cidrv6
736736
737 Transmission Control Protocol Address TCP
738
739 This validates that a string value contains a valid resolvable TCP Adress.
740
741 Usage: tcp_addr
742
743 Transmission Control Protocol Address TCPv4
744
745 This validates that a string value contains a valid resolvable v4 TCP Adress.
746
747 Usage: tcp4_addr
748
749 Transmission Control Protocol Address TCPv6
750
751 This validates that a string value contains a valid resolvable v6 TCP Adress.
752
753 Usage: tcp6_addr
754
755 User Datagram Protocol Address UDP
756
757 This validates that a string value contains a valid resolvable UDP Adress.
758
759 Usage: udp_addr
760
761 User Datagram Protocol Address UDPv4
762
763 This validates that a string value contains a valid resolvable v4 UDP Adress.
764
765 Usage: udp4_addr
766
767 User Datagram Protocol Address UDPv6
768
769 This validates that a string value contains a valid resolvable v6 UDP Adress.
770
771 Usage: udp6_addr
772
773 Internet Protocol Address IP
774
775 This validates that a string value contains a valid resolvable IP Adress.
776
777 Usage: ip_addr
778
779 Internet Protocol Address IPv4
780
781 This validates that a string value contains a valid resolvable v4 IP Adress.
782
783 Usage: ip4_addr
784
785 Internet Protocol Address IPv6
786
787 This validates that a string value contains a valid resolvable v6 IP Adress.
788
789 Usage: ip6_addr
790
791 Unix domain socket end point Address
792
793 This validates that a string value contains a valid Unix Adress.
794
795 Usage: unix_addr
796
737797 Media Access Control Address MAC
738798
739799 This validates that a string value contains a valid MAC Adress.
743803 Note: See Go's ParseMAC for accepted formats and types:
744804
745805 http://golang.org/src/net/mac.go?s=866:918#L29
746
747 Usage: mac
748806
749807 Alias Validators and Tags
750808
17541754 param string
17551755 expected bool
17561756 }{
1757 {"", false},
17571758 {"10.0.0.1", true},
17581759 {"172.16.0.1", true},
17591760 {"192.168.0.1", true},
19801981 val := errs.(ValidationErrors)[""]
19811982 if val.Tag != "cidrv4" {
19821983 t.Fatalf("Index: %d cidrv4 failed Error: %s", i, errs)
1984 }
1985 }
1986 }
1987 }
1988 }
1989
1990 func TestTCPAddrValidation(t *testing.T) {
1991 tests := []struct {
1992 param string
1993 expected bool
1994 }{
1995 {"", false},
1996 {":80", false},
1997 {"127.0.0.1:80", true},
1998 {"[::1]:80", true},
1999 {"256.0.0.0:1", false},
2000 {"[::1]", false},
2001 }
2002
2003 for i, test := range tests {
2004 errs := validate.Field(test.param, "tcp_addr")
2005 if test.expected == true {
2006 if !IsEqual(errs, nil) {
2007 t.Fatalf("Index: %d tcp_addr failed Error: %s", i, errs)
2008 }
2009 } else {
2010 if IsEqual(errs, nil) {
2011 t.Fatalf("Index: %d tcp_addr failed Error: %s", i, errs)
2012 } else {
2013 val := errs.(ValidationErrors)[""]
2014 if val.Tag != "tcp_addr" {
2015 t.Fatalf("Index: %d tcp_addr failed Error: %s", i, errs)
2016 }
2017 }
2018 }
2019 }
2020 }
2021
2022 func TestTCP6AddrValidation(t *testing.T) {
2023 tests := []struct {
2024 param string
2025 expected bool
2026 }{
2027 {"", false},
2028 {":80", false},
2029 {"127.0.0.1:80", false},
2030 {"[::1]:80", true},
2031 {"256.0.0.0:1", false},
2032 {"[::1]", false},
2033 }
2034
2035 for i, test := range tests {
2036 errs := validate.Field(test.param, "tcp6_addr")
2037 if test.expected == true {
2038 if !IsEqual(errs, nil) {
2039 t.Fatalf("Index: %d tcp6_addr failed Error: %s", i, errs)
2040 }
2041 } else {
2042 if IsEqual(errs, nil) {
2043 t.Fatalf("Index: %d tcp6_addr failed Error: %s", i, errs)
2044 } else {
2045 val := errs.(ValidationErrors)[""]
2046 if val.Tag != "tcp6_addr" {
2047 t.Fatalf("Index: %d tcp6_addr failed Error: %s", i, errs)
2048 }
2049 }
2050 }
2051 }
2052 }
2053
2054 func TestTCP4AddrValidation(t *testing.T) {
2055 tests := []struct {
2056 param string
2057 expected bool
2058 }{
2059 {"", false},
2060 {":80", false},
2061 {"127.0.0.1:80", true},
2062 {"[::1]:80", false}, // https://github.com/golang/go/issues/14037
2063 {"256.0.0.0:1", false},
2064 {"[::1]", false},
2065 }
2066
2067 for i, test := range tests {
2068 errs := validate.Field(test.param, "tcp4_addr")
2069 if test.expected == true {
2070 if !IsEqual(errs, nil) {
2071 t.Fatalf("Index: %d tcp4_addr failed Error: %s", i, errs)
2072 }
2073 } else {
2074 if IsEqual(errs, nil) {
2075 t.Log(test.param, IsEqual(errs, nil))
2076 t.Fatalf("Index: %d tcp4_addr failed Error: %s", i, errs)
2077 } else {
2078 val := errs.(ValidationErrors)[""]
2079 if val.Tag != "tcp4_addr" {
2080 t.Fatalf("Index: %d tcp4_addr failed Error: %s", i, errs)
2081 }
2082 }
2083 }
2084 }
2085 }
2086
2087 func TestUDPAddrValidation(t *testing.T) {
2088 tests := []struct {
2089 param string
2090 expected bool
2091 }{
2092 {"", false},
2093 {":80", false},
2094 {"127.0.0.1:80", true},
2095 {"[::1]:80", true},
2096 {"256.0.0.0:1", false},
2097 {"[::1]", false},
2098 }
2099
2100 for i, test := range tests {
2101 errs := validate.Field(test.param, "udp_addr")
2102 if test.expected == true {
2103 if !IsEqual(errs, nil) {
2104 t.Fatalf("Index: %d udp_addr failed Error: %s", i, errs)
2105 }
2106 } else {
2107 if IsEqual(errs, nil) {
2108 t.Fatalf("Index: %d udp_addr failed Error: %s", i, errs)
2109 } else {
2110 val := errs.(ValidationErrors)[""]
2111 if val.Tag != "udp_addr" {
2112 t.Fatalf("Index: %d udp_addr failed Error: %s", i, errs)
2113 }
2114 }
2115 }
2116 }
2117 }
2118
2119 func TestUDP6AddrValidation(t *testing.T) {
2120 tests := []struct {
2121 param string
2122 expected bool
2123 }{
2124 {"", false},
2125 {":80", false},
2126 {"127.0.0.1:80", false},
2127 {"[::1]:80", true},
2128 {"256.0.0.0:1", false},
2129 {"[::1]", false},
2130 }
2131
2132 for i, test := range tests {
2133 errs := validate.Field(test.param, "udp6_addr")
2134 if test.expected == true {
2135 if !IsEqual(errs, nil) {
2136 t.Fatalf("Index: %d udp6_addr failed Error: %s", i, errs)
2137 }
2138 } else {
2139 if IsEqual(errs, nil) {
2140 t.Fatalf("Index: %d udp6_addr failed Error: %s", i, errs)
2141 } else {
2142 val := errs.(ValidationErrors)[""]
2143 if val.Tag != "udp6_addr" {
2144 t.Fatalf("Index: %d udp6_addr failed Error: %s", i, errs)
2145 }
2146 }
2147 }
2148 }
2149 }
2150
2151 func TestUDP4AddrValidation(t *testing.T) {
2152 tests := []struct {
2153 param string
2154 expected bool
2155 }{
2156 {"", false},
2157 {":80", false},
2158 {"127.0.0.1:80", true},
2159 {"[::1]:80", false}, // https://github.com/golang/go/issues/14037
2160 {"256.0.0.0:1", false},
2161 {"[::1]", false},
2162 }
2163
2164 for i, test := range tests {
2165 errs := validate.Field(test.param, "udp4_addr")
2166 if test.expected == true {
2167 if !IsEqual(errs, nil) {
2168 t.Fatalf("Index: %d udp4_addr failed Error: %s", i, errs)
2169 }
2170 } else {
2171 if IsEqual(errs, nil) {
2172 t.Log(test.param, IsEqual(errs, nil))
2173 t.Fatalf("Index: %d udp4_addr failed Error: %s", i, errs)
2174 } else {
2175 val := errs.(ValidationErrors)[""]
2176 if val.Tag != "udp4_addr" {
2177 t.Fatalf("Index: %d udp4_addr failed Error: %s", i, errs)
2178 }
2179 }
2180 }
2181 }
2182 }
2183
2184 func TestIPAddrValidation(t *testing.T) {
2185 tests := []struct {
2186 param string
2187 expected bool
2188 }{
2189 {"", false},
2190 {"127.0.0.1", true},
2191 {"127.0.0.1:80", false},
2192 {"::1", true},
2193 {"256.0.0.0", false},
2194 {"localhost", false},
2195 }
2196
2197 for i, test := range tests {
2198 errs := validate.Field(test.param, "ip_addr")
2199 if test.expected == true {
2200 if !IsEqual(errs, nil) {
2201 t.Fatalf("Index: %d ip_addr failed Error: %s", i, errs)
2202 }
2203 } else {
2204 if IsEqual(errs, nil) {
2205 t.Fatalf("Index: %d ip_addr failed Error: %s", i, errs)
2206 } else {
2207 val := errs.(ValidationErrors)[""]
2208 if val.Tag != "ip_addr" {
2209 t.Fatalf("Index: %d ip_addr failed Error: %s", i, errs)
2210 }
2211 }
2212 }
2213 }
2214 }
2215
2216 func TestIP6AddrValidation(t *testing.T) {
2217 tests := []struct {
2218 param string
2219 expected bool
2220 }{
2221 {"", false},
2222 {"127.0.0.1", false}, // https://github.com/golang/go/issues/14037
2223 {"127.0.0.1:80", false},
2224 {"::1", true},
2225 {"0:0:0:0:0:0:0:1", true},
2226 {"256.0.0.0", false},
2227 }
2228
2229 for i, test := range tests {
2230 errs := validate.Field(test.param, "ip6_addr")
2231 if test.expected == true {
2232 if !IsEqual(errs, nil) {
2233 t.Fatalf("Index: %d ip6_addr failed Error: %s", i, errs)
2234 }
2235 } else {
2236 if IsEqual(errs, nil) {
2237 t.Fatalf("Index: %d ip6_addr failed Error: %s", i, errs)
2238 } else {
2239 val := errs.(ValidationErrors)[""]
2240 if val.Tag != "ip6_addr" {
2241 t.Fatalf("Index: %d ip6_addr failed Error: %s", i, errs)
2242 }
2243 }
2244 }
2245 }
2246 }
2247
2248 func TestIP4AddrValidation(t *testing.T) {
2249 tests := []struct {
2250 param string
2251 expected bool
2252 }{
2253 {"", false},
2254 {"127.0.0.1", true},
2255 {"127.0.0.1:80", false},
2256 {"::1", false}, // https://github.com/golang/go/issues/14037
2257 {"256.0.0.0", false},
2258 {"localhost", false},
2259 }
2260
2261 for i, test := range tests {
2262 errs := validate.Field(test.param, "ip4_addr")
2263 if test.expected == true {
2264 if !IsEqual(errs, nil) {
2265 t.Fatalf("Index: %d ip4_addr failed Error: %s", i, errs)
2266 }
2267 } else {
2268 if IsEqual(errs, nil) {
2269 t.Log(test.param, IsEqual(errs, nil))
2270 t.Fatalf("Index: %d ip4_addr failed Error: %s", i, errs)
2271 } else {
2272 val := errs.(ValidationErrors)[""]
2273 if val.Tag != "ip4_addr" {
2274 t.Fatalf("Index: %d ip4_addr failed Error: %s", i, errs)
2275 }
2276 }
2277 }
2278 }
2279 }
2280
2281 func TestUnixAddrValidation(t *testing.T) {
2282 tests := []struct {
2283 param string
2284 expected bool
2285 }{
2286 {"", true},
2287 {"v.sock", true},
2288 }
2289
2290 for i, test := range tests {
2291 errs := validate.Field(test.param, "unix_addr")
2292 if test.expected == true {
2293 if !IsEqual(errs, nil) {
2294 t.Fatalf("Index: %d unix_addr failed Error: %s", i, errs)
2295 }
2296 } else {
2297 if IsEqual(errs, nil) {
2298 t.Log(test.param, IsEqual(errs, nil))
2299 t.Fatalf("Index: %d unix_addr failed Error: %s", i, errs)
2300 } else {
2301 val := errs.(ValidationErrors)[""]
2302 if val.Tag != "unix_addr" {
2303 t.Fatalf("Index: %d unix_addr failed Error: %s", i, errs)
19832304 }
19842305 }
19852306 }