From 6405165c89d6f20e27938e59dcba3e44a1d09567 Mon Sep 17 00:00:00 2001 From: Yuri Smirnov Date: Mon, 28 Oct 2024 12:32:45 +0300 Subject: [PATCH] Fix email check (#34) --- Gemfile.lock | 2 +- lib/umbrellio_utils/checks.rb | 7 ++++--- lib/umbrellio_utils/version.rb | 2 +- spec/umbrellio_utils/checks_spec.rb | 24 ++++++++++++++++++++++++ 4 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 spec/umbrellio_utils/checks_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index 46a2017..0cbb6b7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - umbrellio-utils (1.5.1) + umbrellio-utils (1.5.2) memery (~> 1) GEM diff --git a/lib/umbrellio_utils/checks.rb b/lib/umbrellio_utils/checks.rb index bc00981..d6cc244 100644 --- a/lib/umbrellio_utils/checks.rb +++ b/lib/umbrellio_utils/checks.rb @@ -1,10 +1,11 @@ # frozen_string_literal: true +require "uri/mailto" + module UmbrellioUtils module Checks extend self - EMAIL_REGEXP = /\A([\w+-].?)+@[a-z\d-]+(\.[a-z]+)*\.[a-z]+\z/i HOLDER_NAME_REGEXP = /\A([A-Za-z0-9.'-]+ ?)+\z/ def secure_compare(src, dest) @@ -30,11 +31,11 @@ def valid_card?(number) end def valid_email?(email) - email.to_s =~ EMAIL_REGEXP + email.to_s.match?(URI::MailTo::EMAIL_REGEXP) end def valid_card_holder?(holder) - holder.to_s =~ HOLDER_NAME_REGEXP + holder.to_s.match?(HOLDER_NAME_REGEXP) end def valid_card_cvv?(cvv) diff --git a/lib/umbrellio_utils/version.rb b/lib/umbrellio_utils/version.rb index 7c8b1df..8dcade6 100644 --- a/lib/umbrellio_utils/version.rb +++ b/lib/umbrellio_utils/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module UmbrellioUtils - VERSION = "1.5.1" + VERSION = "1.5.2" end diff --git a/spec/umbrellio_utils/checks_spec.rb b/spec/umbrellio_utils/checks_spec.rb new file mode 100644 index 0000000..256830d --- /dev/null +++ b/spec/umbrellio_utils/checks_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +describe UmbrellioUtils::Checks do + describe "#valid_email?" do + subject(:result) { described_class.valid_email?(input) } + + expectations = { + "user@example.com" => true, + "user@one.two42.com" => true, + "invalid" => false, + 123 => false, + nil => false, + } + + expectations.each do |input, expected_result| + context "with input #{input.inspect} should return #{expected_result.inspect}" do + let(:input) { input } + let(:expected_result) { expected_result } + + it { is_expected.to eq(expected_result) } + end + end + end +end