Skip to content

Commit 1708468

Browse files
Dean KarnDean Karn
authored andcommitted
Merge pull request #193 from krhubert/v8
add cidr, cidrv4 and cidrv6 validators
2 parents ea31d3e + d015334 commit 1708468

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

baked_in.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ var bakedInValidators = map[string]Func{
8181
"ipv4": isIPv4,
8282
"ipv6": isIPv6,
8383
"ip": isIP,
84+
"cidrv4": isCIDRv4,
85+
"cidrv6": isCIDRv6,
86+
"cidr": isCIDR,
8487
"mac": isMac,
8588
}
8689

@@ -89,6 +92,27 @@ func isMac(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Va
8992
return err == nil
9093
}
9194

95+
func isCIDRv4(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
96+
97+
ip, _, err := net.ParseCIDR(field.String())
98+
99+
return err == nil && ip.To4() != nil
100+
}
101+
102+
func isCIDRv6(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
103+
104+
ip, _, err := net.ParseCIDR(field.String())
105+
106+
return err == nil && ip.To4() == nil
107+
}
108+
109+
func isCIDR(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
110+
111+
_, _, err := net.ParseCIDR(field.String())
112+
113+
return err == nil
114+
}
115+
92116
func isIPv4(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
93117

94118
ip := net.ParseIP(field.String())

doc.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,18 @@ Here is a list of the current built in validators:
452452
This validates that a string value contains a valid v6 IP Adress.
453453
(Usage: ipv6)
454454
455+
cidr
456+
This validates that a string value contains a valid CIDR Adress.
457+
(Usage: cidr)
458+
459+
cidrv4
460+
This validates that a string value contains a valid v4 CIDR Adress.
461+
(Usage: cidrv4)
462+
463+
cidrv6
464+
This validates that a string value contains a valid v6 CIDR Adress.
465+
(Usage: cidrv6)
466+
455467
mac
456468
This validates that a string value contains a valid MAC Adress defined
457469
by go's ParseMAC accepted formats and types see:

validator_test.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,129 @@ func TestIPv4Validation(t *testing.T) {
15421542
}
15431543
}
15441544

1545+
func TestCIDRValidation(t *testing.T) {
1546+
tests := []struct {
1547+
param string
1548+
expected bool
1549+
}{
1550+
{"10.0.0.0/0", true},
1551+
{"10.0.0.1/8", true},
1552+
{"172.16.0.1/16", true},
1553+
{"192.168.0.1/24", true},
1554+
{"192.168.255.254/24", true},
1555+
{"192.168.255.254/48", false},
1556+
{"192.168.255.256/24", false},
1557+
{"172.16.255.254/16", true},
1558+
{"172.16.256.255/16", false},
1559+
{"2001:cdba:0000:0000:0000:0000:3257:9652/64", true},
1560+
{"2001:cdba:0000:0000:0000:0000:3257:9652/256", false},
1561+
{"2001:cdba:0:0:0:0:3257:9652/32", true},
1562+
{"2001:cdba::3257:9652/16", true},
1563+
}
1564+
1565+
for i, test := range tests {
1566+
1567+
errs := validate.Field(test.param, "cidr")
1568+
1569+
if test.expected == true {
1570+
if !IsEqual(errs, nil) {
1571+
t.Fatalf("Index: %d cidr failed Error: %s", i, errs)
1572+
}
1573+
} else {
1574+
if IsEqual(errs, nil) {
1575+
t.Fatalf("Index: %d cidr failed Error: %s", i, errs)
1576+
} else {
1577+
val := errs.(ValidationErrors)[""]
1578+
if val.Tag != "cidr" {
1579+
t.Fatalf("Index: %d cidr failed Error: %s", i, errs)
1580+
}
1581+
}
1582+
}
1583+
}
1584+
}
1585+
1586+
func TestCIDRv6Validation(t *testing.T) {
1587+
tests := []struct {
1588+
param string
1589+
expected bool
1590+
}{
1591+
{"10.0.0.0/0", false},
1592+
{"10.0.0.1/8", false},
1593+
{"172.16.0.1/16", false},
1594+
{"192.168.0.1/24", false},
1595+
{"192.168.255.254/24", false},
1596+
{"192.168.255.254/48", false},
1597+
{"192.168.255.256/24", false},
1598+
{"172.16.255.254/16", false},
1599+
{"172.16.256.255/16", false},
1600+
{"2001:cdba:0000:0000:0000:0000:3257:9652/64", true},
1601+
{"2001:cdba:0000:0000:0000:0000:3257:9652/256", false},
1602+
{"2001:cdba:0:0:0:0:3257:9652/32", true},
1603+
{"2001:cdba::3257:9652/16", true},
1604+
}
1605+
1606+
for i, test := range tests {
1607+
1608+
errs := validate.Field(test.param, "cidrv6")
1609+
1610+
if test.expected == true {
1611+
if !IsEqual(errs, nil) {
1612+
t.Fatalf("Index: %d cidrv6 failed Error: %s", i, errs)
1613+
}
1614+
} else {
1615+
if IsEqual(errs, nil) {
1616+
t.Fatalf("Index: %d cidrv6 failed Error: %s", i, errs)
1617+
} else {
1618+
val := errs.(ValidationErrors)[""]
1619+
if val.Tag != "cidrv6" {
1620+
t.Fatalf("Index: %d cidrv6 failed Error: %s", i, errs)
1621+
}
1622+
}
1623+
}
1624+
}
1625+
}
1626+
1627+
func TestCIDRv4Validation(t *testing.T) {
1628+
tests := []struct {
1629+
param string
1630+
expected bool
1631+
}{
1632+
{"10.0.0.0/0", true},
1633+
{"10.0.0.1/8", true},
1634+
{"172.16.0.1/16", true},
1635+
{"192.168.0.1/24", true},
1636+
{"192.168.255.254/24", true},
1637+
{"192.168.255.254/48", false},
1638+
{"192.168.255.256/24", false},
1639+
{"172.16.255.254/16", true},
1640+
{"172.16.256.255/16", false},
1641+
{"2001:cdba:0000:0000:0000:0000:3257:9652/64", false},
1642+
{"2001:cdba:0000:0000:0000:0000:3257:9652/256", false},
1643+
{"2001:cdba:0:0:0:0:3257:9652/32", false},
1644+
{"2001:cdba::3257:9652/16", false},
1645+
}
1646+
1647+
for i, test := range tests {
1648+
1649+
errs := validate.Field(test.param, "cidrv4")
1650+
1651+
if test.expected == true {
1652+
if !IsEqual(errs, nil) {
1653+
t.Fatalf("Index: %d cidrv4 failed Error: %s", i, errs)
1654+
}
1655+
} else {
1656+
if IsEqual(errs, nil) {
1657+
t.Fatalf("Index: %d cidrv4 failed Error: %s", i, errs)
1658+
} else {
1659+
val := errs.(ValidationErrors)[""]
1660+
if val.Tag != "cidrv4" {
1661+
t.Fatalf("Index: %d cidrv4 failed Error: %s", i, errs)
1662+
}
1663+
}
1664+
}
1665+
}
1666+
}
1667+
15451668
func TestSliceMapArrayChanFuncPtrInterfaceRequiredValidation(t *testing.T) {
15461669

15471670
var m map[string]string

0 commit comments

Comments
 (0)