Skip to content

Commit

Permalink
Max Length checker added.
Browse files Browse the repository at this point in the history
  • Loading branch information
cinar committed Jun 16, 2023
1 parent ccf62db commit ca9657a
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 32 additions & 0 deletions doc/checkers/maxlength.md
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
}
```
39 changes: 39 additions & 0 deletions maxlength.go
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
}
57 changes: 57 additions & 0 deletions maxlength_test.go
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()
}
}

0 comments on commit ca9657a

Please sign in to comment.