Class: ActiveModel::Validations::UrlValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/valid_url.rb

Constant Summary collapse

PROTOCOLS =
domain["protocols"]
ZONES =
domain["zones"]

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/valid_url.rb', line 13

def validate_each(record, attribute, value)
  begin
    url = ensure_protocol(value)
    uri = Addressable::URI.parse(url)
  rescue
    invalid = true
  end

  unless !invalid && valid_scheme?(uri.scheme) && valid_host?(uri.host) && valid_path?(uri.path)
    record.errors[attribute] << (options[:message] || "is an invalid URL")
  end
end