useful icon indicating copy to clipboard operation
useful copied to clipboard

is_valid_url?

Open ndrean opened this issue 1 year ago • 9 comments

I used this a few times:

def is_valid_url?(string) do
  [:scheme, :host, :port]
  |> Enum.map(&Map.get(URI.parse(string), &1))
  |> Enum.all?()
end
test "is_valid_url?/1" do
  assert is_valid_url?("http://www.google.com") == true
  assert is_valid_url?("http//google.com") == false
  assert s_valid_url?("ftp://google.com") == true
  assert is_valid_url?("https://google.com/api&url=ok") == true
  assert is_valid_url?("http://localhost:3000") == true
  assert is_valid_url?("https://localhost") == true
  assert is_valid_url?("htt:/google") == false
  assert is_valid_url?("www.google.com") == false
end

ndrean avatar Oct 08 '23 12:10 ndrean

Why is www.google.com not valid? 💭 You will often see urls represented without the scheme e.g. on billboards IRL. Or just google.com which everyone knows is not valid but is widely accepted as a URL ... Just curious what context you are using the is_valid_url? function to know.

BTW: Agree that it's useful. 😉

nelsonic avatar Nov 14 '23 09:11 nelsonic

Asking because in RFC 3986 allows the scheme to be optional. 💭

See Link.valid?/1 🔗 https://github.com/dwyl/link/blob/ca72089c0a56a0e1a96360ccba424b49b8165e3b/lib/link.ex#L118-L161

nelsonic avatar Nov 14 '23 10:11 nelsonic

I can't think of any other reason other than to enforce strictness. But if dwyl/link@ca72089/lib/link.ex#L118-L161 already does this, maybe #68 is useless.

LuchoTurtle avatar Nov 14 '23 10:11 LuchoTurtle

My one euro input. Suppose you have an API and an endpoint that takes an URL. If you don't put a constraint on the scheme, are you assured of have the redirection? www.google. to goggle is casi automatic by any website. However, I don't think the browser redirects from HTTP to HTTPS, but instead, it should be enabled by the website DNS rules.

ndrean avatar Nov 14 '23 13:11 ndrean

An example. You want to download a file and you have an endpoint that accepts URLs. If you don't specify the scheme, how do you know if it is ftp or http?

Another example: try in the browser:

fetch("google.com").then(console.log)
// and compare to
fetch("http://google.com").then(console.log)

ndrean avatar Nov 14 '23 17:11 ndrean

Good point. 💭 If the person supplied an ftp url it would be in the form: ftp://ftp.funet.fi/pub/standards/RFC/rfc959.txt ✅ But in the case that someone supplies a valid URL that doesn't match the correct scheme it will error, right? ❌ e.g: they supply https://google.com/file.txt the scheme is correct, the URL is valid, but there's no file. 🤷‍♂️ So the application has to handle that error scenario. 🙃

nelsonic avatar Nov 14 '23 20:11 nelsonic

The postman brings a letter to the correct address and finds that the house has been demolished. Yes 😏

ndrean avatar Nov 14 '23 21:11 ndrean

The act of delivering the letter is what triggers the destruction ... 🤯 😜

whitehouse-explode-independence-day

nelsonic avatar Nov 14 '23 21:11 nelsonic

Who was (!) the president?

ndrean avatar Nov 14 '23 22:11 ndrean