Class: ActiveModel::Validations::UrlValidator
- Inherits:
-
EachValidator
- Object
- EachValidator
- ActiveModel::Validations::UrlValidator
- Defined in:
- lib/validate_url.rb
Instance Method Summary collapse
-
#initialize(options) ⇒ UrlValidator
constructor
A new instance of UrlValidator.
- #validate_each(record, attribute, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ UrlValidator
Returns a new instance of UrlValidator.
8 9 10 11 12 |
# File 'lib/validate_url.rb', line 8 def initialize() .reverse_merge!(:schemes => %w(http https)) .reverse_merge!(:message => "is not a valid URL") super() end |
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/validate_url.rb', line 14 def validate_each(record, attribute, value) schemes = [*.fetch(:schemes)].map(&:to_s) if URI::regexp(schemes).match(value) begin URI.parse(value) rescue URI::InvalidURIError record.errors.add(attribute, .fetch(:message), :value => value) end else record.errors.add(attribute, .fetch(:message), :value => value) end end |