-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
300 additions
and
0 deletions.
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
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,30 @@ | ||
# Max Checker | ||
|
||
The ```max``` checker checks if the given ```int``` or ```float``` value is less than the given maximum. If the value is above the maximum, the checker will return the ```NOT_MAX``` result. Here is an example: | ||
|
||
```golang | ||
type Order struct { | ||
Quantity int `checkers:"max:10"` | ||
} | ||
|
||
order := &Order{ | ||
Quantity: 5, | ||
} | ||
|
||
mistakes, valid := Check(order) | ||
if !valid { | ||
// Send the mistakes back to the user | ||
} | ||
``` | ||
|
||
In your custom checkers, you can call the ```max``` checker function ```IsMax``` to validate the user input. Here is an example: | ||
|
||
```golang | ||
quantity := 5 | ||
|
||
result := IsMax(quantity, 10 ) | ||
|
||
if result != ResultValid { | ||
// Send the mistakes back to the user | ||
} | ||
``` |
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,30 @@ | ||
# Min Checker | ||
|
||
The ```min``` checker checks if the given ```int``` or ```float``` value is greather than the given minimum. If the value is below the minimum, the checker will return the ```NOT_MIN``` result. Here is an example: | ||
|
||
```golang | ||
type User struct { | ||
Age int `checkers:"min:21"` | ||
} | ||
|
||
user := &User{ | ||
Age: 45, | ||
} | ||
|
||
mistakes, valid := Check(user) | ||
if !valid { | ||
// Send the mistakes back to the user | ||
} | ||
``` | ||
|
||
In your custom checkers, you can call the ```min``` checker function ```IsMin``` to validate the user input. Here is an example: | ||
|
||
```golang | ||
age := 45 | ||
|
||
result := IsMin(age, 21) | ||
|
||
if result != ResultValid { | ||
// Send the mistakes back to the user | ||
} | ||
``` |
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,40 @@ | ||
package checker | ||
|
||
import ( | ||
"reflect" | ||
"strconv" | ||
) | ||
|
||
// CheckerMax is the name of the checker. | ||
const CheckerMax = "max" | ||
|
||
// ResultNotMax indicates that the given value is above the defined maximum. | ||
const ResultNotMax = "NOT_MIN" | ||
|
||
// IsMax checks if the given value is below than the given maximum. | ||
func IsMax(value interface{}, max float64) Result { | ||
return checkMax(reflect.Indirect(reflect.ValueOf(value)), reflect.ValueOf(nil), max) | ||
} | ||
|
||
// makeMax makes a checker function for the max checker. | ||
func makeMax(config string) CheckFunc { | ||
max, err := strconv.ParseFloat(config, 64) | ||
if err != nil { | ||
panic("unable to parse max") | ||
} | ||
|
||
return func(value, parent reflect.Value) Result { | ||
return checkMax(value, parent, max) | ||
} | ||
} | ||
|
||
// checkMax checks if the given value is less than the given maximum. | ||
func checkMax(value, _ reflect.Value, max float64) Result { | ||
n := numberOf(value) | ||
|
||
if n > max { | ||
return ResultNotMax | ||
} | ||
|
||
return ResultValid | ||
} |
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,57 @@ | ||
package checker | ||
|
||
import "testing" | ||
|
||
func TestIsMaxValid(t *testing.T) { | ||
n := 5 | ||
|
||
if IsMax(n, 10) != ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckMaxInvalidConfig(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Fail() | ||
} | ||
}() | ||
|
||
type Order struct { | ||
Quantity int `checkers:"max:AB"` | ||
} | ||
|
||
order := &Order{} | ||
|
||
Check(order) | ||
} | ||
|
||
func TestCheckMaxValid(t *testing.T) { | ||
type Order struct { | ||
Quantity int `checkers:"max:10"` | ||
} | ||
|
||
order := &Order{ | ||
Quantity: 5, | ||
} | ||
|
||
_, valid := Check(order) | ||
if !valid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckMaxInvalid(t *testing.T) { | ||
type Order struct { | ||
Quantity int `checkers:"max:10"` | ||
} | ||
|
||
order := &Order{ | ||
Quantity: 20, | ||
} | ||
|
||
_, valid := Check(order) | ||
if valid { | ||
t.Fail() | ||
} | ||
} |
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,40 @@ | ||
package checker | ||
|
||
import ( | ||
"reflect" | ||
"strconv" | ||
) | ||
|
||
// CheckerMin is the name of the checker. | ||
const CheckerMin = "min" | ||
|
||
// ResultNotMin indicates that the given value is below the defined minimum. | ||
const ResultNotMin = "NOT_MIN" | ||
|
||
// IsMin checks if the given value is above than the given minimum. | ||
func IsMin(value interface{}, min float64) Result { | ||
return checkMin(reflect.Indirect(reflect.ValueOf(value)), reflect.ValueOf(nil), min) | ||
} | ||
|
||
// makeMin makes a checker function for the min checker. | ||
func makeMin(config string) CheckFunc { | ||
min, err := strconv.ParseFloat(config, 64) | ||
if err != nil { | ||
panic("unable to parse min") | ||
} | ||
|
||
return func(value, parent reflect.Value) Result { | ||
return checkMin(value, parent, min) | ||
} | ||
} | ||
|
||
// checkMin checks if the given value is greather than the given minimum. | ||
func checkMin(value, _ reflect.Value, min float64) Result { | ||
n := numberOf(value) | ||
|
||
if n < min { | ||
return ResultNotMin | ||
} | ||
|
||
return ResultValid | ||
} |
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,57 @@ | ||
package checker | ||
|
||
import "testing" | ||
|
||
func TestIsMinValid(t *testing.T) { | ||
n := 45 | ||
|
||
if IsMin(n, 21) != ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckMinInvalidConfig(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Fail() | ||
} | ||
}() | ||
|
||
type User struct { | ||
Age int `checkers:"min:AB"` | ||
} | ||
|
||
user := &User{} | ||
|
||
Check(user) | ||
} | ||
|
||
func TestCheckMinValid(t *testing.T) { | ||
type User struct { | ||
Age int `checkers:"min:21"` | ||
} | ||
|
||
user := &User{ | ||
Age: 45, | ||
} | ||
|
||
_, valid := Check(user) | ||
if !valid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckMinInvalid(t *testing.T) { | ||
type User struct { | ||
Age int `checkers:"min:21"` | ||
} | ||
|
||
user := &User{ | ||
Age: 18, | ||
} | ||
|
||
_, valid := Check(user) | ||
if valid { | ||
t.Fail() | ||
} | ||
} |