Sha256: 4d95a7c56e02f16d817e6ca6a6428a75043ccc9087d85085bbf0f9f9b79a70e5

Contents?: true

Size: 1.75 KB

Versions: 8

Compression:

Stored size: 1.75 KB

Contents

require 'spec_helper'

describe HashValidator::Validator::Base do
  let(:validator)  { HashValidator::Validator::ManyValidator.new }
  let(:errors)     { Hash.new }

  def many(validation)
    HashValidator::Validations::Many.new(validation)
  end

  describe '#should_validate?' do
    it 'should validate an Many validation' do
      expect(validator.should_validate?(many('string'))).to eq true
    end

    it 'should not validate other things' do
      expect(validator.should_validate?('string')).to eq false
      expect(validator.should_validate?('array')).to eq false
      expect(validator.should_validate?(nil)).to eq false
    end
  end

  describe '#validate' do
    it 'should accept an empty array' do
      validator.validate(:key, [], many('string'), errors)

      expect(errors).to be_empty
    end

    it 'should accept an array of matching elements' do
      validator.validate(:key, ['a', 'b'], many('string'), errors)

      expect(errors).to be_empty
    end

    it 'should not accept an array including a non-matching element' do
      validator.validate(:key, ['a', 2], many('string'), errors)

      expect(errors).to eq({ key: [nil, 'string required'] })
    end

    it 'should accept an array of matching hashes' do
      validator.validate(:key, [{v: 'a'}, {v: 'b'}], many({v: 'string'}), errors)

      expect(errors).to be_empty
    end

    it 'should not accept an array including a non-matching element' do
      validator.validate(:key, [{v: 'a'}, {v: 2}], many({v: 'string'}), errors)

      expect(errors).to eq({ key: [nil, {v: 'string required'}] })
    end

    it 'should not accept a non-enumerable' do
      validator.validate(:key, 'a', many({v: 'string'}), errors)

      expect(errors).to eq({ key: 'enumerable required' })
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
hash_validator-1.1.0 spec/validators/many_spec.rb
hash_validator-1.0.0 spec/validators/many_spec.rb
hash_validator-0.8.0 spec/validators/many_spec.rb
hash_validator-0.7.1 spec/validators/many_spec.rb
hash_validator-0.7.0 spec/validators/many_spec.rb
hash_validator-0.6.0 spec/validators/many_spec.rb
hash_validator-0.5.0 spec/validators/many_spec.rb
hash_validator-0.4.0 spec/validators/many_spec.rb