Skip to content

Commit

Permalink
Added url and uri checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
abice committed Jun 27, 2017
1 parent 6b44c0f commit 8f04c59
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 1 deletion.
23 changes: 23 additions & 0 deletions generator/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions internal/benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ var benchmarks = []struct {
UUID: uuid.NewV4().String(),
MinInt: 12345,
MaxInt: 12345,
URL: "http://test.com/health?whatislife=something",
URI: "http://test.com/health?whatislife=something",
Dive: &SingleString{Entry: "This is a test"},
}},
{"TestAll Fail", true, TestAll{
Expand All @@ -170,6 +172,8 @@ var benchmarks = []struct {
UUID: strings.ToUpper(uuid.NewV4().String() + "adsf"),
MinInt: 12344,
MaxInt: 12346,
URL: "/health?whatislife=something",
URI: "?whatislife=something",
Dive: &SingleString{Entry: ""},
}},
}
Expand Down
2 changes: 2 additions & 0 deletions internal/benchmark/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ type TestAll struct {
MinInt int64 `valid:"min=12345" validate:"min=12345"`
MaxInt int64 `valid:"max=12345" validate:"max=12345"`
Dive *SingleString `valid:"required,dive" validate:"dive"` // Added required flag to have the same logic as playground
URL string `valid:"url" validate:"url"`
URI string `valid:"uri" validate:"uri"`
}
23 changes: 23 additions & 0 deletions internal/benchmark/types_validators.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions internal/example/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"time"

"github.com/abice/gencheck"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

Expand Down Expand Up @@ -174,6 +176,8 @@ func (s *ExampleTestSuite) TestValidateTestStruct_Values() {
CIDR: "0.0.0.0/24",
CIDRv4: "0.0.0.0/24",
CIDRv6: "2620:0:2d0:200::7/32",
URL: "http://test.com",
URI: "scp://test.com/123",
}

err := underTest.Validate()
Expand Down Expand Up @@ -224,6 +228,8 @@ func (s *ExampleTestSuite) TestValidateTestStruct_MinPtrFailure() {
NeMultiple: []string{"", "", "", "", "", "", ""},
NeNumber: 2.33,
NeString: "3",
URL: "http://test.com",
URI: "scp://test.com",
}

err := underTest.Validate()
Expand Down Expand Up @@ -302,6 +308,8 @@ func (s *ExampleTestSuite) TestValidateTestStruct_LteTime() {
NeMultiple: []string{"", "", "", "", "", "", ""},
NeNumber: 2.33,
NeString: "3",
URL: "http://test.com",
URI: "scp://test.com",
}

err := underTest.Validate()
Expand All @@ -313,3 +321,58 @@ func (s *ExampleTestSuite) TestValidateTestStruct_LteTime() {

s.Require().EqualValues(ve[0], gencheck.NewFieldError("Test", "LteTime", "lte", fmt.Errorf("is after now")), "Error should be lte time error")
}

// TestValidateTestStruct_Values
func TestValidateTestStruct_Individual(t *testing.T) {
tests := map[string]struct {
uut Test
field string
expected gencheck.FieldError
}{
"URL": {
field: "URL",
expected: gencheck.NewFieldError("Test", "URL", "url", errors.New("parse x: invalid URI for request")),
uut: Test{
RequiredString: "x",
URL: "x",
},
},
"URLNoScheme": {
field: "URL",
expected: gencheck.NewFieldError("Test", "URL", "url", errors.New("URL is missing a scheme")),
uut: Test{
RequiredString: "x",
URL: "/x/123",
},
},
"URI": {
field: "URI",
expected: gencheck.NewFieldError("Test", "URI", "uri", errors.New("parse x: invalid URI for request")),
uut: Test{
RequiredString: "x",
URI: "x",
},
},
}

for name, test := range tests {
t.Run(name, func(tt *testing.T) {
err := test.uut.Validate()
assert.Error(tt, err, "Valid Struct should have had an error")

assert.IsType(tt, gencheck.ValidationErrors{}, err, "Error returned was not ValidationErrors type")

ve := err.(gencheck.ValidationErrors)
found := false

for _, e := range ve {
if e.Field() == test.field {
found = true
assert.EqualValues(tt, e, test.expected, "Error did not match expected")
}
}
assert.True(tt, found, `Did not find expected error for field '%s'`, test.field)
})
}

}
25 changes: 24 additions & 1 deletion internal/example/example_validators.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions template/url.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{{define "uri"}}
{{ if eq .FieldType "string" "*string" -}}
{{if (isPtr . )}}if s.{{.FieldName}} != nil { {{end -}}
if {{if (isPtr . )}}*{{end}}s.{{.FieldName}} != "" {
_, {{.FieldName}}urierr := url.ParseRequestURI({{if (isPtr . )}}*{{end}}s.{{.FieldName}})
if {{.FieldName}}urierr != nil {
{{ addError . (printf "%surierr" .FieldName) }}
}
}
{{if (isPtr . )}} } {{end -}}
{{ else if eq .FieldType "[]string" "[]*string" -}}
for index, single{{.FieldName}} := range s.{{.FieldName}} {
_, err := url.ParseRequestURI({{if eq .FieldType "[]string"}}&{{end}}single{{.FieldName}})
if err != nil {
{{ addError . (printf `fmt.Errorf("%%s[%%d] - %%s", index, single%s, err)` .FieldName) }}
}
}
{{ else }}
{{ generationError (printf "uri is not valid on field '%s %s'" .FieldName .FieldType) }}
{{end}}
{{- end -}}


{{define "url"}}
{{ if eq .FieldType "string" "*string" -}}
{{if (isPtr . )}}if s.{{.FieldName}} != nil { {{end -}}
if {{if (isPtr . )}}*{{end}}s.{{.FieldName}} != "" {
{{.FieldName}}URL, {{.FieldName}}urlerr := url.ParseRequestURI({{if (isPtr . )}}*{{end}}s.{{.FieldName}})
if {{.FieldName}}urlerr != nil {
{{ addError . (printf "%surlerr" .FieldName) }}
} else if {{.FieldName}}URL.Scheme == "" {
{{ addError . (printf `errors.New("%s is missing a scheme")` .FieldName) }}
}
}
{{if (isPtr . )}} } {{end -}}
{{ else if eq .FieldType "[]string" "[]*string" -}}
for index, single{{.FieldName}} := range s.{{.FieldName}} {
u, err := url.ParseRequestURI({{if eq .FieldType "[]string"}}&{{end}}single{{.FieldName}})
if err != nil {
{{ addError . (printf `fmt.Errorf("%%s[%%d] - %%s", index, single%s, err)` .FieldName) }}
} else if u.Scheme == "" {
{{ addError . (printf `fmt.Errorf("%%s[%%d] - missing scheme", index, single%s)` .FieldName) }}
}
}
{{ else }}
{{ generationError (printf "url is not valid on field '%s %s'" .FieldName .FieldType) }}
{{end}}
{{- end -}}

0 comments on commit 8f04c59

Please sign in to comment.