Sha256: 7c5179b105dc3bf465fd3a4f3085ac186b495d8e54cc495dae8f21952776f4e3

Contents?: true

Size: 914 Bytes

Versions: 5

Compression:

Stored size: 914 Bytes

Contents

RSpec.describe 'Schema with xor rules' do
  subject(:schema) do
    Dry::Validation.Schema do
      configure do
        def self.messages
          Messages.default.merge(
            en: { errors: { be_reasonable: 'you cannot eat cake and have cake!' } }
          )
        end
      end

      key(:eat_cake).required

      key(:have_cake).required

      rule(:be_reasonable) do
        value(:eat_cake).eql?('yes!') ^ value(:have_cake).eql?('yes!')
      end
    end
  end

  describe '#messages' do
    it 'passes when only one option is selected' do
      messages = schema.(eat_cake: 'yes!', have_cake: 'no!').messages[:be_reasonable]

      expect(messages).to be(nil)
    end

    it 'fails when both options are selected' do
      messages = schema.(eat_cake: 'yes!', have_cake: 'yes!').messages[:be_reasonable]

      expect(messages).to eql(['you cannot eat cake and have cake!'])
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

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