Skip to content

Commit

Permalink
Revive added. (#73)
Browse files Browse the repository at this point in the history
# Describe Request

Revice integrated.

# Change Type

Maintenance change.
  • Loading branch information
cinar committed Jun 20, 2023
1 parent 34cfc6f commit 0cb565f
Show file tree
Hide file tree
Showing 19 changed files with 162 additions and 132 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type Person struct {
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.
- [ASCII](doc/checkers/ASCII.md) checks if the given string consists of only ASCII characters.
- [cidr](doc/checkers/cidr.md) checker checks if the value is a valid CIDR notation IP address and prefix length.
- [digits](doc/checkers/digits.md) checks if the given string consists of only digit characters.
- [email](doc/checkers/email.md) checks if the given string is an email address.
Expand Down
26 changes: 13 additions & 13 deletions ascii.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@ import (
"unicode"
)

// CheckerAscii is the name of the checker.
const CheckerAscii = "ascii"
// CheckerASCII is the name of the checker.
const CheckerASCII = "ASCII"

// ResultNotAscii indicates that the given string contains non-ASCII characters.
const ResultNotAscii = "NOT_ASCII"
// ResultNotASCII indicates that the given string contains non-ASCII characters.
const ResultNotASCII = "NOT_ASCII"

// IsAscii checks if the given string consists of only ASCII characters.
func IsAscii(value string) Result {
// IsASCII checks if the given string consists of only ASCII characters.
func IsASCII(value string) Result {
for _, c := range value {
if c > unicode.MaxASCII {
return ResultNotAscii
return ResultNotASCII
}
}

return ResultValid
}

// makeAscii makes a checker function for the ascii checker.
func makeAscii(_ string) CheckFunc {
return checkAscii
// makeASCII makes a checker function for the ASCII checker.
func makeASCII(_ string) CheckFunc {
return checkASCII
}

// checkAscii checks if the given string consists of only ASCII characters.
func checkAscii(value, _ reflect.Value) Result {
// checkASCII checks if the given string consists of only ASCII characters.
func checkASCII(value, _ reflect.Value) Result {
if value.Kind() != reflect.String {
panic("string expected")
}

return IsAscii(value.String())
return IsASCII(value.String())
}
20 changes: 10 additions & 10 deletions ascii_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@ package checker

import "testing"

func TestIsAsciiInvalid(t *testing.T) {
if IsAscii("𝄞 Music!") == ResultValid {
func TestIsASCIIInvalid(t *testing.T) {
if IsASCII("𝄞 Music!") == ResultValid {
t.Fail()
}
}

func TestIsAsciiValid(t *testing.T) {
if IsAscii("Checker") != ResultValid {
func TestIsASCIIValid(t *testing.T) {
if IsASCII("Checker") != ResultValid {
t.Fail()
}
}

func TestCheckAsciiNonString(t *testing.T) {
func TestCheckASCIINonString(t *testing.T) {
defer FailIfNoPanic(t)

type User struct {
Age int `checkers:"ascii"`
Age int `checkers:"ASCII"`
}

user := &User{}

Check(user)
}

func TestCheckAsciiInvalid(t *testing.T) {
func TestCheckASCIIInvalid(t *testing.T) {
type User struct {
Username string `checkers:"ascii"`
Username string `checkers:"ASCII"`
}

user := &User{
Expand All @@ -41,9 +41,9 @@ func TestCheckAsciiInvalid(t *testing.T) {
}
}

func TestCheckAsciiValid(t *testing.T) {
func TestCheckASCIIValid(t *testing.T) {
type User struct {
Username string `checkers:"ascii"`
Username string `checkers:"ASCII"`
}

user := &User{
Expand Down
8 changes: 4 additions & 4 deletions checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ const ResultValid Result = "VALID"
// makers provides mapping to maker function for the checkers.
var makers = map[string]MakeFunc{
CheckerAlphanumeric: makeAlphanumeric,
CheckerAscii: makeAscii,
CheckerASCII: makeASCII,
CheckerCidr: makeCidr,
CheckerDigits: makeDigits,
CheckerEmail: makeEmail,
CheckerFqdn: makeFqdn,
CheckerIp: makeIp,
CheckerIpV4: makeIpV4,
CheckerIpV6: makeIpV6,
CheckerIP: makeIP,
CheckerIPV4: makeIPV4,
CheckerIPV6: makeIPV6,
CheckerLuhn: makeLuhn,
CheckerMac: makeMac,
CheckerMax: makeMax,
Expand Down
10 changes: 5 additions & 5 deletions digits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestCheckDigitsNonString(t *testing.T) {
defer FailIfNoPanic(t)

type User struct {
Id int `checkers:"digits"`
ID int `checkers:"digits"`
}

user := &User{}
Expand All @@ -28,11 +28,11 @@ func TestCheckDigitsNonString(t *testing.T) {

func TestCheckDigitsInvalid(t *testing.T) {
type User struct {
Id string `checkers:"digits"`
ID string `checkers:"digits"`
}

user := &User{
Id: "checker",
ID: "checker",
}

_, valid := Check(user)
Expand All @@ -43,11 +43,11 @@ func TestCheckDigitsInvalid(t *testing.T) {

func TestCheckDigitsValid(t *testing.T) {
type User struct {
Id string `checkers:"digits"`
ID string `checkers:"digits"`
}

user := &User{
Id: "1234",
ID: "1234",
}

_, valid := Check(user)
Expand Down
8 changes: 4 additions & 4 deletions doc/checkers/ascii.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# ASCII Checker

The ```ascii``` checker checks if the given string consists of only ASCII characters. If the string contains non-ASCII characters, the checker will return the ```NOT_ASCII``` result. Here is an example:
The ```ASCII``` checker checks if the given string consists of only ASCII characters. If the string contains non-ASCII characters, the checker will return the ```NOT_ASCII``` result. Here is an example:

```golang
type User struct {
Username string `checkers:"ascii"`
Username string `checkers:"ASCII"`
}

user := &User{
Expand All @@ -17,10 +17,10 @@ if !valid {
}
```

In your custom checkers, you can call the ```ascii``` checker function ```IsAscii``` to validate the user input. Here is an example:
In your custom checkers, you can call the ```ASCII``` checker function ```IsASCII``` to validate the user input. Here is an example:

```golang
result := IsAscii("Checker")
result := IsASCII("Checker")

if result != ResultValid {
// Send the mistakes back to the user
Expand Down
2 changes: 1 addition & 1 deletion doc/checkers/email.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if !valid {
}
```

In your custom checkers, you can call the ```ascii``` checker function ```IsAscii``` to validate the user input. Here is an example:
In your custom checkers, you can call the ```email``` checker function ```IsEmail``` to validate the user input. Here is an example:

```golang
result := IsEmail("[email protected]")
Expand Down
8 changes: 4 additions & 4 deletions doc/checkers/ip.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ The ```ip``` checker checks if the value is an IP address. If the value is not a

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

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

_, valid := Check(request)
Expand All @@ -17,10 +17,10 @@ if !valid {
}
```

In your custom checkers, you can call the ```ip``` checker function ```IsIp``` to validate the user input. Here is an example:
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")
result := IsIP("2001:db8::68")

if result != ResultValid {
// Send the mistakes back to the user
Expand Down
8 changes: 4 additions & 4 deletions doc/checkers/ipv4.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ The ```ipv4``` checker checks if the value is an IPv4 address. If the value is n

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

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

_, valid := Check(request)
Expand All @@ -17,10 +17,10 @@ if !valid {
}
```

In your custom checkers, you can call the ```ipv4``` checker function ```IsIpV4``` to validate the user input. Here is an example:
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")
result := IsIPV4("192.168.1.1")

if result != ResultValid {
// Send the mistakes back to the user
Expand Down
8 changes: 4 additions & 4 deletions doc/checkers/ipv6.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ The ```ipv6``` checker checks if the value is an IPv6 address. If the value is n

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

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

_, valid := Check(request)
Expand All @@ -17,10 +17,10 @@ if !valid {
}
```

In your custom checkers, you can call the ```ipv6``` checker function ```IsIpV6``` to validate the user input. Here is an example:
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")
result := IsIPV6("2001:db8::68")

if result != ResultValid {
// Send the mistakes back to the user
Expand Down
4 changes: 2 additions & 2 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ func isValidEmailDomain(domain string) Result {
if domain[0] == '[' {
if strings.HasPrefix(domain, ipV6Prefix) {
// postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]
return IsIpV6(domain[len(ipV6Prefix) : len(domain)-1])
return IsIPV6(domain[len(ipV6Prefix) : len(domain)-1])
}

// postmaster@[123.123.123.123]
return IsIpV4(domain[1 : len(domain)-1])
return IsIPV4(domain[1 : len(domain)-1])
}

return IsFqdn(domain)
Expand Down
26 changes: 13 additions & 13 deletions ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@ import (
"reflect"
)

// CheckerIp is the name of the checker.
const CheckerIp = "ip"
// 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"
// 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 {
// 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 ResultNotIP
}

return ResultValid
}

// makeIp makes a checker function for the ip checker.
func makeIp(_ string) CheckFunc {
return checkIp
// 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 {
// 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())
return IsIP(value.String())
}
Loading

0 comments on commit 0cb565f

Please sign in to comment.