fix(Champs::PhoneChamp): validates Champs::PhoneChamp respecting it's validation context.

This commit is contained in:
mfo 2024-04-01 07:16:32 +02:00
parent 374d763084
commit d7f152f4af
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
2 changed files with 37 additions and 35 deletions

View file

@ -25,7 +25,8 @@ class Champs::PhoneChamp < Champs::TextChamp
possible: true,
allow_blank: true,
message: I18n.t(:not_a_phone, scope: 'activerecord.errors.messages')
}, unless: -> { Phonelib.valid_for_countries?(value, DEFAULT_COUNTRY_CODES) }
},
if: -> { (validate_champ_value? || validation_context == :prefill) && !Phonelib.valid_for_countries?(value, DEFAULT_COUNTRY_CODES) }
def to_s
return '' if value.blank?

View file

@ -1,44 +1,45 @@
describe Champs::PhoneChamp do
let(:phone_champ) { build(:champ_phone) }
let(:champ) { build(:champ_phone) }
# subject { champ }
describe '#valid?' do
describe '#validate' do
it do
expect(champ_with_value(nil)).to be_valid
expect(champ_with_value("0123456789 0123456789")).to_not be_valid
expect(champ_with_value("01.23.45.67.89 01.23.45.67.89")).to_not be_valid
expect(champ_with_value("3646")).to be_valid
expect(champ_with_value("0123456789")).to be_valid
expect(champ_with_value("01.23.45.67.89")).to be_valid
expect(champ_with_value("0123 45.67.89")).to be_valid
expect(champ_with_value("0033 123-456-789")).to be_valid
expect(champ_with_value("0033 123-456-789")).to be_valid
expect(champ_with_value("0033(0)123456789")).to be_valid
expect(champ_with_value("+33-1.23.45.67.89")).to be_valid
expect(champ_with_value("+33 - 123 456 789")).to be_valid
expect(champ_with_value("+33(0) 123 456 789")).to be_valid
expect(champ_with_value("+33 (0)123 45 67 89")).to be_valid
expect(champ_with_value("+33 (0)1 2345-6789")).to be_valid
expect(champ_with_value("+33(0) - 123456789")).to be_valid
expect(champ_with_value("+1(0) - 123456789")).to be_valid
expect(champ_with_value("+49 2109 87654321")).to be_valid
expect(champ_with_value("012345678")).to be_valid
expect(champ_with_value(nil).validate(:champs_public_value)).to be_truthy
expect(champ_with_value("0123456789 0123456789").validate(:champs_public_value)).to_not be_truthy
expect(champ_with_value("01.23.45.67.89 01.23.45.67.89").validate(:champs_public_value)).to_not be_truthy
expect(champ_with_value("3646").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("0123456789").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("01.23.45.67.89").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("0123 45.67.89").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("0033 123-456-789").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("0033 123-456-789").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("0033(0)123456789").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("+33-1.23.45.67.89").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("+33 - 123 456 789").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("+33(0) 123 456 789").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("+33 (0)123 45 67 89").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("+33 (0)1 2345-6789").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("+33(0) - 123456789").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("+1(0) - 123456789").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("+49 2109 87654321").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("012345678").validate(:champs_public_value)).to be_truthy
# DROM numbers should be valid
expect(champ_with_value("06 96 04 78 07")).to be_valid
expect(champ_with_value("05 94 22 31 31")).to be_valid
expect(champ_with_value("+594 5 94 22 31 31")).to be_valid
expect(champ_with_value("06 96 04 78 07").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("05 94 22 31 31").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("+594 5 94 22 31 31").validate(:champs_public_value)).to be_truthy
# polynesian numbers should not return errors in any way
## landline numbers start with 40 or 45
expect(champ_with_value("45187272")).to be_valid
expect(champ_with_value("40 473 500")).to be_valid
expect(champ_with_value("40473500")).to be_valid
expect(champ_with_value("45473500")).to be_valid
expect(champ_with_value("45187272").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("40 473 500").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("40473500").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("45473500").validate(:champs_public_value)).to be_truthy
## +689 is the international indicator
expect(champ_with_value("+689 45473500")).to be_valid
expect(champ_with_value("0145473500")).to be_valid
expect(champ_with_value("+689 45473500").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("0145473500").validate(:champs_public_value)).to be_truthy
## polynesian mobile numbers start with 87, 88, 89
expect(champ_with_value("87473500")).to be_valid
expect(champ_with_value("88473500")).to be_valid
expect(champ_with_value("89473500")).to be_valid
expect(champ_with_value("87473500").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("88473500").validate(:champs_public_value)).to be_truthy
expect(champ_with_value("89473500").validate(:champs_public_value)).to be_truthy
end
end
@ -61,6 +62,6 @@ describe Champs::PhoneChamp do
end
def champ_with_value(number)
phone_champ.tap { |c| c.value = number }
champ.tap { |c| c.value = number }
end
end