Skip to content

Commit

Permalink
Merge pull request #152 from joostlawerman/master
Browse files Browse the repository at this point in the history
Add IsCIDR check
  • Loading branch information
asaskevich committed Oct 1, 2016
2 parents 593d645 + 719be5d commit 54c0a0d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,12 @@ func IsIPv6(str string) bool {
return ip != nil && strings.Contains(str, ":")
}

// IsCIDR check if the string is an valid CIDR notiation (IPV4 & IPV6)
func IsCIDR(str string) bool {
_, _, err := net.ParseCIDR(str)
return err == nil
}

// IsMAC check if a string is valid MAC address.
// Possible MAC formats:
// 01:23:45:67:89:ab
Expand Down
22 changes: 22 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2387,3 +2387,25 @@ func ExampleValidateStruct() {
}
println(result)
}

func TestIsCIDR(t *testing.T) {
t.Parallel()

var tests = []struct {
param string
expected bool
}{
{"193.168.3.20/7", true},
{"2001:db8::/32", true},
{"2001:0db8:85a3:0000:0000:8a2e:0370:7334/64", true},
{"193.138.3.20/60", false},
{"500.323.2.23/43", false},
{"", false},
}
for _, test := range tests {
actual := IsCIDR(test.param)
if actual != test.expected {
t.Errorf("Expected IsCIDR(%q) to be %v, got %v", test.param, test.expected, actual)
}
}
}

0 comments on commit 54c0a0d

Please sign in to comment.