Sha256: 7630b40850e0d04a5b89e74434fba481a7dd01cd7b4e1002e91aa08a954bb926

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

RSpec.describe Dry::Validation, 'with localized messages' do
  subject(:validation) { schema.new }

  before do
    I18n.config.available_locales_set << :pl
    I18n.load_path = %w(en pl).map { |l| SPEC_ROOT.join("fixtures/locales/#{l}.yml") }
    I18n.backend.load_translations
  end

  describe 'defining schema' do
    context 'without a namespace' do
      let(:schema) do
        Class.new(Dry::Validation::Schema) do
          configure do |config|
            config.messages = :i18n
          end

          key(:email) { |email| email.filled? }
        end
      end

      describe '#messages' do
        it 'returns localized error messages' do
          expect(validation.(email: '').messages(locale: :pl)).to match_array([
            [:email, [['Proszę podać adres email', '']]]
          ])
        end
      end
    end

    context 'with a namespace' do
      let(:schema) do
        Class.new(Dry::Validation::Schema) do
          configure do |config|
            config.messages = :i18n
            config.namespace = :user
          end

          key(:email) { |email| email.filled? }
        end
      end

      describe '#messages' do
        it 'returns localized error messages' do
          expect(validation.(email: '').messages(locale: :pl)).to match_array([
            [:email, [['Hej user! Dawaj ten email no!', '']]]
          ])
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-validation-0.3.0 spec/integration/localized_error_messages_spec.rb