Sha256: 899223e7efe1890d9e03af7931fccd3d87083371c2bef171276dcbb7b550a59c

Contents?: true

Size: 951 Bytes

Versions: 4

Compression:

Stored size: 951 Bytes

Contents

RSpec.describe 'Schema with negated 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

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

      rule(:be_reasonable) do
        rule(:eat_cake) & rule(:have_cake).not
      end
    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/not_spec.rb
dry-validation-0.5.0 spec/integration/schema/not_spec.rb
dry-validation-0.4.1 spec/integration/schema/not_spec.rb
dry-validation-0.4.0 spec/integration/schema/not_spec.rb