From aa7cf1d1b3611877e2878b85f2d22923f5fae791 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Fri, 23 Jul 2021 09:44:27 +0300 Subject: [PATCH 1/2] using validation for only person names --- app/models/contact.rb | 2 +- config/locales/contacts.en.yml | 1 + test/models/contact_test.rb | 5 +++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/models/contact.rb b/app/models/contact.rb index 01a4bab3d..ea422041e 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -32,7 +32,7 @@ class Contact < ApplicationRecord [\u005B-\u005F\u007B-\u007E\u2040-\u206F\u20A0-\u20BF\u2100-\u218F])/x.freeze validates :name, :email, presence: true - validates :name, format: { without: NAME_REGEXP, message: :invalid } + validates :name, format: { without: NAME_REGEXP, message: :invalid }, if: -> { priv? } validates :street, :city, :zip, :country_code, presence: true, if: lambda { self.class.address_processing? diff --git a/config/locales/contacts.en.yml b/config/locales/contacts.en.yml index 4b4d099d7..c3fe0d47c 100644 --- a/config/locales/contacts.en.yml +++ b/config/locales/contacts.en.yml @@ -14,6 +14,7 @@ en: too_long_contact_code: "Contact code is too long, max 100 characters" name: blank: "Required parameter missing - name" + invalid: "Name is invalid" phone: blank: "Required parameter missing - phone" invalid: "Phone nr is invalid" diff --git a/test/models/contact_test.rb b/test/models/contact_test.rb index 189a85dc7..69f17da15 100644 --- a/test/models/contact_test.rb +++ b/test/models/contact_test.rb @@ -40,6 +40,11 @@ class ContactTest < ActiveJob::TestCase assert contact.valid? contact.name = '# "¤ #" ¤ "?' assert contact.invalid? + + contact.country_code = 'FI' + contact.ident_type = Contact::ORG + contact.ident = '1234' + assert contact.valid? end def test_validates_code_format From 17fd942459ba9353a51ce74ad9ccad7b8f93b772 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Fri, 23 Jul 2021 10:57:00 +0300 Subject: [PATCH 2/2] added name_regexp --- app/models/contact.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/models/contact.rb b/app/models/contact.rb index 27e55428b..ea422041e 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -28,6 +28,9 @@ class Contact < ApplicationRecord .where('success = false and verified_at IS NOT NULL') } + NAME_REGEXP = /([\u00A1-\u00B3\u00B5-\u00BF\u0021-\u0026\u0028-\u002C\u003A-\u0040]| + [\u005B-\u005F\u007B-\u007E\u2040-\u206F\u20A0-\u20BF\u2100-\u218F])/x.freeze + validates :name, :email, presence: true validates :name, format: { without: NAME_REGEXP, message: :invalid }, if: -> { priv? }