Revive added. (#73)

# Describe Request

Revice integrated.

# Change Type

Maintenance change.
This commit is contained in:
Onur Cinar 2023-06-19 20:01:57 -07:00 committed by GitHub
commit 0cb565f2d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 162 additions and 132 deletions

View file

@ -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.

View file

@ -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())
}

View file

@ -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{

View file

@ -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,

View file

@ -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)

View file

@ -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

View file

@ -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")

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

26
ip.go
View file

@ -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())
}

View file

@ -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)

28
ipv4.go
View file

@ -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())
}

View file

@ -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)

28
ipv6.go
View file

@ -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())
}

View file

@ -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)

View file

@ -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
}

30
revive.toml Normal file
View file

@ -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]