Sha256: 568373adb72092ef919d953d8c4cdee36901720cc67896fd961701b77cca90b1

Contents?: true

Size: 1.03 KB

Versions: 19

Compression:

Stored size: 1.03 KB

Contents

require 'whyvalidationssuckin96/skippable_validation'
require 'whyvalidationssuckin96/attribute_based_validation'
require 'uri'

module WhyValidationsSuckIn96
  
  # Checks whether a given attribute is a valid URL
  #
  # @example Default usage
  #   setup_validations do
  #     validates_as_url :website
  #   end
  #
  # @example Specifying valid schemes instead of the defaults
  #   setup_validations do
  #     validates_as_url :website, :schemes => %w[ldap mailto]
  #   end
  class ValidatesUrl < Validation  
    include WhyValidationsSuckIn96::SkippableValidation
    include WhyValidationsSuckIn96::AttributeBasedValidation
    
    DefaultOptions = {:message => "is not a valid URL", :schemes => %w[http https]}
    
    def validate
      super
      uri = URI.parse(attribute_value)
      options[:schemes].include?(uri.scheme) ? pass : fail
    rescue URI::InvalidURIError => e
      fail
    end

  end # Validation

  ValidationBuilder.register_macro :validates_as_url, WhyValidationsSuckIn96::ValidatesUrl
end   # WhyValidationsSuckIn96

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
whyvalidationssuckin96-1.6.4 lib/whyvalidationssuckin96/macros/validates_url.rb
whyvalidationssuckin96-1.6.3 lib/whyvalidationssuckin96/macros/validates_url.rb
whyvalidationssuckin96-1.6.2 lib/whyvalidationssuckin96/macros/validates_url.rb
whyvalidationssuckin96-1.6.1 lib/whyvalidationssuckin96/macros/validates_url.rb
whyvalidationssuckin96-1.6.0 lib/whyvalidationssuckin96/macros/validates_url.rb
whyvalidationssuckin96-1.5.5 lib/whyvalidationssuckin96/macros/validates_url.rb
whyvalidationssuckin96-1.5.4 lib/whyvalidationssuckin96/macros/validates_url.rb
whyvalidationssuckin96-1.5.3 lib/whyvalidationssuckin96/macros/validates_url.rb
whyvalidationssuckin96-1.5.2 lib/whyvalidationssuckin96/macros/validates_url.rb
whyvalidationssuckin96-1.5.1 lib/whyvalidationssuckin96/macros/validates_url.rb
whyvalidationssuckin96-1.5.0 lib/whyvalidationssuckin96/macros/validates_url.rb
whyvalidationssuckin96-1.4.0 lib/whyvalidationssuckin96/macros/validates_url.rb
whyvalidationssuckin96-1.3.1 lib/whyvalidationssuckin96/macros/validates_url.rb
whyvalidationssuckin96-1.3.0 lib/whyvalidationssuckin96/macros/validates_url.rb
whyvalidationssuckin96-1.2.3 lib/whyvalidationssuckin96/macros/validates_url.rb
whyvalidationssuckin96-1.2.2 lib/whyvalidationssuckin96/macros/validates_url.rb
whyvalidationssuckin96-1.2.1 lib/whyvalidationssuckin96/macros/validates_url.rb
whyvalidationssuckin96-1.2.0 lib/whyvalidationssuckin96/macros/validates_url.rb
whyvalidationssuckin96-1.1.0 lib/whyvalidationssuckin96/macros/validates_url.rb