Skip to content

Commit

Permalink
add int rule
Browse files Browse the repository at this point in the history
  • Loading branch information
jakesylvestre committed Jan 24, 2021
1 parent 34bd547 commit 393bfcd
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -4,5 +4,6 @@ go 1.13

require (
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496
github.com/brianvoe/gofakeit/v5 v5.11.2
github.com/stretchr/testify v1.4.0
)
2 changes: 2 additions & 0 deletions go.sum
@@ -1,5 +1,7 @@
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 h1:zV3ejI06GQ59hwDQAvmK1qxOQGB3WuVTRoY0okPTAv0=
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
github.com/brianvoe/gofakeit/v5 v5.11.2 h1:Ny5Nsf4z2023ZvYP8ujW8p5B1t5sxhdFaQ/0IYXbeSA=
github.com/brianvoe/gofakeit/v5 v5.11.2/go.mod h1:/ZENnKqX+XrN8SORLe/fu5lZDIo1tuPncWuRD+eyhSI=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
58 changes: 58 additions & 0 deletions int_rule.go
@@ -0,0 +1,58 @@
package validation

// modeled after string valdiator

type intValidator func(int64) bool

// int rule checks an int variable using a specified intValidator
type IntRule struct {
validate intValidator
err Error
}

// NewIntRule creates a new validation rule using a function that takes a int value and returns a bool.
// The rule returned will use the function to check if a given int is valid or not.
func NewIntRule(validator intValidator, message string) IntRule {
return IntRule{
validate: validator,
err: NewError("", message),
}
}

// NewIntRuleWithError creates a new validation rule using a function that takes a int value and returns a bool.
func NewIntRuleWithError(validator intValidator, err Error) IntRule {
return IntRule{
validate: validator,
err: err,
}
}

// Error sets the error message for the rule.
func (i IntRule) Error(message string) IntRule {
i.err = i.err.SetMessage(message)
return i
}

// ErrorObject sets the error struct for the rule.
func (i IntRule) ErrorObject(err Error) IntRule {
i.err = err
return i
}

func (i IntRule) Validate(value interface{}) error {
value, isNil := Indirect(value)
if isNil {
return nil
}

intVal, err := ToInt(value)
if err != nil {
return err
}

if i.validate(intVal) {
return nil
}

return i.err
}
51 changes: 51 additions & 0 deletions int_rule_test.go
@@ -0,0 +1,51 @@
package validation

import (
"github.com/brianvoe/gofakeit/v5"
"testing"
)

// mock true return in rule
func testIntValidatorTrue(int int64) bool {
return true
}

// mock false return in rule
func testIntValidatorFalse(int int64) bool {
return false
}

var errShouldBeTrue = NewError("test_err_should_be_true", "error should return true")

func TestNewIntRule(t *testing.T) {
// test raw int rule false
rule := NewIntRule(testIntValidatorFalse, "this is a test, this should be false")
err := rule.Validate(gofakeit.Int64())
if err == nil {
t.Error("expected error when using testIntValidatorFalse")
}

// test raw int rule true
rule = NewIntRule(testIntValidatorTrue, "this is a test, this should be false")
rule.ErrorObject(errShouldBeTrue)
rule.Error("this is a test, this should be false")
err = rule.Validate(gofakeit.Int64())
if err != nil {
t.Error("expected error when using testIntValidatorTrue")
}

// test intrule with error
rule = NewIntRuleWithError(testIntValidatorFalse, errShouldBeTrue)
err = rule.Validate(gofakeit.Int64())
if err == nil {
t.Error("expected error")
}

// test wrong type
rule = NewIntRuleWithError(testIntValidatorTrue, errShouldBeTrue)
err = rule.Validate(gofakeit.Name())
if err == nil {
t.Error("wrong type should result in err")
}

}

0 comments on commit 393bfcd

Please sign in to comment.