Sha256: fb48a7808a5e0b74440c538d759ef2db9b7ef9e334dfe1bc9329e59cec1147fc

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

RSpec.describe Schema, 'using high-level grouped rules' do
  subject(:validate) { schema.new }

  let(:schema) do
    Class.new(Schema) do
      def self.messages
        Messages.default.merge(
          en: {
            errors: {
              email: {
                absence: 'email must not be selected',
                presence: 'email must be selected',
                format: 'this is not an email lol',
                inclusion: 'sorry, did not expect this lol'
              }
            }
          }
        )
      end

      key(:email) { |email| email.none? | email.filled? }
      key(:login) { |login| login.bool? }

      rule(email: :absence) do
        value(:login).false? > value(:email).none?
      end

      rule(email: :presence) do
        value(:login).true? > value(:email).filled?
      end

      rule(email: :format) do
        value(:email).filled? > value(:email).format?(/[a-z]@[a-z]/)
      end

      rule(email: :inclusion) do
        value(:email).filled? > value(:email).inclusion?(%w[jane@doe])
      end
    end
  end

  it 'passes when login is true and email is present' do
    expect(validate.(login: true, email: 'jane@doe').messages).to be_empty
  end

  it 'fails when login is false and email is not present' do
    expect(validate.(login: true, email: nil).messages).to_not be_empty
  end

  it 'provides merged error messages' do
    expect(validate.(login: true, email: 'not-an-email-lol').messages).to eql(
      email: [
        ["sorry, did not expect this lol", "this is not an email lol"], nil
      ]
    )
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-validation-0.6.0 spec/integration/schema/grouped_rules_spec.rb