-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from rezakhademix/add-custom-rule-method
feat: custom rule method added
- Loading branch information
Showing
18 changed files
with
123 additions
and
27 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,11 @@ | ||
package govalidator | ||
|
||
// CustomRule is a dynamic method to define any custom validation rule by passing a rule as a function or expression | ||
// which will return a boolean. | ||
func (v Validator) CustomRule(ok bool, field, msg string) Validator { | ||
if !ok { | ||
v.addError(field, msg) | ||
} | ||
|
||
return v | ||
} |
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,70 @@ | ||
package govalidator | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func Test_CustomRule(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
field string | ||
value bool | ||
isPassed bool | ||
msg string | ||
expectedMsg string | ||
}{ | ||
{ | ||
name: "test CustomRule with true condition", | ||
value: true, | ||
field: "username", | ||
msg: "", | ||
isPassed: true, | ||
expectedMsg: "", | ||
}, | ||
{ | ||
name: "test CustomRule with false condition and custom message", | ||
value: false, | ||
field: "username", | ||
msg: "username must be unique", | ||
isPassed: false, | ||
expectedMsg: "username must be unique", | ||
}, | ||
{ | ||
name: "test CustomRule with false condition and empty message", | ||
value: false, | ||
field: "email", | ||
msg: "", | ||
isPassed: false, | ||
expectedMsg: "", | ||
}, | ||
{ | ||
name: "test CustomRule with true condition and custom message", | ||
value: true, | ||
field: "email", | ||
msg: "email must be valid", | ||
isPassed: true, | ||
expectedMsg: "", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
v := New() | ||
|
||
v.CustomRule(test.value, test.field, test.msg) | ||
|
||
assert.Equal(t, test.isPassed, v.IsPassed(), test.name) | ||
|
||
if !test.isPassed { | ||
assert.Equalf( | ||
t, | ||
test.expectedMsg, | ||
v.Errors()[test.field], | ||
"test case %q failed, expected: %s, got: %s", | ||
test.name, | ||
test.expectedMsg, | ||
v.Errors()[test.field], | ||
) | ||
} | ||
} | ||
} |
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
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
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
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