Skip to content

Commit

Permalink
Merge branch 'master' of github.com:markbates/validate
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Sep 27, 2017
2 parents 76eb7e8 + b4702c9 commit ce7531e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
24 changes: 24 additions & 0 deletions validators/strings_match.go
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)
}
}
45 changes: 45 additions & 0 deletions validators/strings_match_test.go
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)
}
}
11 changes: 7 additions & 4 deletions validators/url_is_present.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ type URLIsPresent struct {
// uses net/url ParseRequestURI to check validity
func (v *URLIsPresent) IsValid(errors *validate.Errors) {
if v.Field == "http://" || v.Field == "https://" {
v.Message = fmt.Sprintf("%s url is empty",
v.Name)
v.Message = fmt.Sprintf("%s url is empty", v.Name)
errors.Add(GenerateKey(v.Name), v.Message)
}
_, err := url.ParseRequestURI(v.Field)
parsedUrl, err := url.ParseRequestURI(v.Field)
if err != nil {
if v.Message == "" {
v.Message = fmt.Sprintf("%s does not match url format. Err: %s", v.Name,
err)
}
errors.Add(GenerateKey(v.Name), v.Message)
} else {
if parsedUrl.Scheme != "" && parsedUrl.Scheme != "http" && parsedUrl.Scheme != "https" {
v.Message = fmt.Sprintf("%s invalid url scheme", v.Name)
errors.Add(GenerateKey(v.Name), v.Message)
}
}

}
4 changes: 3 additions & 1 deletion validators/url_is_present_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

0 comments on commit ce7531e

Please sign in to comment.