diff --git a/validator/validator.go b/validator/validator.go index e72bdee..36cbc91 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -278,7 +278,7 @@ func IsUrl(str string) bool { } u, err := url.Parse(str) - if err != nil || u.Scheme == "" || u.Host == "" { + if err != nil || u.Scheme == "" { return false } @@ -298,7 +298,7 @@ func IsUrl(str string) bool { } if u.Scheme == "file" || u.Scheme == "mailto" || u.Scheme == "data" { - return true + return u.Host != "" || u.Path != "" || u.Opaque != "" } host := u.Hostname() diff --git a/validator/validator_test.go b/validator/validator_test.go index af78004..beef4c4 100644 --- a/validator/validator_test.go +++ b/validator/validator_test.go @@ -436,6 +436,11 @@ func TestIsUrl(t *testing.T) { t.Parallel() assert := internal.NewAssert(t, "TestIsUrl") + assert.Equal(true, IsUrl("mailto:user@example.com")) + assert.Equal(true, IsUrl("file:///tmp/file.txt")) + assert.Equal(true, IsUrl("data:text/plain,hello")) + assert.Equal(false, IsUrl("mailto:")) + assert.Equal(false, IsUrl("http:/path")) tests := []struct { input string