Sha256: a1d4824bce1f0fe139f47578751b7f77742de87c2b60c0aa4824d6af6c337cfd

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

require 'dry/validation/messages/i18n'

RSpec.describe Dry::Validation do
  shared_context 'schema with customized messages' do
    describe '#messages' do
      it 'returns compiled error messages' do
        expect(schema.(email: '', age: 12).messages).to eql(
          email: ['Please provide your email'], age: ['12 must be greater than 18']
        )

        expect(schema.(email: '', age: 14).messages).to eql(
          email: ['Please provide your email'], age: ['14 must be greater than 18']
        )
      end
    end
  end

  context 'yaml' do
    subject(:schema) do
      Dry::Validation.Schema do
        configure do
          config.messages_file = SPEC_ROOT.join('fixtures/locales/en.yml')
        end

        required(:email).filled
        required(:age).value(gt?: 18)
      end
    end

    include_context 'schema with customized messages'
  end

  context 'i18n' do
    context 'with custom messages set globally' do
      before do
        I18n.load_path << SPEC_ROOT.join('fixtures/locales/en.yml')
        I18n.backend.load_translations
      end

      subject(:schema) do
        Dry::Validation.Schema do
          configure do
            config.messages = :i18n
          end

          required(:email).filled
          required(:age).value(gt?: 18)
        end
      end

      include_context 'schema with customized messages'
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-validation-0.13.3 spec/integration/custom_error_messages_spec.rb