|
|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
package validation
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"reflect"
|
|
|
|
|
@ -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) {
|
|
|
|
|
|