summaryrefslogtreecommitdiff
path: root/app/validators/url_placeholder_validator.rb
blob: dd681218b6bfc61a80047b287e05a460f05d366a (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
# 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.
#
# Also, this validator can help you validate urls with placeholders inside.
# Usually, if you have a url like 'http://www.example.com/%{project_path}' the
# URI parser will reject that URL format. Provide a `:placeholder_regex` option
# to configure accepted placeholders.
#
# 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) }
#
#     validates :placeholder_url, url: { placeholder_regex: /(project_path|project_id|default_branch)/ }
#   end
#
class UrlPlaceholderValidator < UrlValidator
  def validate_each(record, attribute, value)
    placeholder_regex = self.options[:placeholder_regex]
    value = value.gsub(/%{#{placeholder_regex}}/, 'foo') if placeholder_regex && value

    super(record, attribute, value)
  end
end