Sha256: 4baa8722ddc6cdbcc8bf0e1fc2585593f0ed67a8662a9827e54cbc4d092dd1bd
Contents?: true
Size: 1.72 KB
Versions: 2
Compression:
Stored size: 1.72 KB
Contents
require 'addressable/uri' require 'active_model' module ActiveModel module Validations class UrlValidator < ActiveModel::EachValidator def initialize(options) options.reverse_merge!(:schemes => %w(http https)) options.reverse_merge!(:message => "is not a valid URL") super(options) end def validate_each(record, attribute, value) schemes = [*options.fetch(:schemes)].map(&:to_s) begin uri = Addressable::URI.parse(value) unless uri && schemes.include?(uri.scheme) record.errors.add(attribute, options.fetch(:message), :value => value) end rescue Addressable::URI::InvalidURIError record.errors.add(attribute, options.fetch(:message), :value => value) end end end module ClassMethods # Validates whether the value of the specified attribute is valid url. # # class Unicorn # include ActiveModel::Validations # attr_accessor :homepage, :ftpsite # validates_url :homepage, :allow_blank => true # validates_url :ftpsite, :schemes => ['ftp'] # end # Configuration options: # * <tt>:message</tt> - A custom error message (default is: "is not a valid URL"). # * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+). # * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+). # * <tt>:schemes</tt> - Array of URI schemes to validate against. (default is +['http', 'https']+) def validates_url(*attr_names) validates_with UrlValidator, _merge_attributes(attr_names) end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
validate_url-0.2.2 | lib/validate_url.rb |
validate_url-0.2.1 | lib/validate_url.rb |