Sha256: 0d16aad1ab264786f553cbc15899d0463ed80f288682e52d0fb7e4b15e05d40e

Contents?: true

Size: 927 Bytes

Versions: 26

Compression:

Stored size: 927 Bytes

Contents

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

      optional(:eat_cake).filled
      optional(:have_cake).filled

      rule(:be_reasonable) do
        value(:eat_cake).eql?('yes!') & value(:have_cake).eql?('yes!').not
      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

26 entries across 26 versions & 1 rubygems

Version Path
dry-validation-0.9.4 spec/integration/schema/not_spec.rb
dry-validation-0.9.3 spec/integration/schema/not_spec.rb
dry-validation-0.9.2 spec/integration/schema/not_spec.rb
dry-validation-0.9.1 spec/integration/schema/not_spec.rb
dry-validation-0.9.0 spec/integration/schema/not_spec.rb
dry-validation-0.8.0 spec/integration/schema/not_spec.rb