internetee/registry

View on GitHub
app/validators/contact/ident/national_id_validator.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
class Contact::Ident::NationalIdValidator < ActiveModel::EachValidator
  def self.country_specific_validations
    {
      Country.new('EE') => proc { |code| Isikukood.new(code).valid? },
    }
  end

  def validate_each(record, attribute, value)
    validation = validation_for(record.country)

    return unless validation

    valid = validation.call(value)
    record.errors.add(attribute, :invalid_national_id, country: record.country) unless valid
  end

  private

  def validation_for(country)
    self.class.country_specific_validations[country]
  end
end