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

fix: support negative range values #452

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
21 changes: 21 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3440,6 +3440,27 @@ func ExampleValidateStruct() {
println(result)
}

func TestValidateStructRange(t *testing.T) {
type Test1 struct {
Range1 int `valid:"range(0|1)"`
Range2 int `valid:"range(1|10)"`
Range3 int `valid:"range(-10|10)"`
Range4 int `valid:"range(-10|-1)"`
}
test1Ok := &Test1{1, 1, -1, -1}
test1NotOk := &Test1{-1, -1, 11, 11}

_, err := ValidateStruct(test1Ok)
if err != nil {
t.Errorf("Test failed: %s", err)
}

_, err = ValidateStruct(test1NotOk)
if err == nil {
t.Errorf("Test failed: nil")
}
}

func TestValidateStructParamValidatorInt(t *testing.T) {
type Test1 struct {
Int int `valid:"range(1|10)"`
Expand Down