Skip to content

Commit

Permalink
Merge pull request #17 from rezakhademix/improve-required-int-method
Browse files Browse the repository at this point in the history
improved: required int method
  • Loading branch information
rezakhademix authored Mar 4, 2024
2 parents 13fe0eb + 909fc86 commit 3c2db4a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion required.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (v *Validator) RequiredString(s, field string, msg string) *Validator {

// RequiredInt checks if an integer value is provided or not.
func (v *Validator) RequiredInt(i int, field string, msg string) *Validator {
v.Check(i == 0, field, v.msg(Required, msg, field))
v.Check(i != 0, field, v.msg(Required, msg, field))

return v
}
Expand Down
37 changes: 37 additions & 0 deletions required_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,43 @@ import (
"github.com/stretchr/testify/assert"
)

func TestValidator_RequiredInt(t *testing.T) {
tests := []struct {
tag string
value int
message string
isPassed bool
expectedMsg string
}{
{
tag: "t0",
value: 1,
message: "",
isPassed: true,
expectedMsg: "",
},
{
tag: "t1",
value: 0,
message: "t1 is required",
isPassed: false,
expectedMsg: "t1 is required",
},
}

v := New()

for _, test := range tests {
v.RequiredInt(test.value, test.tag, test.message)

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

if !test.isPassed {
assert.Equal(t, test.expectedMsg, v.Errors()[test.tag])
}
}
}

func TestValidator_RequiredString(t *testing.T) {
tests := []struct {
tag string
Expand Down

0 comments on commit 3c2db4a

Please sign in to comment.