1

I want to validate a URL in Rails using URI.Parse, by saying that the URL is valid as long as URI.parse does not raise URI::InvalidURIError. Is that a good idea? What URLs would pass this validation test?

1 Answer 1

3

Yes, it's a good idea if you plan to use those URLs in your app.

URI will prove the string is parseable into these parts: scheme, userinfo, host, port, registry, path, opaque, query, fragment.

URI handles these schemes:

  • FTP, HTTP, HTTPS, LDAP, LDAPS, or MailTo
  • or URI::Generic

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/uri/rdoc/URI/Parser.html#method-i-parse

If you have other schemes, you can handle them yourself after the parse.

URI.parse works by calling URI.split, which uses two regular expressions:

  • URI::ABS_URI for absolute URIs
  • URI::REL_URI for relative URIs

You can look at these to see how they match. You can alter them too if you like.

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

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.