mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(package): [validator] accept supported hostless URL schemes (#395)
This commit is contained in:
1 parent
9a8b67ac85
commit
9685357daf
2 files changed
+7
-2
No files matched your search
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user