parent
ccf62dbd6a
commit
643cfeeab2
5 changed files with 130 additions and 0 deletions
|
|
@ -66,6 +66,7 @@ type Person struct {
|
|||
|
||||
This package currently provides the following checkers:
|
||||
|
||||
- [max-length](doc/checkers/maxlength.md) checks if the length of the given value is less than the given maximum length.
|
||||
- [min-length](doc/checkers/minlength.md) checks if the length of the given value is greather than the given minimum length.
|
||||
- [required](doc/checkers/required.md) checks if the required value is provided.
|
||||
- [same](doc/checkers/same.md) checks if the given value is equal to the value of the field with the given name.
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ const ResultValid Result = "VALID"
|
|||
|
||||
// makers provides mapping to maker function for the checkers.
|
||||
var makers = map[string]MakeFunc{
|
||||
CheckerMaxLength: makeMaxLength,
|
||||
CheckerMinLength: makeMinLength,
|
||||
CheckerRequired: makeRequired,
|
||||
CheckerSame: makeSame,
|
||||
|
|
|
|||
32
doc/checkers/maxlength.md
Normal file
32
doc/checkers/maxlength.md
Normal file
|
|
@ -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
|
||||
}
|
||||
```
|
||||
39
maxlength.go
Normal file
39
maxlength.go
Normal file
|
|
@ -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
|
||||
}
|
||||
57
maxlength_test.go
Normal file
57
maxlength_test.go
Normal file
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue