Sha256: 89b8d583f061744b3494a4cf2007858e0e2b31626956fab7a3a91296b92a1dc1

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 KB

Contents

RSpec.describe Schema, 'using nested values' do
  let(:schema) do
    Dry::Validation.Schema do
      key(:email).maybe

      key(:settings) do
        optional(:offers).required(:bool?).when(:true?) do
          value([:settings, :newsletter]).false?
        end

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

  it 'passes when newsletter setting is false' do
    expect(schema.(settings: { newsletter: false }, email: nil)).to be_success
  end

  it 'passes when newsletter setting is true and email is filled' do
    expect(schema.(settings: { newsletter: false }, email: 'jane@doe')).to be_success
  end

  it 'passes when offers is false and newsletter is true' do
    expect(schema.(settings: { offers: false, newsletter: true }, email: 'jane@doe')).to be_success
  end

  it 'fails when newsletter is true and email is not filled' do
    expect(schema.(settings: { newsletter: true }, email: nil).messages).to eql(
      email: ['must be filled']
    )
  end

  it 'fails when offers is true and newsletter is true' do
    expect(schema.(settings: { offers: true, newsletter: true }, email: 'jane@doe').messages).to eql(
      settings: { newsletter: ['must be false'] }
    )
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

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