Reputation: 3558
After a bunch of research and trying different gems I ended up rolling with the following:
validates :url,
presence: true,
length: { maximum: 245 },
format: { with: URI.regexp(%w(http https)), message: :bad_url_format }
This correctly validates the http:// and https:// prefixes but it allows any type of domain extension i.e. http://go.forever.
Is this considered normal?
Upvotes: 1
Views: 696
Reputation: 660
If you really wanted to validate the domain extension, I would do something along the lines of this.
Using a custom validation
validate :correct_url_extension
EXTENSIONS = %w(com org gov blah foo blah bar)
def correct_url_extension
ext = url.split(".")[-1] #extension should always be at the end (parse here)
return EXTENSIONS.include?(ext) ? true : false
end
Upvotes: 1
Reputation: 7532
Yes, that's normal for URL validations. Given the recent expansion of top-level domains to over one hundred, and the likelihood of that list to grow, it would be pretty cumbersome and brittle to try and enforce a list of allowed TLDs.
Upvotes: 1