Skip to content

Commit

Permalink
added: Required float64 validation rule
Browse files Browse the repository at this point in the history
  • Loading branch information
masoudfesahat committed Mar 4, 2024
1 parent 03e45c5 commit b823d57
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
6 changes: 6 additions & 0 deletions required.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ func (v *Validator) RequiredInt(i int, field string, msg string) *Validator {

return v
}

func (v *Validator) RequiredFloat(f float64, field string, msg string) *Validator {

Check failure on line 26 in required.go

View workflow job for this annotation

GitHub Actions / audit

exported method Validator.RequiredFloat should have comment or be unexported
v.Check(f != 0.0, field, v.msg(Required, msg, field))

return v
}
45 changes: 45 additions & 0 deletions required_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validator

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -48,3 +49,47 @@ func TestValidator_RequiredString(t *testing.T) {
}
}
}

func TestValidator_RequiredFloat(t *testing.T) {
tests := []struct {
tag string
value float64
message string
isPassed bool
expectedMsg string
}{
{
tag: "t0",
value: 1.0,
message: "",
isPassed: true,
expectedMsg: "f2 is required",
},
{
tag: "t1",
value: -1.0,
message: "f2 is required",
isPassed: true,
expectedMsg: fmt.Sprintf(RequiredMsg, "t1"),
},
{
tag: "t2",
value: 0.0,
message: "f2 is required",
isPassed: false,
expectedMsg: "f2 is required",
},
}

v := New()

for _, test := range tests {
v.RequiredFloat(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])
}
}
}

0 comments on commit b823d57

Please sign in to comment.