Sha256: 85584660c15cb36f2d35578e05a1f63c15b8c44a59cb9c5d699fcb84618c1734

Contents?: true

Size: 1.61 KB

Versions: 5

Compression:

Stored size: 1.61 KB

Contents

RSpec.describe 'Macros #when' do
  context 'with a result rule returned from the block' do
    subject(:schema) do
      Dry::Validation.Schema do
        key(:email).maybe

        key(:login).required.when(:true?) do
          value(:email).filled?
        end
      end
    end

    it 'generates check rule' do
      expect(schema.(login: true, email: nil).messages).to eql(
        email: ['must be filled']
      )

      expect(schema.(login: false, email: nil).messages).to be_empty
    end
  end

  describe 'with a result rule depending on another result' do
    subject(:schema) do
      Dry::Validation.Schema do
        key(:left).maybe(:int?)
        key(:right).maybe(:int?)

        key(:compare).maybe(:bool?).when(:true?) do
          value(:left).gt?(value(:right))
        end
      end
    end

    it 'generates check rule' do
      expect(schema.(compare: false, left: nil, right: nil)).to be_success

      expect(schema.(compare: true, left: 1, right: 2).messages).to eql(
        left: ['must be greater than 2']
      )
    end
  end

  describe 'with multiple result rules' do
    subject(:schema) do
      Dry::Validation.Schema do
        key(:email).maybe
        key(:password).maybe

        key(:login).maybe(:bool?).when(:true?) do
          value(:email).filled?
          value(:password).filled?
        end
      end
    end

    it 'generates check rule' do
      expect(schema.(login: false, email: nil, password: nil)).to be_success

      expect(schema.(login: true, email: nil, password: nil).messages).to eql(
        email: ['must be filled'],
        password: ['must be filled']
      )
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

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