Skip to content

Commit

Permalink
helper/validation: IP address and IP address range validation helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
asarfaty authored and apparentlymart committed Mar 29, 2018
1 parent d4eb604 commit dfa6232
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
46 changes: 46 additions & 0 deletions helper/validation/validation.go
Original file line number Diff line number Diff line change
@@ -1,6 1,7 @@
package validation

import (
"bytes"
"fmt"
"net"
"reflect"
Expand Down Expand Up @@ -180,6 181,51 @@ func CIDRNetwork(min, max int) schema.SchemaValidateFunc {
}
}

// SingleIP returns a SchemaValidateFunc which tests if the provided value
// is of type string, and in valid single IP notation
func SingleIP() schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(string)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return
}

ip := net.ParseIP(v)
if ip == nil {
es = append(es, fmt.Errorf(
"expected %s to contain a valid IP, got: %s", k, v))
}
return
}
}

// IPRange returns a SchemaValidateFunc which tests if the provided value
// is of type string, and in valid IP range notation
func IPRange() schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(string)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return
}

ips := strings.Split(v, "-")
if len(ips) != 2 {
es = append(es, fmt.Errorf(
"expected %s to contain a valid IP range, got: %s", k, v))
return
}
ip1 := net.ParseIP(ips[0])
ip2 := net.ParseIP(ips[1])
if ip1 == nil || ip2 == nil || bytes.Compare(ip1, ip2) > 0 {
es = append(es, fmt.Errorf(
"expected %s to contain a valid IP range, got: %s", k, v))
}
return
}
}

// ValidateJsonString is a SchemaValidateFunc which tests to make sure the
// supplied string is valid JSON.
func ValidateJsonString(v interface{}, k string) (ws []string, errors []error) {
Expand Down
43 changes: 43 additions & 0 deletions helper/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 144,49 @@ func TestValidationRegexp(t *testing.T) {
})
}

func TestValidationSingleIP(t *testing.T) {
runTestCases(t, []testCase{
{
val: "172.10.10.10",
f: SingleIP(),
},
{
val: "1.1.1",
f: SingleIP(),
expectedErr: regexp.MustCompile(regexp.QuoteMeta("expected test_property to contain a valid IP, got:")),
},
{
val: "1.1.1.0/20",
f: SingleIP(),
expectedErr: regexp.MustCompile(regexp.QuoteMeta("expected test_property to contain a valid IP, got:")),
},
{
val: "256.1.1.1",
f: SingleIP(),
expectedErr: regexp.MustCompile(regexp.QuoteMeta("expected test_property to contain a valid IP, got:")),
},
})
}

func TestValidationIPRange(t *testing.T) {
runTestCases(t, []testCase{
{
val: "172.10.10.10-172.10.10.12",
f: IPRange(),
},
{
val: "172.10.10.20",
f: IPRange(),
expectedErr: regexp.MustCompile(regexp.QuoteMeta("expected test_property to contain a valid IP range, got:")),
},
{
val: "172.10.10.20-172.10.10.12",
f: IPRange(),
expectedErr: regexp.MustCompile(regexp.QuoteMeta("expected test_property to contain a valid IP range, got:")),
},
})
}

func TestValidateRFC3339TimeString(t *testing.T) {
runTestCases(t, []testCase{
{
Expand Down

0 comments on commit dfa6232

Please sign in to comment.