Sha256: de48560406a24706c0d7f12f8448cb1def83363c7cc8ece61868131b2613c183

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

require 'dry/validation/messages/i18n'

RSpec.describe Dry::Validation, 'with localized messages' do
  before do
    I18n.config.available_locales_set << :pl
    I18n.load_path.concat(%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
      subject(:schema) do
        Dry::Validation.Schema do
          configure do
            config.messages = :i18n
          end

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

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

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

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dry-validation-0.7.4 spec/integration/localized_error_messages_spec.rb
dry-validation-0.7.3 spec/integration/localized_error_messages_spec.rb
dry-validation-0.7.2 spec/integration/localized_error_messages_spec.rb
dry-validation-0.7.1 spec/integration/localized_error_messages_spec.rb
dry-validation-0.7.0 spec/integration/localized_error_messages_spec.rb