-
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
6 changed files
with
124 additions
and
1 deletion.
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,36 @@ | ||
package checker | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
// CheckerDigits is the name of the checker. | ||
const CheckerDigits = "digits" | ||
|
||
// ResultNotDigits indicates that the given string contains non-digit characters. | ||
const ResultNotDigits = "NOT_DIGITS" | ||
|
||
// IsDigits checks if the given string consists of only digit characters. | ||
func IsDigits(value string) Result { | ||
for i := 0; i < len(value); i++ { | ||
if value[i] < '0' || value[i] > '9' { | ||
return ResultNotDigits | ||
} | ||
} | ||
|
||
return ResultValid | ||
} | ||
|
||
// makeDigits makes a checker function for the digits checker. | ||
func makeDigits(_ string) CheckFunc { | ||
return checkDigits | ||
} | ||
|
||
// checkDigits checks if the given string consists of only digit characters. | ||
func checkDigits(value, _ reflect.Value) Result { | ||
if value.Kind() != reflect.String { | ||
panic("string expected") | ||
} | ||
|
||
return IsDigits(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 TestIsDigitsInvalid(t *testing.T) { | ||
if IsDigits("checker") == ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestIsDigitsValid(t *testing.T) { | ||
if IsDigits("1234") != ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckDigitsNonString(t *testing.T) { | ||
defer FailIfNoPanic(t) | ||
|
||
type User struct { | ||
Id int `checkers:"digits"` | ||
} | ||
|
||
user := &User{} | ||
|
||
Check(user) | ||
} | ||
|
||
func TestCheckDigitsInvalid(t *testing.T) { | ||
type User struct { | ||
Id string `checkers:"digits"` | ||
} | ||
|
||
user := &User{ | ||
Id: "checker", | ||
} | ||
|
||
_, valid := Check(user) | ||
if valid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckDigitsValid(t *testing.T) { | ||
type User struct { | ||
Id string `checkers:"digits"` | ||
} | ||
|
||
user := &User{ | ||
Id: "1234", | ||
} | ||
|
||
_, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Digits Checker | ||
|
||
The ```digits``` checker checks if the given string consists of only digit characters. If the string contains non-digit characters, the checker will return the ```NOT_DIGITS``` result. Here is an example: | ||
|
||
```golang | ||
type User struct { | ||
Id string `checkers:"digits"` | ||
} | ||
|
||
user := &User{ | ||
Id: "1234", | ||
} | ||
|
||
_, valid := Check(user) | ||
if !valid { | ||
// Send the mistakes back to the user | ||
} | ||
``` | ||
|
||
In your custom checkers, you can call the ```digits``` checker function ```IsDigits``` to validate the user input. Here is an example: | ||
|
||
```golang | ||
result := IsDigits("1234") | ||
|
||
if result != ResultValid { | ||
// Send the mistakes back to the user | ||
} | ||
``` |