diff --git a/types.go b/types.go index c573abb..f4b97ec 100644 --- a/types.go +++ b/types.go @@ -132,6 +132,7 @@ var TagMap = map[string]Validator{ "float": IsFloat, "null": IsNull, "notnull": IsNotNull, + "positive": Positive, "uuid": IsUUID, "uuidv3": IsUUIDv3, "uuidv4": IsUUIDv4, diff --git a/validator.go b/validator.go index 46ecfc8..04f7121 100644 --- a/validator.go +++ b/validator.go @@ -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) { diff --git a/validator_test.go b/validator_test.go index bff8613..713e807 100644 --- a/validator_test.go +++ b/validator_test.go @@ -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 {