1

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.