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..76f8c28 --- /dev/null +++ b/spec/umbrellio_utils/checks_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +describe UmbrellioUtils::Checks do + describe "#valid_email?" do + subject(:result) { described_class.valid_email?(input) } + + let(:input) { "user@example.com" } + + it { is_expected.to eq(true) } + + context "invalid input" do + let(:input) { "invalid" } + + it { is_expected.to eq(false) } + end + + context "input with subdomains and digits" do + let(:input) { "user@one.two42.com" } + + it { is_expected.to eq(true) } + end + + context "non-string input" do + let(:input) { 123 } + + it { is_expected.to eq(false) } + end + end +end