-
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
130 additions
and
0 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 ```max-length``` checker checks if the length of the given value is less than the given maximum length. If the length of the value is above the minimum length, the checker will return the ```NOT_MAX_LENGTH``` result. Here is an example: | ||
|
||
```golang | ||
type User struct { | ||
Password string `checkers:"max-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 ```max-length``` checker function ```IsMaxLength``` to validate the user input. Here is an example: | ||
|
||
```golang | ||
s := "1234" | ||
|
||
result := IsMaxLength(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" | ||
) | ||
|
||
// CheckerMaxLength is the name of the checker. | ||
const CheckerMaxLength = "max-length" | ||
|
||
// ResultNotMaxLength indicates that the length of the given value is above the defined number. | ||
const ResultNotMaxLength = "NOT_MAX_LENGTH" | ||
|
||
// IsMaxLength checks if the length of the given value is less than the given maximum length. | ||
func IsMaxLength(value interface{}, maxLength int) Result { | ||
return checkMaxLength(reflect.Indirect(reflect.ValueOf(value)), reflect.ValueOf(nil), maxLength) | ||
} | ||
|
||
// makeMaxLength makes a checker function for the max length checker. | ||
func makeMaxLength(config string) CheckFunc { | ||
maxLength, err := strconv.Atoi(config) | ||
if err != nil { | ||
panic("unable to parse max length value") | ||
} | ||
|
||
return func(value, parent reflect.Value) Result { | ||
return checkMaxLength(value, parent, maxLength) | ||
} | ||
} | ||
|
||
// checkMaxLength checks if the length of the given value is less than the given maximum length. | ||
// The function uses the reflect.Value.Len() function to determaxe the length of the value. | ||
func checkMaxLength(value, _ reflect.Value, maxLength int) Result { | ||
if value.Len() > maxLength { | ||
return ResultNotMaxLength | ||
} | ||
|
||
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 TestIsMaxLengthValid(t *testing.T) { | ||
s := "1234" | ||
|
||
if IsMaxLength(s, 4) != ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckMaxLengthInvalidConfig(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Fail() | ||
} | ||
}() | ||
|
||
type User struct { | ||
Password string `checkers:"max-length:AB"` | ||
} | ||
|
||
user := &User{} | ||
|
||
Check(user) | ||
} | ||
|
||
func TestCheckMaxLengthValid(t *testing.T) { | ||
type User struct { | ||
Password string `checkers:"max-length:4"` | ||
} | ||
|
||
user := &User{ | ||
Password: "1234", | ||
} | ||
|
||
_, valid := Check(user) | ||
if !valid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckMaxLengthInvalid(t *testing.T) { | ||
type User struct { | ||
Password string `checkers:"max-length:4"` | ||
} | ||
|
||
user := &User{ | ||
Password: "123456", | ||
} | ||
|
||
_, valid := Check(user) | ||
if valid { | ||
t.Fail() | ||
} | ||
} |