perfectline/validates_url

Allow "mailto:" urls

gravitystorm opened this issue · 2 comments

I've been trying to figure out why mailto: urls are rejected by this gem. mailto: urls are slightly unusual since they only consist of a scheme and an opaque part (to use the RFC 2396 / RFC 3986 terminology).

> URI.parse('mailto:foo@example.com')
=> #<URI::MailTo mailto:foo@example.com>
> URI.split('mailto:foo@example.com')
=> ["mailto", nil, nil, nil, nil, nil, "foo@example.com", nil, nil]
> 'mailto:foo@example.com' =~ /\A#{URI::regexp(['mailto'])}\z/
=> 0
> URI.parse('mailto:foo@example.com').host
=> nil

Note that the host is nil, but the url is otherwise valid.

Our application considers http, https and mailto to all be valid for one particular attribute, so I'd like to use validates_url like this:

validates :url, :url => { :allow_nil => true, :schemes => %w[http https mailto] }

I think I've tracked it down to this line in the code:

valid_scheme = host && scheme && schemes.include?(scheme)

This requires the host to be not nil, and was introduced in #18 .

I'm not sure what the right approach would be here to permit mailto: urls (and other schemes?) that don't require a host.

it's better to write own validator in this case

Thanks @kritik for the quick response! It's not the answer that I was hoping for 😄 but I understand your position.