Skip to content

Commit

Permalink
Merge pull request #50 from rezakhademix/add-max-min-string-len-valid…
Browse files Browse the repository at this point in the history
…ation-rule

added: min, max string validation rule
  • Loading branch information
rezakhademix authored Mar 9, 2024
2 parents c405d16 + 4478957 commit ec66c98
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 4 deletions.
17 changes: 17 additions & 0 deletions max.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package validator

import "strings"

const (
// Max represents the rule name which will be used to find the default error message.
Max = "max"
// MaxString represents the rule name which will be used to find the default error message.
MaxString = "maxString"
// MaxMsg is the default error message format for fields with the maximum validation rule.
MaxMsg = "%s should be less than %v"
// MaxStringMsg is the default error message format for fields with the MaxString validation rule.
MaxStringMsg = "%s should has less than %v characters"
)

// MaxInt checks if the integer value is less than or equal the given max value.
Expand All @@ -28,3 +34,14 @@ func (v *Validator) MaxFloat(f, max float64, field, msg string) *Validator {

return v
}

// MaxString checks if the length of given string is less than or equal the given max value.
//
// Example:
//
// validator.MaxString("rey", 5, "name", "name should has less than 5 characters.")
func (v *Validator) MaxString(s string, maxLen int, field, msg string) *Validator {
v.Check(len(strings.TrimSpace(s)) <= maxLen, field, v.msg(MaxString, msg, field, maxLen))

return v
}
68 changes: 68 additions & 0 deletions max_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,71 @@ func Test_MaxFloat64(t *testing.T) {
}
}
}

func Test_MaxString(t *testing.T) {
tests := []struct {
name string
field string
value string
max int
isPassed bool
msg string
expectedMsg string
}{
{
name: "test `rey` will pass validation when maximum valid length is 5",
field: "name",
value: "rey",
max: 5,
isPassed: true,
msg: "",
expectedMsg: "",
},
{
name: "test empty string will pass validation when maximum valid length is 2",
field: "username",
value: "",
max: 2,
isPassed: true,
msg: "",
expectedMsg: "",
},
{
name: "test empty space string won't pass validation when maximum valid length is 0",
field: "username",
value: " ",
max: -1,
isPassed: false,
msg: "",
expectedMsg: "username should has less than -1 characters",
},
{
name: "test `abcd` won't pass validation when maximum valid length is 3",
field: "alphabet",
value: "abcd",
max: 3,
isPassed: false,
msg: "alphabet should has less than 3 characters",
expectedMsg: "alphabet should has less than 3 characters",
},
}

v := New()

for _, test := range tests {
v.MaxString(test.value, test.max, test.field, test.msg)

assert.Equal(t, test.isPassed, v.IsPassed())

if !test.isPassed {
assert.Equalf(
t,
test.expectedMsg,
v.Errors()[test.field],
"test case %q failed: expected: %s, got: %s",
test.expectedMsg,
v.Errors()[test.field],
)
}
}
}
17 changes: 17 additions & 0 deletions min.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package validator

import "strings"

const (
// Min represents the rule name which will be used to find the default error message.
Min = "min"
// MinString represents the rule name which will be used to find the default error message.
MinString = "minString"
// MinMsg is the default error message format for fields with the minimum validation rule.
MinMsg = "%s should be more than %v"
// MinStringMsg is the default error message format for fields with the MinString validation rule.
MinStringMsg = "%s should has more than %v characters"
)

// MinInt checks if the given integer value is greater than or equal the given min value.
Expand All @@ -28,3 +34,14 @@ func (v *Validator) MinFloat(f, min float64, field, msg string) *Validator {

return v
}

// MinString checks if the length of given string is greater than or equal the given min value.
//
// Example:
//
// validator.MinString("rey", 5, "name", "name should has more than 5 characters.")
func (v *Validator) MinString(s string, minLen int, field, msg string) *Validator {
v.Check(len(strings.TrimSpace(s)) >= minLen, field, v.msg(MinString, msg, field, minLen))

return v
}
68 changes: 68 additions & 0 deletions min_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,71 @@ func Test_MinFloat(t *testing.T) {
}
}
}

func Test_MinString(t *testing.T) {
tests := []struct {
name string
field string
value string
min int
isPassed bool
msg string
expectedMsg string
}{
{
name: "test `rey` will pass validation when minimum valid length is 2",
field: "name",
value: "rey",
min: 2,
isPassed: true,
msg: "",
expectedMsg: "",
},
{
name: "test empty string won't pass validation when minimum valid length is 5",
field: "username",
value: "",
min: 5,
isPassed: false,
msg: "",
expectedMsg: "username should has more than 5 characters",
},
{
name: "test empty space string won't pass validation when minimum valid length is 2",
field: "username",
value: " ",
min: 2,
isPassed: false,
msg: "",
expectedMsg: "username should has more than 2 characters",
},
{
name: "test `abcd` won't pass validation when minimum valid length is 7",
field: "alphabet",
value: "abcd",
min: 7,
isPassed: false,
msg: "alphabet should has more than 7 characters",
expectedMsg: "alphabet should has more than 7 characters",
},
}

v := New()

for _, test := range tests {
v.MinString(test.value, test.min, test.field, test.msg)

assert.Equal(t, test.isPassed, v.IsPassed())

if !test.isPassed {
assert.Equalf(
t,
test.expectedMsg,
v.Errors()[test.field],
"test case %q failed: expected: %s, got: %s",
test.expectedMsg,
v.Errors()[test.field],
)
}
}
}
4 changes: 3 additions & 1 deletion validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var (
Len: LenMsg,
LenList: LenListMsg,
Max: MaxMsg,
MaxString: MaxStringMsg,
MinString: MinStringMsg,
Min: MinMsg,
Between: BetweenMsg,
NotExists: NotExistsMsg,
Expand All @@ -43,7 +45,7 @@ var (
}

// ErrMethodMessageNotFound is the default message when a method does not have any error message on methodToErrorMessage.
ErrMethodMessageNotFound = errors.New("method message does not exist")
ErrMethodMessageNotFound = errors.New("method default validation message does not exist in methodToErrorMessage")
)

// New will return a new validator
Expand Down
6 changes: 3 additions & 3 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ func Test_msg(t *testing.T) {
name: "test not exists method will result a panic",
method: "qwert",
msg: "",
expectedMsg: "method message does not exist",
expectedMsg: "method default validation message does not exist in methodToErrorMessage",
},
{
name: "test empty string method will result a panic",
method: "",
msg: "",
expectedMsg: "method message does not exist",
expectedMsg: "method default validation message does not exist in methodToErrorMessage",
},
{
name: "test empty space string method will result a panic",
method: " ",
msg: "",
expectedMsg: "method message does not exist",
expectedMsg: "method default validation message does not exist in methodToErrorMessage",
},
}

Expand Down

0 comments on commit ec66c98

Please sign in to comment.