From 9685357daf9f93b306087ed2f4986012d9330fad Mon Sep 17 00:00:00 2001 From: Yusuf Efe <120668197+yuefdev@users.noreply.github.com> Date: Thu, 22 Oct 2026 05:31:04 +0300 Subject: [PATCH] fix(package): [validator] accept supported hostless URL schemes (#395) --- validator/validator.go | 4 ++-- validator/validator_test.go | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) 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