Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Min length checker added. #32

Merged
merged 2 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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