summaryrefslogtreecommitdiff
path: root/app/validators/url_validator.rb
blob: a77beb2683dd88a94dfe89c04923ba37c8bb4d01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# UrlValidator
#
# Custom validator for URLs.
#
# By default, only URLs for the HTTP(S) protocols will be considered valid.
# Provide a `:protocols` option to configure accepted protocols.
#
# Example:
#
#   class User < ActiveRecord::Base
#     validates :personal_url, url: true
#
#     validates :ftp_url, url: { protocols: %w(ftp) }
#
#     validates :git_url, url: { protocols: %w(http https ssh git) }
#   end
#
class UrlValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    unless valid_url?(value)
      record.errors.add(attribute, "must be a valid URL")
    end
  end

  private

  def default_options
    @default_options ||= { protocols: %w(http https) }
  end

  def valid_url?(value)
    return false if value.nil?

    options = default_options.merge(self.options)

    value.strip!
    value =~ /\A#{URI.regexp(options[:protocols])}\z/
  end
end