hummingbird-me/kitsu-server

View on GitHub
app/validators/country_code_validator.rb

Summary

Maintainability
A
1 hr
Test Coverage
A
100%
class CountryCodeValidator < ActiveModel::EachValidator
  def validate_each(record, attr, value)
    if value.respond_to?(:each)
      value.each do |v|
        error = validate_value(v)
        record.errors.add(attr, message: error) if error
      end
    else
      error = validate_value(value)
      record.errors.add(attr, message: error) if error
    end
  end

  def validate_value(value)
    nil if IsoCountryCodes.find(value).present?
  rescue IsoCountryCodes::UnknownCodeError
    "#{value} is not a known ISO 3166-1 country code"
  end
end