Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added: Required float64 validation rule #13

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions required.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ func (v *Validator) RequiredSlice(s []any, field string, msg string) *Validator

return v
}

// RequiredFloat checks if float value is provided or not.
func (v *Validator) RequiredFloat(f float64, field string, msg string) *Validator {
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 @@ -86,6 +87,50 @@ 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])
}
}
}

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