Sha256: 5a320867873a53c3fc6b156285e7680ad7fefe08842c7432dccab660ad2e85c8

Contents?: true

Size: 916 Bytes

Versions: 4

Compression:

Stored size: 916 Bytes

Contents

RSpec.describe 'Schema with xor rules' do
  subject(:validate) { schema.new }

  let(:schema) do
    Class.new(Dry::Validation::Schema) do
      def self.messages
        Messages.default.merge(
          en: { errors: { be_reasonable: 'you cannot eat cake and have cake!' } }
        )
      end

      key(:eat_cake) { |v| v.eql?('yes!') }
      key(:have_cake) { |v| v.eql?('yes!') }

      rule(:be_reasonable) { rule(:eat_cake) ^ rule(:have_cake) }
    end
  end

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

      expect(messages).to be(nil)
    end

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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dry-validation-0.6.0 spec/integration/schema/xor_spec.rb
dry-validation-0.5.0 spec/integration/schema/xor_spec.rb
dry-validation-0.4.1 spec/integration/schema/xor_spec.rb
dry-validation-0.4.0 spec/integration/schema/xor_spec.rb