Sha256: 37f499d0ffed2df7c98efb499f84adbd7d4353b52fb6265fab6c1f5315092a8a

Contents?: true

Size: 1.77 KB

Versions: 13

Compression:

Stored size: 1.77 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

          required(: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

          required(: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

      describe '#errors' do
        context 'with different locale' do
          before do
            I18n.locale = :pl
          end

          after do
            I18n.locale = :en
          end

          it 'contains the localized errors' do
            expect(schema.(email: '').errors).to eql(
              { email: ['Hej user! Dawaj ten email no!'] }
            )
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
dry-validation-0.13.3 spec/integration/localized_error_messages_spec.rb
dry-validation-0.13.2 spec/integration/localized_error_messages_spec.rb
dry-validation-0.13.1 spec/integration/localized_error_messages_spec.rb
dry-validation-0.11.2 spec/integration/localized_error_messages_spec.rb
dry-validation-0.12.3 spec/integration/localized_error_messages_spec.rb
dry-validation-0.13.0 spec/integration/localized_error_messages_spec.rb
dry-validation-0.12.2 spec/integration/localized_error_messages_spec.rb
dry-validation-0.12.1 spec/integration/localized_error_messages_spec.rb
dry-validation-0.12.0 spec/integration/localized_error_messages_spec.rb
dry-validation-0.11.1 spec/integration/localized_error_messages_spec.rb
dry-validation-0.11.0 spec/integration/localized_error_messages_spec.rb
dry-validation-0.10.7 spec/integration/localized_error_messages_spec.rb
dry-validation-0.10.6 spec/integration/localized_error_messages_spec.rb