-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
125 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
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,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 | ||
} | ||
``` |
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
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,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()) | ||
} |
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,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() | ||
} | ||
} |