Sha256: d6bf64e1143743ce64929f87de265493771f915b4bc08ad7bf4b43e927ac938f

Contents?: true

Size: 1.43 KB

Versions: 25

Compression:

Stored size: 1.43 KB

Contents

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

      required(:settings).schema do
        optional(:offers).filled(:bool?)
        required(:newsletter).filled(:bool?)
      end

      rule(newsletter: [[:settings, :newsletter], [:settings, :offers]]) do |newsletter, offers|
        offers.true?.then(newsletter.false?)
      end

      rule(email: [[:settings, :newsletter], :email]) do |newsletter, email|
        newsletter.true?.then(email.filled?)
      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
    input = { settings: { offers: true, newsletter: true }, email: 'jane@doe' }

    expect(schema.(input).messages).to eql(
      settings: { newsletter: ['must be false'] }
    )
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

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