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

ref: #441. validation for negative integer range #442

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var ParamTagMap = map[string]ParamValidator{

// ParamTagRegexMap maps param tags to their respective regexes.
var ParamTagRegexMap = map[string]*regexp.Regexp{
"range": regexp.MustCompile("^range\\((\\d+)\\|(\\d+)\\)$"),
"range": regexp.MustCompile("^range\\((-?\\d+)\\|(-?\\d+)\\)$"),
"length": regexp.MustCompile("^length\\((\\d+)\\|(\\d+)\\)$"),
"runelength": regexp.MustCompile("^runelength\\((\\d+)\\|(\\d+)\\)$"),
"stringlength": regexp.MustCompile("^stringlength\\((\\d+)\\|(\\d+)\\)$"),
Expand Down
15 changes: 8 additions & 7 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3442,11 +3442,12 @@ func ExampleValidateStruct() {

func TestValidateStructParamValidatorInt(t *testing.T) {
type Test1 struct {
Int int `valid:"range(1|10)"`
Int8 int8 `valid:"range(1|10)"`
Int16 int16 `valid:"range(1|10)"`
Int32 int32 `valid:"range(1|10)"`
Int64 int64 `valid:"range(1|10)"`
Int int `valid:"range(1|10)"`
Int8 int8 `valid:"range(1|10)"`
Int16 int16 `valid:"range(1|10)"`
Int32 int32 `valid:"range(1|10)"`
Int64 int64 `valid:"range(1|10)"`
NegInt int `valid:"range(-1|-10)"`

Uint uint `valid:"range(1|10)"`
Uint8 uint8 `valid:"range(1|10)"`
Expand All @@ -3457,8 +3458,8 @@ func TestValidateStructParamValidatorInt(t *testing.T) {
Float32 float32 `valid:"range(1|10)"`
Float64 float64 `valid:"range(1|10)"`
}
test1Ok := &Test1{5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}
test1NotOk := &Test1{11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}
test1Ok := &Test1{5, 5, 5, 5, 5, -5, 5, 5, 5, 5, 5, 5, 5}
test1NotOk := &Test1{11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}

_, err := ValidateStruct(test1Ok)
if err != nil {
Expand Down