Class: ActiveModel::Validations::UrlValidator
- Inherits:
-
EachValidator
- Object
- EachValidator
- ActiveModel::Validations::UrlValidator
- Defined in:
- lib/active_validators/active_model/validations/url_validator.rb
Overview
Public: Uses ‘URI.regexp` to validate URLs, by default only allows the http and https protocols.
Examples
validates :url, :url => true
# => only allow http, https
validates :url, :url => %w{http https ftp ftps}
# => allow http, https, ftp and ftps
Instance Method Summary collapse
-
#as_uri(value) ⇒ Object
Internal: Tries to convert supplied string into URI, if not possible returns nil => invalid URI.
-
#check_validity! ⇒ Object
Internal: Ensures that at least one protocol is defined.
-
#initialize(options) ⇒ UrlValidator
constructor
Public: Creates a new instance, overrides ‘:protocols` if either `:with` or `:in` are defined, to allow for shortcut setters.
-
#protocols ⇒ Object
Internal: Returns an array of protocols to use with the URI regexp.
-
#uri_regexp ⇒ Object
Internal: Constructs the regular expression to check the URI for the configured protocols.
-
#validate_each(record, attribute, value) ⇒ Object
Public: Validate URL, if it fails adds an error.
Constructor Details
#initialize(options) ⇒ UrlValidator
Public: Creates a new instance, overrides ‘:protocols` if either `:with` or `:in` are defined, to allow for shortcut setters.
Examples:
validates :url, :url => { :protocols => %w{http https ftp} }
# => accepts http, https and ftp URLs
validates :url, :url => 'https'
# => accepts only https URLs (shortcut form of above)
validates :url, :url => true
# => by default allows only http and https
Raises an ArgumentError if the array with the allowed protocols is empty.
Returns a new instance.
38 39 40 41 |
# File 'lib/active_validators/active_model/validations/url_validator.rb', line 38 def initialize() [:protocols] ||= .delete(:protocol) || .delete(:with) || .delete(:in) super end |
Instance Method Details
#as_uri(value) ⇒ Object
Internal: Tries to convert supplied string into URI, if not possible returns nil => invalid URI.
Returns the URI or nil.
73 74 75 |
# File 'lib/active_validators/active_model/validations/url_validator.rb', line 73 def as_uri(value) URI.parse(value.to_s) rescue nil if value.present? end |
#check_validity! ⇒ Object
Internal: Ensures that at least one protocol is defined. If the protocols are empty it throws an ArgumentError.
Raises ArgumentError if protocols is empty.
Returns nothing.
49 50 51 |
# File 'lib/active_validators/active_model/validations/url_validator.rb', line 49 def check_validity! raise ArgumentError, "At least one URI protocol is required" if protocols.empty? end |
#protocols ⇒ Object
Internal: Returns an array of protocols to use with the URI regexp. The default protocols are ‘http` and `https`.
Returns the Array with the allowed protocols.
57 58 59 |
# File 'lib/active_validators/active_model/validations/url_validator.rb', line 57 def protocols Array.wrap([:protocols] || %w{http https}) end |
#uri_regexp ⇒ Object
Internal: Constructs the regular expression to check the URI for the configured protocols.
Returns the Regexp.
65 66 67 |
# File 'lib/active_validators/active_model/validations/url_validator.rb', line 65 def uri_regexp @uri_regexp ||= /\A#{URI::Parser.new.make_regexp(protocols)}\z/ end |
#validate_each(record, attribute, value) ⇒ Object
Public: Validate URL, if it fails adds an error.
Returns nothing.
80 81 82 83 |
# File 'lib/active_validators/active_model/validations/url_validator.rb', line 80 def validate_each(record, attribute, value) uri = as_uri(value) record.errors.add(attribute) unless uri && value.to_s =~ uri_regexp end |