-
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
137 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package checker | ||
|
||
import ( | ||
"reflect" | ||
"unicode" | ||
) | ||
|
||
// CheckerAlphanumeric is the name of the checker. | ||
const CheckerAlphanumeric = "alphanumeric" | ||
|
||
// ResultNotAlphanumeric indicates that the given string contains non-alphanumeric characters. | ||
const ResultNotAlphanumeric = "NOT_ALPHANUMERIC" | ||
|
||
// IsAlphanumeric checks if the given string consists of only alphanumeric characters. | ||
func IsAlphanumeric(value string) Result { | ||
for _, c := range value { | ||
if !unicode.IsDigit(c) && !unicode.IsLetter(c) { | ||
return ResultNotAlphanumeric | ||
} | ||
} | ||
|
||
return ResultValid | ||
} | ||
|
||
// makeAlphanumeric makes a checker function for the alphanumeric checker. | ||
func makeAlphanumeric(_ string) CheckFunc { | ||
return checkAlphanumeric | ||
} | ||
|
||
// checkAlphanumeric checks if the given string consists of only alphanumeric characters. | ||
func checkAlphanumeric(value, _ reflect.Value) Result { | ||
if value.Kind() != reflect.String { | ||
panic("string expected") | ||
} | ||
|
||
return IsAlphanumeric(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 TestIsAlphanumericInvalid(t *testing.T) { | ||
if IsAlphanumeric("-/") == ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestIsAlphanumericValid(t *testing.T) { | ||
if IsAlphanumeric("ABcd1234") != ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckAlphanumericNonString(t *testing.T) { | ||
defer FailIfNoPanic(t) | ||
|
||
type User struct { | ||
Username int `checkers:"alphanumeric"` | ||
} | ||
|
||
user := &User{} | ||
|
||
Check(user) | ||
} | ||
|
||
func TestCheckAlphanumericInvalid(t *testing.T) { | ||
type User struct { | ||
Username string `checkers:"alphanumeric"` | ||
} | ||
|
||
user := &User{ | ||
Username: "user-/", | ||
} | ||
|
||
_, valid := Check(user) | ||
if valid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckAlphanumericValid(t *testing.T) { | ||
type User struct { | ||
Username string `checkers:"alphanumeric"` | ||
} | ||
|
||
user := &User{ | ||
Username: "ABcd1234", | ||
} | ||
|
||
_, valid := Check(user) | ||
if !valid { | ||
t.Fail() | ||
} | ||
} |
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
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 @@ | ||
# Alphanumeric Checker | ||
|
||
The ```alphanumeric``` checker checks if the given string consists of only alphanumeric characters. If the string contains non-alphanumeric characters, the checker will return the ```NOT_ALPHANUMERIC``` result. Here is an example: | ||
|
||
```golang | ||
type User struct { | ||
Username string `checkers:"alphanumeric"` | ||
} | ||
|
||
user := &User{ | ||
Username: "ABcd1234", | ||
} | ||
|
||
_, valid := Check(user) | ||
if !valid { | ||
// Send the mistakes back to the user | ||
} | ||
``` | ||
|
||
In your custom checkers, you can call the ```alphanumeric``` checker function ```IsAlphanumeric``` to validate the user input. Here is an example: | ||
|
||
```golang | ||
result := IsAlphanumeric("ABcd1234") | ||
|
||
if result != ResultValid { | ||
// Send the mistakes back to the user | ||
} | ||
``` |