Skip to content

Commit

Permalink
add brazilian contact phone number validator
Browse files Browse the repository at this point in the history
  • Loading branch information
clairton committed Feb 16, 2023
1 parent 22c6c70 commit 5aafcad
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ gem 'faker'
gem 'net-imap', require: false
gem 'net-pop', require: false
gem 'net-smtp', require: false
gem 'phonelib'

group :production, :staging do
# we dont want request timing out in development while using byebug
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ GEM
pg_search (2.3.6)
activerecord (>= 5.2)
activesupport (>= 5.2)
phonelib (0.7.1)
procore-sift (0.16.0)
rails (> 4.2.0)
pry (0.14.1)
Expand Down Expand Up @@ -824,6 +825,7 @@ DEPENDENCIES
omniauth-rails_csrf_protection (~> 1.0)
pg
pg_search
phonelib
procore-sift
pry-rails
puma
Expand Down
13 changes: 13 additions & 0 deletions app/models/concerns/brazilian_number_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'phonelib'

class BrazilianNumberValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if value.blank?

phone = Phonelib.parse(value)

return if phone.country != 'BR' || phone.valid? || phone.type != :mobile || phone.local_number.scan(/\d/).join.length >= 11

record.errors.add(attribute, (options[:message] || 'Número esta inválido ou é um celular e precisa ter o nono digito!'))
end
end
1 change: 1 addition & 0 deletions app/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Contact < ApplicationRecord
validates :phone_number,
allow_blank: true, uniqueness: { scope: [:account_id] },
format: { with: /\+[1-9]\d{1,14}\z/, message: I18n.t('errors.contacts.phone_number.invalid') }
validates :phone_number, brazilian_number: true
validates :name, length: { maximum: 255 }

belongs_to :account
Expand Down

2 comments on commit 5aafcad

@brendewguedes
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Olá, como faço para adicionar isso? Boa noite!

@clairton
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pode usar no docker ghcr.io/clairton/chatwoot:v2.17.0-uno-ce, esta com esta e mais algumas alterações para integração com a unoapi
Ou fazer um git cherry-pick desse commit especifico

Please sign in to comment.