diff options
author | Reuben Pereira <rpereira@gitlab.com> | 2019-07-31 06:54:03 +0000 |
---|---|---|
committer | James Lopez <james@gitlab.com> | 2019-07-31 06:54:03 +0000 |
commit | 5c7f2853dc5a8eca874108a0217a115090f29e9b (patch) | |
tree | 15e22a546814fa495fbb00ce1fdfbf2efa97a1d2 /spec/validators | |
parent | 67ffe3ced0a704d78f528e5dc8ea4243e5e4a47f (diff) | |
download | gitlab-ce-5c7f2853dc5a8eca874108a0217a115090f29e9b.tar.gz |
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.
Diffstat (limited to 'spec/validators')
-rw-r--r-- | spec/validators/qualified_domain_array_validator_spec.rb | 30 |
1 files changed, 11 insertions, 19 deletions
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 |