Skip to content

Commit

Permalink
Digits checker is added. (#37)
Browse files Browse the repository at this point in the history
Fixes #20
  • Loading branch information
cinar committed Jun 17, 2023
1 parent 3687719 commit 852b320
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Person struct {
This package currently provides the following checkers:

- [ascii](doc/checkers/ascii.md) checks if the given string consists of only ASCII characters.
- [digits](doc/checkers/digits.md) checks if the given string consists of only digit characters.
- [max](doc/checkers/max.md) checks if the given value is less than the given maximum.
- [max-length](doc/checkers/maxlength.md) checks if the length of the given value is less than the given maximum length.
- [min](doc/checkers/min.md) checks if the given value is greather than the given minimum.
Expand Down
1 change: 1 addition & 0 deletions checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const ResultValid Result = "VALID"
// makers provides mapping to maker function for the checkers.
var makers = map[string]MakeFunc{
CheckerAscii: makeAscii,
CheckerDigits: makeDigits,
CheckerMax: makeMax,
CheckerMaxLength: makeMaxLength,
CheckerMin: makeMin,
Expand Down
36 changes: 36 additions & 0 deletions digits.go
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())
}
57 changes: 57 additions & 0 deletions digits_test.go
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()
}
}
2 changes: 1 addition & 1 deletion doc/checkers/ascii.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ASCII Checker

The ```ascii``` checkr 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 {
Expand Down
28 changes: 28 additions & 0 deletions doc/checkers/digits.md
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
}
```

0 comments on commit 852b320

Please sign in to comment.