fields
fields copied to clipboard
URL with without https:// fails
We are using Fields.url
in a project and a user is attempting to insert a url:
www.example.com
To the user, this looks like a perfectly valid URL, sadly our regex in #16 rejects a url without a protocol.
Question:
Do we want to allow urls without a protocol (i.e. be user-friendly) or do we want to be strict and continue rejecting them?
Relevant test: https://github.com/dwyl/fields/blob/a40df5c9856bbf21fdd31f16c724fc8a7201fa53/test/url_test.exs#L21
Sadly only 56.6% of websites use HTTPS: https://w3techs.com/technologies/details/ce-httpsdefault
However most of the top sites use HTTPS: https://transparencyreport.google.com/https/overview
The last thing we want is a person being blocked from adding a link to the @dwyl app because it does not start with https://
... so think we may need to update the regular expression to be more permissive.
@nelsonic
Bumped into this. Maybe something like below if this is still relevant?
> Enum.any?([:scheme, :authority, :host], &Map.get(URI.parse(url), &1))
> u = URI.parse("www.test.com")
%URI{
scheme: nil,
userinfo: nil,
host: nil,
port: nil,
path: "://www.test.com",
query: nil,
fragment: nil
}
> Enum.any?([:scheme, :authority, :host], &Map.get(u,&1))
false
##
>u = URI.parse("://www.test.com")
%URI{
scheme: nil,
userinfo: nil,
host: nil,
port: nil,
path: "www.test.com",
query: nil,
fragment: nil
}
>Enum.any?([:scheme, :authority, :host], &Map.get(u,&1))
false
##
>u = URI.parse("ftp://www.test.com")
%URI{
scheme: "ftp",
authority: "www.test.com",
userinfo: nil,
host: "www.test.com",
port: 21,
path: nil,
query: nil,
fragment: nil
}
>Enum.any?([:scheme, :authority, :host], &Map.get(u,&1))
true
> u = URI.parse("https://www.test.com?p=1")
%URI{
scheme: "https",
authority: "www.test.com",
userinfo: nil,
host: "www.test.com",
port: 443,
path: nil,
query: "p=1",
fragment: nil
}
> Enum.any?([:scheme, :authority, :host], &Map.get(u,&1))
true