forked from fdonzello/validate
-
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.
Merge branch 'master' of github.com:markbates/validate
- Loading branch information
Showing
4 changed files
with
79 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package validators | ||
|
||
import ( | ||
"fmt" | ||
"github.com/markbates/validate" | ||
"strings" | ||
) | ||
|
||
type StringsMatch struct { | ||
Name string | ||
Field string | ||
Field2 string | ||
Message string | ||
} | ||
|
||
// IsValid performs the validation equality of two strings. | ||
func (v *StringsMatch) IsValid(errors *validate.Errors) { | ||
if strings.TrimSpace(v.Field) != strings.TrimSpace(v.Field2) { | ||
if v.Message == "" { | ||
v.Message = fmt.Sprintf("%s does not equal %s.", v.Field, v.Field2) | ||
} | ||
errors.Add(GenerateKey(v.Name), v.Message) | ||
} | ||
} |
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,45 @@ | ||
package validators_test | ||
|
||
import ( | ||
"github.com/markbates/validate" | ||
"github.com/markbates/validate/validators" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func Test_StringsMatch_IsValid(t *testing.T) { | ||
r := require.New(t) | ||
var cases = []struct { | ||
str1 string | ||
str2 string | ||
expected bool | ||
}{ | ||
{"test", "test", true}, | ||
{"test_fail", "test_true", false}, | ||
{"test with space", " test with space ", true}, | ||
{" test with space second", " test with space second ", true}, | ||
} | ||
|
||
for _, test_case := range cases { | ||
v := validators.StringsMatch{Name: "strings", Field: test_case.str1, Field2: test_case.str2} | ||
errors := validate.NewErrors() | ||
v.IsValid(errors) | ||
r.Equal(test_case.expected, !errors.HasAny(), "Str1: %s, Str2: %s", test_case.str1, test_case.str2) | ||
} | ||
} | ||
|
||
func BenchmarkStringsMatch_IsValid_Valid(b *testing.B) { | ||
errors := validate.NewErrors() | ||
for i := 0; i <= b.N; i++ { | ||
v := validators.StringsMatch{Name: "strings", Field: " Some string ", Field2: " Some string "} | ||
v.IsValid(errors) | ||
} | ||
} | ||
|
||
func BenchmarkStringsMatch_IsValid_InValid(b *testing.B) { | ||
errors := validate.NewErrors() | ||
for i := 0; i <= b.N; i++ { | ||
v := validators.StringsMatch{Name: "strings", Field: " Some string ", Field2: " Some string failure"} | ||
v.IsValid(errors) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -21,14 +21,16 @@ func Test_URLIsPresent(t *testing.T) { | |
{"google.com", false}, | ||
{"http://www.google.com", true}, | ||
{"http://google.com", true}, | ||
{"google.com", false}, | ||
{"https://www.google.cOM", true}, | ||
{"ht123tps://www.google.cOM", false}, | ||
{"https://golang.Org", true}, | ||
{"https://invalid#$%#[email protected]", false}, | ||
} | ||
for _, test := range tests { | ||
v := URLIsPresent{Name: "URL", Field: test.url} | ||
errors := validate.NewErrors() | ||
v.IsValid(errors) | ||
r.Equal(test.valid, !errors.HasAny()) | ||
r.Equal(test.valid, !errors.HasAny(), test.url, errors.Error()) | ||
} | ||
} |