-
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.
Fixes #15
- Loading branch information
Showing
7 changed files
with
138 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
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,32 @@ | ||
# Min Length Checker | ||
|
||
The ```min-length``` checker checks if the length of the given value is greather than the given minimum length. If the length of the value is below the minimum length, the checker will return the ```NOT_MIN_LENGTH``` result. Here is an example: | ||
|
||
```golang | ||
type User struct { | ||
Password string `checkers:"min-length:4"` | ||
} | ||
|
||
user := &User{ | ||
Password: "1234", | ||
} | ||
|
||
mistakes, valid := Check(user) | ||
if !valid { | ||
// Send the mistakes back to the user | ||
} | ||
``` | ||
|
||
The checker can be applied to all types that are supported by the [reflect.Value.Len()](https://pkg.go.dev/reflect#Value.Len) function. | ||
|
||
If you do not want to validate user input stored in a struct, you can individually call the ```min-length``` checker function ```IsMinLength``` to validate the user input. Here is an example: | ||
|
||
```golang | ||
s := "1234" | ||
|
||
result := IsMinLength(s, 4) | ||
|
||
if result != ResultValid { | ||
// Send the mistakes back to the user | ||
} | ||
``` |
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", | ||
} | ||
|
||
_, valid := Check(user) | ||
if !valid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckMinLengthInvalid(t *testing.T) { | ||
type User struct { | ||
Password string `checkers:"min-length:4"` | ||
} | ||
|
||
user := &User{ | ||
Password: "12", | ||
} | ||
|
||
_, 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