Class: UriValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/validates_as_uri/uri_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/validates_as_uri/uri_validator.rb', line 2

def validate_each(record, attribute, value)
  protocols = options[:protocols] || %w(http https)
  
  if value !~ %r{^(#{Array.wrap(protocols).join('|')})://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$}
    record.errors.add(attribute, options[:message] || :invalid_uri)
  end
end