kaize/validates

View on GitHub
lib/uri_component_validator.rb

Summary

Maintainability
A
0 mins
Test Coverage
class UriComponentValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    unless value =~ URI::DEFAULT_PARSER.regexp[component]
      record.errors.add(attribute, :uri_component, options.merge(value: value))
    end
  end

  def check_validity!
    valid_components = URI::DEFAULT_PARSER.regexp.keys

    if valid_components.exclude? component
      raise ArgumentError, "Component must be of the following type: #{valid_components}"
    end
  end

  def component
    options[:component]
  end
end