Skip to content

Commit 393bfcd

Browse files
committed
add int rule
1 parent 34bd547 commit 393bfcd

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ go 1.13
44

55
require (
66
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496
7+
github.com/brianvoe/gofakeit/v5 v5.11.2
78
github.com/stretchr/testify v1.4.0
89
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 h1:zV3ejI06GQ59hwDQAvmK1qxOQGB3WuVTRoY0okPTAv0=
22
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
3+
github.com/brianvoe/gofakeit/v5 v5.11.2 h1:Ny5Nsf4z2023ZvYP8ujW8p5B1t5sxhdFaQ/0IYXbeSA=
4+
github.com/brianvoe/gofakeit/v5 v5.11.2/go.mod h1:/ZENnKqX+XrN8SORLe/fu5lZDIo1tuPncWuRD+eyhSI=
35
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
46
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
57
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

int_rule.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package validation
2+
3+
// modeled after string valdiator
4+
5+
type intValidator func(int64) bool
6+
7+
// int rule checks an int variable using a specified intValidator
8+
type IntRule struct {
9+
validate intValidator
10+
err Error
11+
}
12+
13+
// NewIntRule creates a new validation rule using a function that takes a int value and returns a bool.
14+
// The rule returned will use the function to check if a given int is valid or not.
15+
func NewIntRule(validator intValidator, message string) IntRule {
16+
return IntRule{
17+
validate: validator,
18+
err: NewError("", message),
19+
}
20+
}
21+
22+
// NewIntRuleWithError creates a new validation rule using a function that takes a int value and returns a bool.
23+
func NewIntRuleWithError(validator intValidator, err Error) IntRule {
24+
return IntRule{
25+
validate: validator,
26+
err: err,
27+
}
28+
}
29+
30+
// Error sets the error message for the rule.
31+
func (i IntRule) Error(message string) IntRule {
32+
i.err = i.err.SetMessage(message)
33+
return i
34+
}
35+
36+
// ErrorObject sets the error struct for the rule.
37+
func (i IntRule) ErrorObject(err Error) IntRule {
38+
i.err = err
39+
return i
40+
}
41+
42+
func (i IntRule) Validate(value interface{}) error {
43+
value, isNil := Indirect(value)
44+
if isNil {
45+
return nil
46+
}
47+
48+
intVal, err := ToInt(value)
49+
if err != nil {
50+
return err
51+
}
52+
53+
if i.validate(intVal) {
54+
return nil
55+
}
56+
57+
return i.err
58+
}

int_rule_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package validation
2+
3+
import (
4+
"github.com/brianvoe/gofakeit/v5"
5+
"testing"
6+
)
7+
8+
// mock true return in rule
9+
func testIntValidatorTrue(int int64) bool {
10+
return true
11+
}
12+
13+
// mock false return in rule
14+
func testIntValidatorFalse(int int64) bool {
15+
return false
16+
}
17+
18+
var errShouldBeTrue = NewError("test_err_should_be_true", "error should return true")
19+
20+
func TestNewIntRule(t *testing.T) {
21+
// test raw int rule false
22+
rule := NewIntRule(testIntValidatorFalse, "this is a test, this should be false")
23+
err := rule.Validate(gofakeit.Int64())
24+
if err == nil {
25+
t.Error("expected error when using testIntValidatorFalse")
26+
}
27+
28+
// test raw int rule true
29+
rule = NewIntRule(testIntValidatorTrue, "this is a test, this should be false")
30+
rule.ErrorObject(errShouldBeTrue)
31+
rule.Error("this is a test, this should be false")
32+
err = rule.Validate(gofakeit.Int64())
33+
if err != nil {
34+
t.Error("expected error when using testIntValidatorTrue")
35+
}
36+
37+
// test intrule with error
38+
rule = NewIntRuleWithError(testIntValidatorFalse, errShouldBeTrue)
39+
err = rule.Validate(gofakeit.Int64())
40+
if err == nil {
41+
t.Error("expected error")
42+
}
43+
44+
// test wrong type
45+
rule = NewIntRuleWithError(testIntValidatorTrue, errShouldBeTrue)
46+
err = rule.Validate(gofakeit.Name())
47+
if err == nil {
48+
t.Error("wrong type should result in err")
49+
}
50+
51+
}

0 commit comments

Comments
 (0)