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
1 parent
34bd547
commit 393bfcd
Showing
4 changed files
with
112 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
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,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") | ||
} | ||
|
||
} |