Skip to content

Commit

Permalink
ref: asaskevich#441. validation for negative integer range
Browse files Browse the repository at this point in the history
  • Loading branch information
joicemjoseph committed May 6, 2021
1 parent f21760c commit e27a4e2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
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

0 comments on commit e27a4e2

Please sign in to comment.