Skip to content

Commit

Permalink
Min length checker added. (#32)
Browse files Browse the repository at this point in the history
Fixes #15
  • Loading branch information
cinar authored Jun 16, 2023
1 parent 39241a1 commit ccf62db
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ type Person struct {

This package currently provides the following checkers:

- [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.
5 changes: 3 additions & 2 deletions checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ const ResultValid Result = "VALID"

// makers provides mapping to maker function for the checkers.
var makers = map[string]MakeFunc{
"required": makeRequired,
"same": makeSame,
CheckerMinLength: makeMinLength,
CheckerRequired: makeRequired,
CheckerSame: makeSame,
}

// Register registers the given checker name and the maker function.
Expand Down
32 changes: 32 additions & 0 deletions doc/checkers/minlength.md
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
}
```
39 changes: 39 additions & 0 deletions minlenght.go
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
}
57 changes: 57 additions & 0 deletions minlength_test.go
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()
}
}
3 changes: 3 additions & 0 deletions required.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package checker

import "reflect"

// CheckerRequired is the name of the checker.
const CheckerRequired = "required"

// ResultRequired indicates that the required value is missing.
const ResultRequired Result = "REQUIRED"

Expand Down
3 changes: 3 additions & 0 deletions same.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"reflect"
)

// CheckerSame is the name of the checker.
const CheckerSame = "same"

// ResultNotSame indicates that the given two values are not equal to each other.
const ResultNotSame = "NOT_SAME"

Expand Down

0 comments on commit ccf62db

Please sign in to comment.