Skip to content

Commit

Permalink
IP checkers are added. (#41)
Browse files Browse the repository at this point in the history
* IPv4 checker is added. Fixes #23

* IPv6 checker is added. Fixes #24

* IP checker is added. Fixes #22

* IPv4 and IPv6 tests.
  • Loading branch information
cinar authored Jun 17, 2023
1 parent 306675b commit ec32795
Show file tree
Hide file tree
Showing 11 changed files with 389 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ This package currently provides the following checkers:
- [alphanumeric](doc/checkers/alphanumeric.md) checks if the given string consists of only alphanumeric characters.
- [ascii](doc/checkers/ascii.md) checks if the given string consists of only ASCII characters.
- [digits](doc/checkers/digits.md) checks if the given string consists of only digit characters.
- [ip](doc/checkers/ip.md) checks if the given value is an IP address.
- [ipv4](doc/checkers/ipv4.md) checks if the given value is an IPv4 address.
- [ipv6](doc/checkers/ipv6.md) checks if the given value is an IPv6 address.
- [max](doc/checkers/max.md) checks if the given value is less than the given maximum.
- [max-length](doc/checkers/maxlength.md) checks if the length of the given value is less than the given maximum length.
- [min](doc/checkers/min.md) checks if the given value is greather than the given minimum.
Expand Down
3 changes: 3 additions & 0 deletions checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ var makers = map[string]MakeFunc{
CheckerAlphanumeric: makeAlphanumeric,
CheckerAscii: makeAscii,
CheckerDigits: makeDigits,
CheckerIp: makeIp,
CheckerIpV4: makeIpV4,
CheckerIpV6: makeIpV6,
CheckerMax: makeMax,
CheckerMaxLength: makeMaxLength,
CheckerMin: makeMin,
Expand Down
28 changes: 28 additions & 0 deletions doc/checkers/ip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# IP Checker

The ```ip``` checker checks if the value is an IP address. If the value is not an IP address, the checker will return the ```NOT_IP``` result. Here is an example:

```golang
type Request struct {
RemoteIp string `checkers:"ip"`
}

request := &Request{
RemoteIp: "192.168.1.1",
}

_, valid := Check(request)
if !valid {
// Send the mistakes back to the user
}
```

In your custom checkers, you can call the ```ip``` checker function ```IsIp``` to validate the user input. Here is an example:

```golang
result := IsIp("2001:db8::68")

if result != ResultValid {
// Send the mistakes back to the user
}
```
28 changes: 28 additions & 0 deletions doc/checkers/ipv4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# IPv4 Checker

The ```ipv4``` checker checks if the value is an IPv4 address. If the value is not an IPv4 address, the checker will return the ```NOT_IP_V4``` result. Here is an example:

```golang
type Request struct {
RemoteIp string `checkers:"ipv4"`
}

request := &Request{
RemoteIp: "192.168.1.1",
}

_, valid := Check(request)
if !valid {
// Send the mistakes back to the user
}
```

In your custom checkers, you can call the ```ipv4``` checker function ```IsIpV4``` to validate the user input. Here is an example:

```golang
result := IsIpV4("192.168.1.1")

if result != ResultValid {
// Send the mistakes back to the user
}
```
28 changes: 28 additions & 0 deletions doc/checkers/ipv6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# IPv6 Checker

The ```ipv6``` checker checks if the value is an IPv6 address. If the value is not an IPv6 address, the checker will return the ```NOT_IP_V6``` result. Here is an example:

```golang
type Request struct {
RemoteIp string `checkers:"ipv6"`
}

request := &Request{
RemoteIp: "2001:db8::68",
}

_, valid := Check(request)
if !valid {
// Send the mistakes back to the user
}
```

In your custom checkers, you can call the ```ipv6``` checker function ```IsIpV6``` to validate the user input. Here is an example:

```golang
result := IsIpV6("2001:db8::68")

if result != ResultValid {
// Send the mistakes back to the user
}
```
36 changes: 36 additions & 0 deletions ip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package checker

import (
"net"
"reflect"
)

// CheckerIp is the name of the checker.
const CheckerIp = "ip"

// ResultNotIp indicates that the given value is not an IP address.
const ResultNotIp = "NOT_IP"

// IsIp checks if the given value is an IP address.
func IsIp(value string) Result {
ip := net.ParseIP(value)
if ip == nil {
return ResultNotIp
}

return ResultValid
}

// makeIp makes a checker function for the ip checker.
func makeIp(_ string) CheckFunc {
return checkIp
}

// checkIp checks if the given value is an IP address.
func checkIp(value, _ reflect.Value) Result {
if value.Kind() != reflect.String {
panic("string expected")
}

return IsIp(value.String())
}
57 changes: 57 additions & 0 deletions ip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package checker

import "testing"

func TestIsIpInvalid(t *testing.T) {
if IsIp("900.800.200.100") == ResultValid {
t.Fail()
}
}

func TestIsIpValid(t *testing.T) {
if IsIp("2001:db8::68") != ResultValid {
t.Fail()
}
}

func TestCheckIpNonString(t *testing.T) {
defer FailIfNoPanic(t)

type Request struct {
RemoteIp int `checkers:"ip"`
}

request := &Request{}

Check(request)
}

func TestCheckIpInvalid(t *testing.T) {
type Request struct {
RemoteIp string `checkers:"ip"`
}

request := &Request{
RemoteIp: "900.800.200.100",
}

_, valid := Check(request)
if valid {
t.Fail()
}
}

func TestCheckIpValid(t *testing.T) {
type Request struct {
RemoteIp string `checkers:"ip"`
}

request := &Request{
RemoteIp: "192.168.1.1",
}

_, valid := Check(request)
if !valid {
t.Fail()
}
}
40 changes: 40 additions & 0 deletions ipv4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package checker

import (
"net"
"reflect"
)

// CheckerIpV4 is the name of the checker.
const CheckerIpV4 = "ipv4"

// ResultNotIpV4 indicates that the given value is not an IPv4 address.
const ResultNotIpV4 = "NOT_IP_V4"

// IsIpV4 checks if the given value is an IPv4 address.
func IsIpV4(value string) Result {
ip := net.ParseIP(value)
if ip == nil {
return ResultNotIpV4
}

if ip.To4() == nil {
return ResultNotIpV4
}

return ResultValid
}

// makeIpV4 makes a checker function for the ipV4 checker.
func makeIpV4(_ string) CheckFunc {
return checkIpV4
}

// checkIpV4 checks if the given value is an IPv4 address.
func checkIpV4(value, _ reflect.Value) Result {
if value.Kind() != reflect.String {
panic("string expected")
}

return IsIpV4(value.String())
}
63 changes: 63 additions & 0 deletions ipv4_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package checker

import "testing"

func TestIsIpV4Invalid(t *testing.T) {
if IsIpV4("900.800.200.100") == ResultValid {
t.Fail()
}
}

func TestIsIpV4InvalidV6(t *testing.T) {
if IsIpV4("2001:db8::68") == ResultValid {
t.Fail()
}
}

func TestIsIpV4Valid(t *testing.T) {
if IsIpV4("192.168.1.1") != ResultValid {
t.Fail()
}
}

func TestCheckIpV4NonString(t *testing.T) {
defer FailIfNoPanic(t)

type Request struct {
RemoteIp int `checkers:"ipv4"`
}

request := &Request{}

Check(request)
}

func TestCheckIpV4Invalid(t *testing.T) {
type Request struct {
RemoteIp string `checkers:"ipv4"`
}

request := &Request{
RemoteIp: "900.800.200.100",
}

_, valid := Check(request)
if valid {
t.Fail()
}
}

func TestCheckIpV4Valid(t *testing.T) {
type Request struct {
RemoteIp string `checkers:"ipv4"`
}

request := &Request{
RemoteIp: "192.168.1.1",
}

_, valid := Check(request)
if !valid {
t.Fail()
}
}
40 changes: 40 additions & 0 deletions ipv6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package checker

import (
"net"
"reflect"
)

// CheckerIpV6 is the name of the checker.
const CheckerIpV6 = "ipv6"

// ResultNotIpV6 indicates that the given value is not an IPv6 address.
const ResultNotIpV6 = "NOT_IP_V6"

// IsIpV6 checks if the given value is an IPv6 address.
func IsIpV6(value string) Result {
ip := net.ParseIP(value)
if ip == nil {
return ResultNotIpV6
}

if ip.To4() != nil {
return ResultNotIpV6
}

return ResultValid
}

// makeIpV6 makes a checker function for the ipV6 checker.
func makeIpV6(_ string) CheckFunc {
return checkIpV6
}

// checkIpV6 checks if the given value is an IPv6 address.
func checkIpV6(value, _ reflect.Value) Result {
if value.Kind() != reflect.String {
panic("string expected")
}

return IsIpV6(value.String())
}
Loading

0 comments on commit ec32795

Please sign in to comment.