Skip to content
URL Validation for Rails
Branch: master
Clone or download
Latest commit 535d97b May 16, 2019

Validates URL

This gem adds the capability of validating URLs to ActiveRecord and ActiveModel.


# add this to your Gemfile
gem "validate_url"

# or run
sudo gem install validate_url


With ActiveRecord

class Pony < ActiveRecord::Base
  # standard validation
  validates :homepage, url: true

  # with allow_nil
  validates :homepage, url: { allow_nil: true }

  # with allow_blank
  validates :homepage, url: { allow_blank: true }

  # without local hostnames
  validates :homepage, url: { no_local: true }

  # with custom schemes
  validates :homepage, url: { schemes: ['https'] }

  # with public suffix database
  validates :homepage, url: { public_suffix: true }

With ActiveModel

class Unicorn
  include ActiveModel::Validations

  attr_accessor :homepage

  # with legacy syntax (the syntax above works also)
  validates_url :homepage, allow_blank: true

With RSpec

Require the matcher in spec_helper.rb or rails_helper.rb:

require 'validate_url/rspec_matcher'

In your spec:

describe Unicorn
  it { validate_url_of(:homepage) }


The default error message is not a valid URL. You can pass the message: "my custom error" option to your validation to define your own, custom message.


Big thanks to Tanel Suurhans, Tarmo Lehtpuu, Steve Smith and all the contributors! We appreciate all your work on new features and bugfixes.


Validates URL is created and maintained by PerfectLine, LLC.


Validates URL is Copyright © 2010-2014 PerfectLine, LLC. It is free software, and may be redistributed under the terms specified in the LICENSE file.

How to push new version

rake version:bump:patch
rake gemspec

Fix validate_url.gemspec to remove unneeded wrong strings

gem build validate_url.gemspec
gem push validate_url-1.0.8.gem
You can’t perform that action at this time.