Skip to content

Commit

Permalink
Min length checker added.
Browse files Browse the repository at this point in the history
  • Loading branch information
cinar committed Jun 16, 2023
1 parent 39241a1 commit 0b8b43c
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
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
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",
}

_, ok := Check(user)
if !ok {
t.Fail()
}
}

func TestCheckMinLengthInvalid(t *testing.T) {
type User struct {
Password string `checkers:"min-length:4"`
}

user := &User{
Password: "12",
}

_, ok := Check(user)
if ok {
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 0b8b43c

Please sign in to comment.