Skip to content

Commit

Permalink
fix(champs.email): allow nil
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin committed Feb 19, 2024
1 parent 284ecf8 commit 7d8111b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/models/champs/email_champ.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Champs::EmailChamp < Champs::TextChamp
include EmailSanitizableConcern
before_validation -> { sanitize_email(:value) }
validates :value, format: { with: StrictEmailValidator::REGEXP }, if: :validate_champ_value?
validates :value, allow_blank: true, format: { with: StrictEmailValidator::REGEXP }, if: :validate_champ_value?
end
14 changes: 8 additions & 6 deletions spec/models/champs/email_champ_spec.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
describe Champs::EmailChamp do
describe 'validation' do
let(:now) { Time.zone.now }
let(:before) { now + 1.day }
let(:after) { now + 1.day }
let(:champ) { build(:champ_email, value: value) }

subject { champ.valid?(:validate_champ_value) }
subject { champ.validate(:champs_public_value) }

context 'when nil' do
let(:value) { nil }
# what we allowed but it was a mistake
it { is_expected.to be_truthy }
end
context 'when value is username' do
let(:value) { 'username' }
# what we allowed but it was a mistake
it { is_expected.to be_truthy }
it { is_expected.to be_falsey }
end

context 'when value does not contain extension' do
let(:value) { 'username@mailserver' }
# what we allowed but it was a mistake
it { is_expected.to be_truthy }
it { is_expected.to be_falsey }
end

context 'when value include an alias' do
Expand Down

0 comments on commit 7d8111b

Please sign in to comment.