Sha256: b50c146715b21462524ab7435325f170da9b477779d4c086587aef39f04da79a

Contents?: true

Size: 1.43 KB

Versions: 26

Compression:

Stored size: 1.43 KB

Contents

RSpec.describe 'Predicates: hash?' do
  shared_examples 'hash predicate' do
    context 'with valid input' do
      let(:input) { { foo: { a: 1 } } }

      it 'is successful' do
        expect(result).to be_successful
      end
    end

    context 'with nil input' do
      let(:input) { { foo: nil } }

      it 'is not successful' do
        expect(result).to be_failing ['must be a hash']
      end
    end

    context 'with blank input' do
      let(:input) { { foo: '' } }

      it 'is not successful' do
        expect(result).to be_failing ['must be a hash']
      end
    end

    context 'with invalid type' do
      let(:input) { { foo: 1 } }

      it 'is not successful' do
        expect(result).to be_failing ['must be a hash']
      end
    end
  end

  context 'with required' do
    subject(:schema) do
      Dry::Validation.Schema do
        required(:foo) { hash? }
      end
    end

    it_behaves_like 'hash predicate' do
      context 'with missing input' do
        let(:input) { {} }

        it 'is not successful' do
          expect(result).to be_failing ['is missing']
        end
      end
    end
  end

  context 'with optional' do
    subject(:schema) do
      Dry::Validation.Schema do
        optional(:foo) { hash? }
      end
    end

    it_behaves_like 'hash predicate' do
      let(:input) { {} }

      it 'is successful when key is no present' do
        expect(result).to be_successful
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

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