-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add range rule implementation and tests (#8)
* Add range rule implementation and tests * Add more tests for range validation * Use the right copyright header...
- Loading branch information
Showing
5 changed files
with
277 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2016 Qiang Xue. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package validation | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
// Range returns a validation rule that checks if a value is within the given range: [min,max]. | ||
// Note that the value being checked and the boundary values must be of the same type. | ||
func Range(min interface{}, max interface{}) *rangeRule { | ||
return &rangeRule{ | ||
min: min, | ||
max: max, | ||
message: fmt.Sprintf("must be between %v and %v", min, max), | ||
} | ||
} | ||
|
||
type rangeRule struct { | ||
min interface{} | ||
max interface{} | ||
message string | ||
} | ||
|
||
// Validate checks if the given value is valid or not. | ||
func (r *rangeRule) Validate(value interface{}) error { | ||
value, isNil := Indirect(value) | ||
if isNil { | ||
return nil | ||
} | ||
|
||
rv := reflect.ValueOf(value) | ||
switch rv.Kind() { | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
min, err := ToInt(r.min) | ||
if err != nil { | ||
return err | ||
} | ||
max, err := ToInt(r.max) | ||
if err != nil { | ||
return err | ||
} | ||
if min <= rv.Int() && rv.Int() <= max { | ||
return nil | ||
} | ||
|
||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: | ||
min, err := ToUint(r.min) | ||
if err != nil { | ||
return err | ||
} | ||
max, err := ToUint(r.max) | ||
if err != nil { | ||
return err | ||
} | ||
if min <= rv.Uint() && rv.Uint() <= max { | ||
return nil | ||
} | ||
|
||
case reflect.Float32, reflect.Float64: | ||
min, err := ToFloat(r.min) | ||
if err != nil { | ||
return err | ||
} | ||
max, err := ToFloat(r.max) | ||
if err != nil { | ||
return err | ||
} | ||
if min <= rv.Float() && rv.Float() <= max { | ||
return nil | ||
} | ||
|
||
default: | ||
r.message = fmt.Sprintf("cannot apply range rule on type %v", rv.Kind()) | ||
} | ||
return errors.New(r.message) | ||
} | ||
|
||
// Error sets the error message for the rule. | ||
func (r *rangeRule) Error(message string) *rangeRule { | ||
r.message = message | ||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2016 Qiang Xue. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package validation | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRange(t *testing.T) { | ||
var i int | ||
var u uint | ||
var f float64 | ||
|
||
tests := []struct { | ||
tag string | ||
min, max interface{} | ||
value interface{} | ||
err string | ||
}{ | ||
{"t1", 2, 4, "", "cannot apply range rule on type string"}, | ||
{"t2", 2, 4, "abc", "cannot apply range rule on type string"}, | ||
{"t3", 2, 4, []int{1, 2}, "cannot apply range rule on type slice"}, | ||
{"t4", 2, 4, map[string]int{"A": 1}, "cannot apply range rule on type map"}, | ||
{"t5", 0, 2, nil, ""}, | ||
|
||
{"t6", 2, 4, 2, ""}, | ||
{"t7", 2, 4, 3, ""}, | ||
{"t8", 2, 4, 4, ""}, | ||
{"t9", 0, 2, &i, ""}, | ||
{"t10", 1, 2, &i, "must be between 1 and 2"}, | ||
{"t11", 2, 4, 5, "must be between 2 and 4"}, | ||
{"t12", uint(2), 4, 3, "cannot convert uint to int64"}, | ||
{"t13", 2, uint(4), 3, "cannot convert uint to int64"}, | ||
|
||
{"t14", uint(0), uint(2), &u, ""}, | ||
{"t15", uint(1), uint(2), &u, "must be between 1 and 2"}, | ||
{"t16", uint(0), uint(1), uint(1), ""}, | ||
{"t17", uint(0), uint(1), uint(2), "must be between 0 and 1"}, | ||
{"t18", 0, uint(2), uint(1), "cannot convert int to uint64"}, | ||
{"t19", uint(0), 2, uint(1), "cannot convert int to uint64"}, | ||
|
||
{"t20", 0.0, 2.0, &f, ""}, | ||
{"t21", 1.0, 2.0, &f, "must be between 1 and 2"}, | ||
{"t22", 0.0, 0.1, 1.0, "must be between 0 and 0.1"}, | ||
{"t23", 0, 1.0, 0.5, "cannot convert int to float64"}, | ||
{"t24", 0.0, 1, 1.0, "cannot convert int to float64"}, | ||
} | ||
|
||
for _, test := range tests { | ||
r := Range(test.min, test.max) | ||
err := r.Validate(test.value) | ||
assertError(t, test.err, err, test.tag) | ||
} | ||
} | ||
|
||
func TestRangeError(t *testing.T) { | ||
r := Range(10, 20) | ||
assert.Equal(t, "must be between 10 and 20", r.message) | ||
|
||
r.Error("123") | ||
assert.Equal(t, "123", r.message) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters