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

feat: add IsPositive tag validator #453

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ var TagMap = map[string]Validator{
"float": IsFloat,
"null": IsNull,
"notnull": IsNotNull,
"positive": Positive,
"uuid": IsUUID,
"uuidv3": IsUUIDv3,
"uuidv4": IsUUIDv4,
Expand Down
6 changes: 6 additions & 0 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ func IsUTFNumeric(str string) bool {

}

// Positive checks if the a positive numeric value.
func Positive(str string) bool {
value, _ := ToFloat(str)
return IsPositive(value)
}

// IsUTFDigit checks if the string contains only unicode radix-10 decimal digits. Empty string is valid.
func IsUTFDigit(str string) bool {
if IsNull(str) {
Expand Down
22 changes: 22 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3543,6 +3543,28 @@ func TestValidateStructParamValidatorInt(t *testing.T) {
}
}

func TestValidateStructPositive(t *testing.T) {
t.Parallel()
type Test struct {
Value int `valid:"positive"`
}

var tests = []struct {
test Test
err bool
}{
{Test{1}, false},
{Test{-1}, true},
{Test{0}, false},
}
for _, test := range tests {
if _, err := ValidateStruct(test.test); (err != nil) != test.err {
t.Errorf("Expected ValidateStruct error to be %v, got %v", test.err, err)

}
}
}

func TestValidateStructUpperAndLowerCaseWithNumTypeCheck(t *testing.T) {

type StructCapital struct {
Expand Down