-
-
Save johnjohndoe/7022109 to your computer and use it in GitHub Desktop.
Rails UriValidator using the `adressable` gem, https://github.com/sporkmonger/addressable. Related Stackoverflow thread: http://stackoverflow.com/questions/7167895/whats-a-good-way-to-validate-links-urls-in-rails-3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'addressable/uri' | |
# Source: http://gist.github.com/bf4/5320847 | |
# Accepts options[:message] and options[:allowed_protocols] | |
# spec/validators/uri_validator_spec.rb | |
class UriValidator < ActiveModel::EachValidator | |
def validate_each(record, attribute, value) | |
uri = parse_uri(value) | |
if !uri | |
record.errors[attribute] << generic_failure_message | |
elsif !allowed_protocols.include?(uri.scheme) | |
record.errors[attribute] << "must begin with #{allowed_protocols_humanized}" | |
end | |
end | |
private | |
def generic_failure_message | |
options[:message] || "is an invalid URL" | |
end | |
def allowed_protocols_humanized | |
allowed_protocols.to_sentence(:two_words_connector => ' or ') | |
end | |
def allowed_protocols | |
@allowed_protocols ||= [(options[:allowed_protocols] || ['http', 'https'])].flatten | |
end | |
def parse_uri(value) | |
uri = Addressable::URI.parse(value) | |
uri.scheme && uri.host && uri | |
rescue URI::InvalidURIError, Addressable::URI::InvalidURIError, TypeError | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
# Source: http://gist.github.com/bf4/5320847 | |
# spec/validators/uri_validator_spec.rb | |
describe UriValidator do | |
subject do | |
Class.new do | |
include ActiveModel::Validations | |
attr_accessor :url | |
validates :url, uri: true | |
end.new | |
end | |
it "should be valid for a valid http url" do | |
subject.url = 'http://www.google.com' | |
subject.valid? | |
subject.errors.full_messages.should == [] | |
end | |
['http://google', 'http://.com', 'http://ftp://ftp.google.com','http://ssh://google.com'].each do |invalid_url| | |
it "#{invalid_url.inspect} is a invalid http url" do | |
subject.url = invalid_url | |
subject.valid? | |
subject.errors.full_messages.should == [] | |
end | |
end | |
['http:/www.google.com','<>hi'].each do |invalid_url| | |
it "#{invalid_url.inspect} is an invalid url" do | |
subject.url = invalid_url | |
subject.valid? | |
subject.errors.should have_key(:url) | |
subject.errors[:url].should include("is an invalid URL") | |
end | |
end | |
['www.google.com','google.com'].each do |invalid_url| | |
it "#{invalid_url.inspect} is an invalid url" do | |
subject.url = invalid_url | |
subject.valid? | |
subject.errors.should have_key(:url) | |
subject.errors[:url].should include("is an invalid URL") | |
end | |
end | |
['ftp://ftp.google.com','ssh://google.com'].each do |invalid_url| | |
it "#{invalid_url.inspect} is an invalid url" do | |
subject.url = invalid_url | |
subject.valid? | |
subject.errors.should have_key(:url) | |
subject.errors[:url].should include("must begin with http or https") | |
end | |
end | |
end |
see https://gist.github.com/adamico/2a781d6a4fc2ff577e7f for a Rails 4 with I18n messages
line 9 uri = parse_uri(value) if value
or you'll get
NoMethodError:
undefined method `scheme' for nil:NilClass
Update:
Probably makes more sense to edit parse_uri
def parse_uri(value)
return unless value
uri = Addressable::URI.parse(value)
uri.scheme && uri.host && uri
rescue URI::InvalidURIError, Addressable::URI::InvalidURIError, TypeError
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think the fifth line (comment) in
uri_validator.rb
should read