Sha256: 4a4d29208e2e7e890d2e1c787f9897f0a75239fa192646348da3403a1125f7fc

Contents?: true

Size: 1.5 KB

Versions: 19

Compression:

Stored size: 1.5 KB

Contents

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

        required(:login).filled.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
        required(:left).maybe(:int?)
        required(:right).maybe(:int?)

        required(: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

  context 'predicate with options' do
    subject(:schema) do
      Dry::Validation.Schema do
        required(:bar).maybe

        required(:foo).filled.when(size?: 3) do
          value(:bar).filled?
        end
      end
    end

    it 'generates check rule' do
      expect(schema.(foo: [1,2,3], bar: nil).messages).to eql(
        bar: ['must be filled']
      )

      expect(schema.(foo: [1,2], bar: nil).messages).to be_empty
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

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