Class: ActiveModel::Validations::UrlValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/can_has_validations/validators/url_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/can_has_validations/validators/url_validator.rb', line 9

def validate_each(record, attribute, value)
  allowed_schemes = Array.wrap(options[:scheme] || %w(http https))
  
  if defined?(Addressable::URI)
    u = Addressable::URI.parse(value) rescue nil
    u2 = u && URI.parse(u.normalize.to_s) rescue nil
  else
    u2 = u = URI.parse(value) rescue nil
  end
  if !u || !u2 || u.relative? || allowed_schemes.exclude?(u.scheme)
    record.errors.add(attribute, :invalid_url, options)
  end
end