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 617c93ce74 (diff-6c2d018290e298803c0c9419d8739885L195)
fix was to emulate browser and strip the '#' suffix prior to validation. see #237
pull/238/head
joeybloggs 8 years ago
parent 1214d3629c
commit 93bb347253
  1. 31
      baked_in.go
  2. 1
      validator_test.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
}

@ -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},

Loading…
Cancel
Save