Raw Syntax

The stuff programs are made of

URL Validation in Rails 3 (and Ruby in General)


I've seen some approaches, most notably here on igvita. Though the reference on igvita is quite dated (2006 ??? being more or less the beginning of the epoch in rails-years), I've seen plenty of code using the validate-a-uri-by-regex approach.

It doesn't work and always ends up excluding valid urls. This is why I prefer to use the parsing approach. If the url can be parsed, and the scheme seems reasonably valid, good enough.

An Interesting URL Problem

Recently I ran into a situation where I needed to be able to validate and handle unicode / internationalized links. One such link would be the russian tld registrar кц.рф. The typical approach of parsing with URI fails below

The Solution

At this point, I happened to remember there is another library for parsing URIs. It's called Addressable::URI. Its aim is to be a full replacement of the ruby standard library's URI module.

More importantly, it successfully parses unicode urls.

Here's how I do URL validation in rails 3, using ActiveModel and Addressable::URI.