This repository has been archived by the owner on Feb 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from choria-io/3
(#3) add ipv4, ipv6 and ipaddress validators
- Loading branch information
Showing
11 changed files
with
323 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
vendor | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ipaddress | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"reflect" | ||
) | ||
|
||
// ValidateString validates that the given string is either an IPv6 or an IPv4 address | ||
func ValidateString(input string) (bool, error) { | ||
ip := net.ParseIP(input) | ||
|
||
if ip == nil { | ||
return false, fmt.Errorf("%s is not an IP address", input) | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
// ValidateStructField validates a struct field holds either an IPv6 or an IPv4 address | ||
func ValidateStructField(value reflect.Value, tag string) (bool, error) { | ||
if value.Kind() != reflect.String { | ||
return false, fmt.Errorf("only strings can be IPv6 validated") | ||
} | ||
|
||
return ValidateString(value.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package ipaddress | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestFileContent(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Validator/IPv4") | ||
} | ||
|
||
var _ = Describe("ValidateString", func() { | ||
It("Should match ipv4 addresses correctly", func() { | ||
ok, err := ValidateString("1.2.3.4") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(ok).To(BeTrue()) | ||
|
||
ok, err = ValidateString("2a00:1450:4003:807::200e") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(ok).To(BeTrue()) | ||
|
||
ok, err = ValidateString("foo") | ||
Expect(err).To(MatchError("foo is not an IP address")) | ||
Expect(ok).To(BeFalse()) | ||
}) | ||
}) | ||
|
||
var _ = Describe("ValidateStructField", func() { | ||
type t struct { | ||
IP string `validate:"ipaddress"` | ||
} | ||
|
||
It("Should validate the struct correctly", func() { | ||
st := t{"1.2.3.4"} | ||
|
||
val := reflect.ValueOf(st) | ||
valueField := val.FieldByName("IP") | ||
typeField, _ := val.Type().FieldByName("IP") | ||
|
||
ok, err := ValidateStructField(valueField, typeField.Tag.Get("validate")) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(ok).To(BeTrue()) | ||
|
||
st.IP = "foo" | ||
valueField = reflect.ValueOf(st).FieldByName("IP") | ||
ok, err = ValidateStructField(valueField, typeField.Tag.Get("validate")) | ||
Expect(err).To(MatchError("foo is not an IP address")) | ||
Expect(ok).To(BeFalse()) | ||
|
||
st.IP = "2a00:1450:4003:807::200e" | ||
valueField = reflect.ValueOf(st).FieldByName("IP") | ||
ok, err = ValidateStructField(valueField, typeField.Tag.Get("validate")) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(ok).To(BeTrue()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ipv4 | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"reflect" | ||
) | ||
|
||
// ValidateString validates that the given string is an IPv4 address | ||
func ValidateString(input string) (bool, error) { | ||
ip := net.ParseIP(input).To4() | ||
|
||
if ip == nil { | ||
return false, fmt.Errorf("%s is not an IPv4 address", input) | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
// ValidateStructField validates a struct field holds an IPv4 address | ||
func ValidateStructField(value reflect.Value, tag string) (bool, error) { | ||
if value.Kind() != reflect.String { | ||
return false, fmt.Errorf("only strings can be IPv4 validated") | ||
} | ||
|
||
return ValidateString(value.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package ipv4 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestFileContent(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Validator/IPv4") | ||
} | ||
|
||
var _ = Describe("ValidateString", func() { | ||
It("Should match ipv4 addresses correctly", func() { | ||
ok, err := ValidateString("1.2.3.4") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(ok).To(BeTrue()) | ||
|
||
ok, err = ValidateString("foo") | ||
Expect(err).To(MatchError("foo is not an IPv4 address")) | ||
Expect(ok).To(BeFalse()) | ||
|
||
ok, err = ValidateString("2a00:1450:4003:807::200e") | ||
Expect(err).To(MatchError("2a00:1450:4003:807::200e is not an IPv4 address")) | ||
Expect(ok).To(BeFalse()) | ||
}) | ||
}) | ||
|
||
var _ = Describe("ValidateStructField", func() { | ||
type t struct { | ||
IP string `validate:"ipv4"` | ||
} | ||
|
||
It("Should validate the struct correctly", func() { | ||
st := t{"1.2.3.4"} | ||
|
||
val := reflect.ValueOf(st) | ||
valueField := val.FieldByName("IP") | ||
typeField, _ := val.Type().FieldByName("IP") | ||
|
||
ok, err := ValidateStructField(valueField, typeField.Tag.Get("validate")) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(ok).To(BeTrue()) | ||
|
||
st.IP = "foo" | ||
valueField = reflect.ValueOf(st).FieldByName("IP") | ||
ok, err = ValidateStructField(valueField, typeField.Tag.Get("validate")) | ||
Expect(err).To(MatchError("foo is not an IPv4 address")) | ||
Expect(ok).To(BeFalse()) | ||
|
||
st.IP = "2a00:1450:4003:807::200e" | ||
valueField = reflect.ValueOf(st).FieldByName("IP") | ||
ok, err = ValidateStructField(valueField, typeField.Tag.Get("validate")) | ||
Expect(err).To(MatchError("2a00:1450:4003:807::200e is not an IPv4 address")) | ||
Expect(ok).To(BeFalse()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package ipv6 | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"reflect" | ||
) | ||
|
||
// ValidateString validates that the given string is an IPv4 address | ||
func ValidateString(input string) (bool, error) { | ||
ip := net.ParseIP(input) | ||
|
||
if ip == nil { | ||
return false, fmt.Errorf("%s is not an IPv6 address", input) | ||
} | ||
|
||
if ip.To4() != nil { | ||
return false, fmt.Errorf("%s is not an IPv6 address", input) | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
// ValidateStructField validates a struct field holds an IPv4 address | ||
func ValidateStructField(value reflect.Value, tag string) (bool, error) { | ||
if value.Kind() != reflect.String { | ||
return false, fmt.Errorf("only strings can be IPv6 validated") | ||
} | ||
|
||
return ValidateString(value.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package ipv6 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestFileContent(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Validator/IPv4") | ||
} | ||
|
||
var _ = Describe("ValidateString", func() { | ||
It("Should match ipv4 addresses correctly", func() { | ||
ok, err := ValidateString("1.2.3.4") | ||
Expect(err).To(MatchError("1.2.3.4 is not an IPv6 address")) | ||
Expect(ok).To(BeFalse()) | ||
|
||
ok, err = ValidateString("foo") | ||
Expect(err).To(MatchError("foo is not an IPv6 address")) | ||
Expect(ok).To(BeFalse()) | ||
|
||
ok, err = ValidateString("2a00:1450:4003:807::200e") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(ok).To(BeTrue()) | ||
}) | ||
}) | ||
|
||
var _ = Describe("ValidateStructField", func() { | ||
type t struct { | ||
IP string `validate:"ipv6"` | ||
} | ||
|
||
It("Should validate the struct correctly", func() { | ||
st := t{"1.2.3.4"} | ||
|
||
val := reflect.ValueOf(st) | ||
valueField := val.FieldByName("IP") | ||
typeField, _ := val.Type().FieldByName("IP") | ||
|
||
ok, err := ValidateStructField(valueField, typeField.Tag.Get("validate")) | ||
Expect(err).To(MatchError("1.2.3.4 is not an IPv6 address")) | ||
Expect(ok).To(BeFalse()) | ||
|
||
st.IP = "foo" | ||
valueField = reflect.ValueOf(st).FieldByName("IP") | ||
ok, err = ValidateStructField(valueField, typeField.Tag.Get("validate")) | ||
Expect(err).To(MatchError("foo is not an IPv6 address")) | ||
Expect(ok).To(BeFalse()) | ||
|
||
st.IP = "2a00:1450:4003:807::200e" | ||
valueField = reflect.ValueOf(st).FieldByName("IP") | ||
ok, err = ValidateStructField(valueField, typeField.Tag.Get("validate")) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(ok).To(BeTrue()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.