Sha256: f994444488f947121ee503b3bcb51eae497f5545c021ddf94d538cab59969a7e

Contents?: true

Size: 911 Bytes

Versions: 19

Compression:

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

      required(:eat_cake).filled

      required(:have_cake).filled

      rule(be_reasonable: %i[eat_cake have_cake]) do |ec, hc|
        ec.eql?('yes!') ^ hc.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
      expect(schema.(eat_cake: 'yes!', have_cake: 'yes!').messages).to eql(
        be_reasonable: ['you cannot eat cake and have cake!']
      )
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
dry-validation-0.13.3 spec/integration/schema/xor_spec.rb
dry-validation-0.13.2 spec/integration/schema/xor_spec.rb
dry-validation-0.13.1 spec/integration/schema/xor_spec.rb
dry-validation-0.11.2 spec/integration/schema/xor_spec.rb
dry-validation-0.12.3 spec/integration/schema/xor_spec.rb
dry-validation-0.13.0 spec/integration/schema/xor_spec.rb
dry-validation-0.12.2 spec/integration/schema/xor_spec.rb
dry-validation-0.12.1 spec/integration/schema/xor_spec.rb
dry-validation-0.12.0 spec/integration/schema/xor_spec.rb
dry-validation-0.11.1 spec/integration/schema/xor_spec.rb
dry-validation-0.11.0 spec/integration/schema/xor_spec.rb
dry-validation-0.10.7 spec/integration/schema/xor_spec.rb
dry-validation-0.10.6 spec/integration/schema/xor_spec.rb
dry-validation-0.10.5 spec/integration/schema/xor_spec.rb
dry-validation-0.10.4 spec/integration/schema/xor_spec.rb
dry-validation-0.10.3 spec/integration/schema/xor_spec.rb
dry-validation-0.10.2 spec/integration/schema/xor_spec.rb
dry-validation-0.10.1 spec/integration/schema/xor_spec.rb
dry-validation-0.10.0 spec/integration/schema/xor_spec.rb