-
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
5 changed files
with
105 additions
and
2 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,39 @@ | ||
package checker | ||
|
||
import ( | ||
"reflect" | ||
"strconv" | ||
) | ||
|
||
// CheckerMinLength is the name of the checker. | ||
const CheckerMinLength = "min-length" | ||
|
||
// ResultNotMinLength indicates that the length of the given value is below the defined number. | ||
const ResultNotMinLength = "NOT_MIN_LENGTH" | ||
|
||
// IsMinLength checks if the length of the given value is greather than the given minimum length. | ||
func IsMinLength(value interface{}, minLength int) Result { | ||
return checkMinLength(reflect.Indirect(reflect.ValueOf(value)), reflect.ValueOf(nil), minLength) | ||
} | ||
|
||
// makeMinLength makes a checker function for the min length checker. | ||
func makeMinLength(config string) CheckFunc { | ||
minLength, err := strconv.Atoi(config) | ||
if err != nil { | ||
panic("unable to parse min length value") | ||
} | ||
|
||
return func(value, parent reflect.Value) Result { | ||
return checkMinLength(value, parent, minLength) | ||
} | ||
} | ||
|
||
// checkMinLength checks if the length of the given value is greather than the given minimum length. | ||
// The function uses the reflect.Value.Len() function to determine the length of the value. | ||
func checkMinLength(value, _ reflect.Value, minLength int) Result { | ||
if value.Len() < minLength { | ||
return ResultNotMinLength | ||
} | ||
|
||
return ResultValid | ||
} |
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 TestIsMinLengthValid(t *testing.T) { | ||
s := "1234" | ||
|
||
if IsMinLength(s, 4) != ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckMinLengthInvalidConfig(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Fail() | ||
} | ||
}() | ||
|
||
type User struct { | ||
Password string `checkers:"min-length:AB"` | ||
} | ||
|
||
user := &User{} | ||
|
||
Check(user) | ||
} | ||
|
||
func TestCheckMinLengthValid(t *testing.T) { | ||
type User struct { | ||
Password string `checkers:"min-length:4"` | ||
} | ||
|
||
user := &User{ | ||
Password: "1234", | ||
} | ||
|
||
_, ok := Check(user) | ||
if !ok { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckMinLengthInvalid(t *testing.T) { | ||
type User struct { | ||
Password string `checkers:"min-length:4"` | ||
} | ||
|
||
user := &User{ | ||
Password: "12", | ||
} | ||
|
||
_, ok := Check(user) | ||
if ok { | ||
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