diff --git a/required.go b/required.go index 3039f90..8ddfc30 100644 --- a/required.go +++ b/required.go @@ -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 } diff --git a/required_test.go b/required_test.go index 4663362..6c05653 100644 --- a/required_test.go +++ b/required_test.go @@ -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