From 93bb347253bc96ae9f96dfc6248b40cbcd8f1de2 Mon Sep 17 00:00:00 2001 From: joeybloggs Date: Tue, 23 Feb 2016 15:25:49 -0500 Subject: [PATCH] Update validations for "url" and "uri" because of Go 1.6 changes added checks for blank and fragment/suffix because of Go 1.6 change https://github.com/golang/go/commit/617c93ce740c3c3cc28cdd1a0d712be183d0b328#diff-6c2d018290e298803c0c9419d8739885L195 fix was to emulate browser and strip the '#' suffix prior to validation. see #237 --- baked_in.go | 31 +++++++++++++++++++++++++++---- validator_test.go | 1 + 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/baked_in.go b/baked_in.go index 2d60162..4871456 100644 --- a/baked_in.go +++ b/baked_in.go @@ -745,7 +745,20 @@ func IsURI(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Va switch fieldKind { case reflect.String: - _, err := url.ParseRequestURI(field.String()) + + s := field.String() + + // checks needed as of Go 1.6 becuase of change https://github.com/golang/go/commit/617c93ce740c3c3cc28cdd1a0d712be183d0b328#diff-6c2d018290e298803c0c9419d8739885L195 + // emulate browser and strip the '#' suffix prior to validation. see issue-#237 + if i := strings.Index(s, "#"); i > -1 { + s = s[:i] + } + + if s == blank { + return false + } + + _, err := url.ParseRequestURI(s) return err == nil } @@ -760,13 +773,23 @@ func IsURL(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Va switch fieldKind { case reflect.String: - url, err := url.ParseRequestURI(field.String()) - if err != nil { + var i int + s := field.String() + + // checks needed as of Go 1.6 becuase of change https://github.com/golang/go/commit/617c93ce740c3c3cc28cdd1a0d712be183d0b328#diff-6c2d018290e298803c0c9419d8739885L195 + // emulate browser and strip the '#' suffix prior to validation. see issue-#237 + if i = strings.Index(s, "#"); i > -1 { + s = s[:i] + } + + if s == blank { return false } - if url.Scheme == blank { + url, err := url.ParseRequestURI(s) + + if err != nil || url.Scheme == blank { return false } diff --git a/validator_test.go b/validator_test.go index bc932a0..d4a8745 100644 --- a/validator_test.go +++ b/validator_test.go @@ -4977,6 +4977,7 @@ func TestUrl(t *testing.T) { {"rtmp://foobar.com", true}, {"http://www.foo_bar.com/", true}, {"http://localhost:3000/", true}, + {"http://foobar.com/#baz", true}, {"http://foobar.com#baz=qux", true}, {"http://foobar.com/t$-_.+!*\\'(),", true}, {"http://www.foobar.com/~foobar", true},