Improve contact phone number validation

#569
This commit is contained in:
Artur Beljajev 2017-08-08 15:13:13 +03:00
parent 7d2d34cb34
commit bc3962cdfa
4 changed files with 63 additions and 16 deletions

View file

@ -22,7 +22,7 @@ class Contact < ActiveRecord::Base
validates :name, :email, :ident, :ident_type, presence: true
validates :street, :city, :zip, :country_code, presence: true, if: 'self.class.address_processing?'
validates :phone, presence: true, format: /\+[0-9]{1,3}\.[0-9]{1,14}?/, phone: true
validates :phone, presence: true, e164: true, phone: true
validates :email, format: /@/
validates :email, email_format: { message: :invalid }, if: proc { |c| c.email_changed? }

View file

@ -0,0 +1,10 @@
class E164Validator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
validator = ActiveModel::Validations::LengthValidator.new(maximum: 17, attributes: attribute)
validator.validate(record)
validator = ActiveModel::Validations::FormatValidator.new(with: /\+[0-9]{1,3}\.[0-9]{1,14}?/,
attributes: attribute)
validator.validate(record)
end
end

View file

@ -0,0 +1,29 @@
# https://en.wikipedia.org/wiki/E.164
RSpec.shared_examples 'e164 phone number' do
describe 'validation' do
it 'rejects invalid format' do
model.send("#{attribute}=", '+.1')
model.validate
expect(model.errors).to be_added(attribute, :invalid)
end
it 'rejects longer than max length' do
model.send("#{attribute}=", '1' * 18)
model.validate
expect(model.errors).to be_added(attribute, :too_long, count: 17)
end
it 'accepts valid format' do
model.send("#{attribute}=", '+123.4')
model.validate
expect(model.errors).to_not be_added(attribute, :invalid)
end
it 'accepts max length' do
model.send("#{attribute}=", '1' * 17)
model.validate
expect(model.errors).to_not be_added(attribute, :too_long, count: 17)
end
end
end

View file

@ -1,37 +1,45 @@
require 'rails_helper'
require 'lib/e164_phone_number'
RSpec.describe Contact do
describe 'phone validation', db: false do
let(:contact) { described_class.new }
let(:contact) { described_class.new }
describe 'phone', db: false do
it_behaves_like 'e164 phone number' do
let(:model) { contact }
let(:attribute) { :phone }
end
end
describe 'phone validation', db: false do
it 'rejects absent' do
contact.phone = nil
contact.validate
expect(contact.errors).to have_key(:phone)
end
it 'rejects invalid format' do
contact.phone = '123'
contact.validate
expect(contact.errors).to have_key(:phone)
expect(contact.errors).to be_added(:phone, :blank)
end
it 'rejects all zeros in country code' do
contact.phone = '+000.1'
contact.validate
expect(contact.errors).to have_key(:phone)
expect(contact.errors).to be_added(:phone, :invalid)
end
it 'rejects all zeros in phone number' do
it 'rejects all zeros in subscriber number' do
contact.phone = '+123.0'
contact.validate
expect(contact.errors).to have_key(:phone)
expect(contact.errors).to be_added(:phone, :invalid)
end
it 'accepts valid' do
contact.phone = '+123.4'
it 'translates :blank error message' do
contact.phone = nil
contact.validate
expect(contact.errors).to_not have_key(:phone)
expect(contact.errors.generate_message(:phone, :blank)).to eq('Required parameter missing - phone')
end
it 'translates :invalid error message' do
contact.phone = nil
contact.validate
expect(contact.errors.generate_message(:phone, :invalid)).to eq('Phone nr is invalid')
end
end
end