From 5c7f2853dc5a8eca874108a0217a115090f29e9b Mon Sep 17 00:00:00 2001 From: Reuben Pereira Date: Wed, 31 Jul 2019 06:54:03 +0000 Subject: Allow blank but not nil in validations - The most common use case for qualified_domain_validator currently is to allow blank ([]) but not allow nil. Modify the qualified_domain_validator to support this use case. --- .../qualified_domain_array_validator_spec.rb | 30 ++++++++-------------- 1 file changed, 11 insertions(+), 19 deletions(-) (limited to 'spec/validators/qualified_domain_array_validator_spec.rb') diff --git a/spec/validators/qualified_domain_array_validator_spec.rb b/spec/validators/qualified_domain_array_validator_spec.rb index a96b00bfd1d..6beb4c67f6f 100644 --- a/spec/validators/qualified_domain_array_validator_spec.rb +++ b/spec/validators/qualified_domain_array_validator_spec.rb @@ -19,20 +19,19 @@ describe QualifiedDomainArrayValidator do subject { validator.validate(record) } - shared_examples 'cannot be blank' do - it 'returns error when attribute is blank' do - record.domain_array = [] + shared_examples 'can be nil' do + it 'allows when attribute is nil' do + record.domain_array = nil subject - expect(record.errors).to be_present - expect(record.errors.first[1]).to eq 'entries cannot be blank' + expect(record.errors).to be_empty end end - shared_examples 'can be nil' do - it 'allows when attribute is nil' do - record.domain_array = nil + shared_examples 'can be blank' do + it 'allows when attribute is blank' do + record.domain_array = [] subject @@ -43,7 +42,7 @@ describe QualifiedDomainArrayValidator do describe 'validations' do let(:validator) { described_class.new(attributes: [:domain_array]) } - it_behaves_like 'cannot be blank' + it_behaves_like 'can be blank' it 'returns error when attribute is nil' do record.domain_array = nil @@ -51,6 +50,7 @@ describe QualifiedDomainArrayValidator do subject expect(record.errors).to be_present + expect(record.errors.first[1]).to eq('entries cannot be nil') end it 'allows when domain is valid' do @@ -91,21 +91,13 @@ describe QualifiedDomainArrayValidator do let(:validator) { described_class.new(attributes: [:domain_array], allow_nil: true) } it_behaves_like 'can be nil' - - it_behaves_like 'cannot be blank' + it_behaves_like 'can be blank' end context 'when allow_blank is set to true' do let(:validator) { described_class.new(attributes: [:domain_array], allow_blank: true) } it_behaves_like 'can be nil' - - it 'allows when attribute is blank' do - record.domain_array = [] - - subject - - expect(record.errors).to be_empty - end + it_behaves_like 'can be blank' end end -- cgit v1.2.3