useful
useful copied to clipboard
is_valid_url?
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
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
. 😉
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
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.
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.
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)
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. 🙃
The postman brings a letter to the correct address and finds that the house has been demolished. Yes 😏
The act of delivering the letter is what triggers the destruction ... 🤯 😜
Who was (!) the president?