From 643cfeeab2a5683daeffc98dfb72cc26039e59a5 Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Thu, 15 Jun 2023 18:51:16 -0700 Subject: [PATCH] Max Length checker added. (#33) Fixes #16 --- README.md | 1 + checker.go | 1 + doc/checkers/maxlength.md | 32 ++++++++++++++++++++++ maxlength.go | 39 +++++++++++++++++++++++++++ maxlength_test.go | 57 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 130 insertions(+) create mode 100644 doc/checkers/maxlength.md create mode 100644 maxlength.go create mode 100644 maxlength_test.go diff --git a/README.md b/README.md index 71c2729..bd0f3e1 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/checker.go b/checker.go index 504be13..687f4d4 100644 --- a/checker.go +++ b/checker.go @@ -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, diff --git a/doc/checkers/maxlength.md b/doc/checkers/maxlength.md new file mode 100644 index 0000000..901903a --- /dev/null +++ b/doc/checkers/maxlength.md @@ -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 +} +``` diff --git a/maxlength.go b/maxlength.go new file mode 100644 index 0000000..44e28ef --- /dev/null +++ b/maxlength.go @@ -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 +} diff --git a/maxlength_test.go b/maxlength_test.go new file mode 100644 index 0000000..496dca4 --- /dev/null +++ b/maxlength_test.go @@ -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() + } +}