diff --git a/README.md b/README.md index cf9433c..bfc1489 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/ascii.go b/ascii.go index f41faa3..d259c68 100644 --- a/ascii.go +++ b/ascii.go @@ -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()) } diff --git a/ascii_test.go b/ascii_test.go index 14afc0f..bc5eb72 100644 --- a/ascii_test.go +++ b/ascii_test.go @@ -2,23 +2,23 @@ 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{} @@ -26,9 +26,9 @@ func TestCheckAsciiNonString(t *testing.T) { 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{ @@ -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{ diff --git a/checker.go b/checker.go index 842deaf..99e009b 100644 --- a/checker.go +++ b/checker.go @@ -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, diff --git a/digits_test.go b/digits_test.go index 9928989..af57cac 100644 --- a/digits_test.go +++ b/digits_test.go @@ -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{} @@ -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) @@ -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) diff --git a/doc/checkers/ascii.md b/doc/checkers/ascii.md index a28f6a2..8ad0cec 100644 --- a/doc/checkers/ascii.md +++ b/doc/checkers/ascii.md @@ -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{ @@ -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 diff --git a/doc/checkers/email.md b/doc/checkers/email.md index e27f7cd..d226b58 100644 --- a/doc/checkers/email.md +++ b/doc/checkers/email.md @@ -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("user@zdo.com") diff --git a/doc/checkers/ip.md b/doc/checkers/ip.md index 58d4af2..970b03a 100644 --- a/doc/checkers/ip.md +++ b/doc/checkers/ip.md @@ -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) @@ -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 diff --git a/doc/checkers/ipv4.md b/doc/checkers/ipv4.md index ee7ac0e..c6d8b99 100644 --- a/doc/checkers/ipv4.md +++ b/doc/checkers/ipv4.md @@ -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) @@ -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 diff --git a/doc/checkers/ipv6.md b/doc/checkers/ipv6.md index e2801ae..050f442 100644 --- a/doc/checkers/ipv6.md +++ b/doc/checkers/ipv6.md @@ -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) @@ -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 diff --git a/email.go b/email.go index 021e0e7..4459824 100644 --- a/email.go +++ b/email.go @@ -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) diff --git a/ip.go b/ip.go index c9bd10e..aacef03 100644 --- a/ip.go +++ b/ip.go @@ -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()) } diff --git a/ip_test.go b/ip_test.go index 1fd607b..add19eb 100644 --- a/ip_test.go +++ b/ip_test.go @@ -2,14 +2,14 @@ package checker import "testing" -func TestIsIpInvalid(t *testing.T) { - if IsIp("900.800.200.100") == ResultValid { +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 { +func TestIsIPValid(t *testing.T) { + if IsIP("2001:db8::68") != ResultValid { t.Fail() } } @@ -18,7 +18,7 @@ func TestCheckIpNonString(t *testing.T) { defer FailIfNoPanic(t) type Request struct { - RemoteIp int `checkers:"ip"` + RemoteIP int `checkers:"ip"` } request := &Request{} @@ -28,11 +28,11 @@ func TestCheckIpNonString(t *testing.T) { func TestCheckIpInvalid(t *testing.T) { type Request struct { - RemoteIp string `checkers:"ip"` + RemoteIP string `checkers:"ip"` } request := &Request{ - RemoteIp: "900.800.200.100", + RemoteIP: "900.800.200.100", } _, valid := Check(request) @@ -41,13 +41,13 @@ func TestCheckIpInvalid(t *testing.T) { } } -func TestCheckIpValid(t *testing.T) { +func TestCheckIPValid(t *testing.T) { 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) diff --git a/ipv4.go b/ipv4.go index 782c5c7..54ca08c 100644 --- a/ipv4.go +++ b/ipv4.go @@ -5,36 +5,36 @@ import ( "reflect" ) -// CheckerIpV4 is the name of the checker. -const CheckerIpV4 = "ipv4" +// 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" +// 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 { +// IsIPV4 checks if the given value is an IPv4 address. +func IsIPV4(value string) Result { ip := net.ParseIP(value) if ip == nil { - return ResultNotIpV4 + return ResultNotIPV4 } if ip.To4() == nil { - return ResultNotIpV4 + return ResultNotIPV4 } return ResultValid } -// makeIpV4 makes a checker function for the ipV4 checker. -func makeIpV4(_ string) CheckFunc { - return checkIpV4 +// 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 { +// 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()) + return IsIPV4(value.String()) } diff --git a/ipv4_test.go b/ipv4_test.go index 5e853a7..2548ee4 100644 --- a/ipv4_test.go +++ b/ipv4_test.go @@ -2,29 +2,29 @@ package checker import "testing" -func TestIsIpV4Invalid(t *testing.T) { - if IsIpV4("900.800.200.100") == ResultValid { +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 { +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 { +func TestIsIPV4Valid(t *testing.T) { + if IsIPV4("192.168.1.1") != ResultValid { t.Fail() } } -func TestCheckIpV4NonString(t *testing.T) { +func TestCheckIPV4NonString(t *testing.T) { defer FailIfNoPanic(t) type Request struct { - RemoteIp int `checkers:"ipv4"` + RemoteIP int `checkers:"ipv4"` } request := &Request{} @@ -32,13 +32,13 @@ func TestCheckIpV4NonString(t *testing.T) { Check(request) } -func TestCheckIpV4Invalid(t *testing.T) { +func TestCheckIPV4Invalid(t *testing.T) { type Request struct { - RemoteIp string `checkers:"ipv4"` + RemoteIP string `checkers:"ipv4"` } request := &Request{ - RemoteIp: "900.800.200.100", + RemoteIP: "900.800.200.100", } _, valid := Check(request) @@ -47,13 +47,13 @@ func TestCheckIpV4Invalid(t *testing.T) { } } -func TestCheckIpV4Valid(t *testing.T) { +func TestCheckIPV4Valid(t *testing.T) { 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) diff --git a/ipv6.go b/ipv6.go index eb56fe9..10edf45 100644 --- a/ipv6.go +++ b/ipv6.go @@ -5,36 +5,36 @@ import ( "reflect" ) -// CheckerIpV6 is the name of the checker. -const CheckerIpV6 = "ipv6" +// 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" +// 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 { +// IsIPV6 checks if the given value is an IPv6 address. +func IsIPV6(value string) Result { ip := net.ParseIP(value) if ip == nil { - return ResultNotIpV6 + return ResultNotIPV6 } if ip.To4() != nil { - return ResultNotIpV6 + return ResultNotIPV6 } return ResultValid } -// makeIpV6 makes a checker function for the ipV6 checker. -func makeIpV6(_ string) CheckFunc { - return checkIpV6 +// 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 { +// 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()) + return IsIPV6(value.String()) } diff --git a/ipv6_test.go b/ipv6_test.go index bc8489d..55e72c3 100644 --- a/ipv6_test.go +++ b/ipv6_test.go @@ -2,29 +2,29 @@ package checker import "testing" -func TestIsIpV6Invalid(t *testing.T) { - if IsIpV6("900.800.200.100") == ResultValid { +func TestIsIPV6Invalid(t *testing.T) { + if IsIPV6("900.800.200.100") == ResultValid { t.Fail() } } -func TestIsIpV6InvalidV4(t *testing.T) { - if IsIpV6("192.168.1.1") == ResultValid { +func TestIsIPV6InvalidV4(t *testing.T) { + if IsIPV6("192.168.1.1") == ResultValid { t.Fail() } } -func TestIsIpV6Valid(t *testing.T) { - if IsIpV6("2001:db8::68") != ResultValid { +func TestIsIPV6Valid(t *testing.T) { + if IsIPV6("2001:db8::68") != ResultValid { t.Fail() } } -func TestCheckIpV6NonString(t *testing.T) { +func TestCheckIPV6NonString(t *testing.T) { defer FailIfNoPanic(t) type Request struct { - RemoteIp int `checkers:"ipv6"` + RemoteIP int `checkers:"ipv6"` } request := &Request{} @@ -32,13 +32,13 @@ func TestCheckIpV6NonString(t *testing.T) { Check(request) } -func TestCheckIpV6Invalid(t *testing.T) { +func TestCheckIPV6Invalid(t *testing.T) { type Request struct { - RemoteIp string `checkers:"ipv6"` + RemoteIP string `checkers:"ipv6"` } request := &Request{ - RemoteIp: "900.800.200.100", + RemoteIP: "900.800.200.100", } _, valid := Check(request) @@ -47,13 +47,13 @@ func TestCheckIpV6Invalid(t *testing.T) { } } -func TestCheckIpV6Valid(t *testing.T) { +func TestCheckIPV6Valid(t *testing.T) { type Request struct { - RemoteIp string `checkers:"ipv6"` + RemoteIP string `checkers:"ipv6"` } request := &Request{ - RemoteIp: "2001:db8::68", + RemoteIP: "2001:db8::68", } _, valid := Check(request) diff --git a/luhn.go b/luhn.go index b366580..ef12591 100644 --- a/luhn.go +++ b/luhn.go @@ -26,7 +26,7 @@ func IsLuhn(number string) Result { } // makeLuhn makes a checker function for the Luhn algorithm. -func makeLuhn(config string) CheckFunc { +func makeLuhn(_ string) CheckFunc { return checkLuhn } diff --git a/revive.toml b/revive.toml new file mode 100644 index 0000000..f9e2405 --- /dev/null +++ b/revive.toml @@ -0,0 +1,30 @@ +ignoreGeneratedHeader = false +severity = "warning" +confidence = 0.8 +errorCode = 0 +warningCode = 0 + +[rule.blank-imports] +[rule.context-as-argument] +[rule.context-keys-type] +[rule.dot-imports] +[rule.error-return] +[rule.error-strings] +[rule.error-naming] +[rule.exported] +[rule.if-return] +[rule.increment-decrement] +[rule.var-naming] +[rule.var-declaration] +[rule.package-comments] +[rule.range] +[rule.receiver-naming] +[rule.time-naming] +[rule.unexported-return] +[rule.indent-error-flow] +[rule.errorf] +[rule.empty-block] +[rule.superfluous-else] +[rule.unused-parameter] +[rule.unreachable-code] +[rule.redefines-builtin-id]