Skip to content

Instantly share code, notes, and snippets.

View jonstorer's full-sized avatar

Jonathon Storer jonstorer

View GitHub Profile
@leemeichin
leemeichin / url_validator.rb
Created July 20, 2012 15:02
Rails Custom URL Validator
class UrlValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors[attribute] << (options[:message] || "must be a valid URL") unless url_valid?(value)
end
# a URL may be technically well-formed but may not actually be
# valid, so this checks for both.
def url_valid?(url)
url = URI.parse(url) rescue false
url.kind_of?(URI::HTTP) || url.kind_of?(URI::HTTPS)