andycandrea/hnews — pull request #8 by andycandrea (2014-06-19 21:00:01+00:00)
Title: Improve front end, add additional validation and tests.

This branch got away from me a bit and thus includes an improved (read: existent) design as well as validation to ensure that URIs are properly formed and that an article has exactly one of the URL and content fields filled to match Hacker News (questions vs. articles) and Reddit (self posts vs regular posts).

To allow for URIs that don't specify a protocol, I checked them against both URI::REGEXP and a custom regular expression. Since my custom regex isn't nearly as robust as the one used by URI::REGEXP, I purposefully avoided validating anything starting with http:// or https:// to ensure that they'll be check against a stronger pattern, meaning that if there were a URI starting with http:// that were not validated by URI::REGEXP, my regex would not accidentally allow it through. That said, it is conceivable that some poorly formed URIs may get past my regex (so any suggestions for improvement are welcome), but I felt that having the ability to submit a link like `google.com` would be worth it.

For the cases of URLs with no specified scheme that pass validation, I prepend `http://` before saving to the database to allow for easy linking later, as not doing so led to the creation of relative, rather than full, links.