Skip to content

Commit

Permalink
feat: Unify Email Validation Across the Application (#2464)
Browse files Browse the repository at this point in the history
## Context

This pull request unifies the email validation logic across the
application, use the same validation in customer that we already had for
the organization
  • Loading branch information
brunomiguelpinto authored Aug 22, 2024
1 parent 534e226 commit c31f7bb
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 9 deletions.
1 change: 1 addition & 0 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Customer < ApplicationRecord
validates :net_payment_term, numericality: {greater_than_or_equal_to: 0}, allow_nil: true
validates :payment_provider, inclusion: {in: PAYMENT_PROVIDERS}, allow_nil: true
validates :timezone, timezone: true, allow_nil: true
validates :email, email: true, if: :email?

def self.ransackable_attributes(_auth_object = nil)
%w[id name external_id email]
Expand Down
7 changes: 0 additions & 7 deletions app/services/customers/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ def create_from_api(organization:, params:)
new_customer = customer.new_record?
shipping_address = params[:shipping_address] ||= {}

unless valid_email?(params[:email])
return result.single_validation_failure!(
field: :email,
error_code: 'invalid_format'
)
end

unless valid_metadata_count?(metadata: params[:metadata])
return result.single_validation_failure!(
field: :metadata,
Expand Down
4 changes: 3 additions & 1 deletion lib/tasks/auto_annotate_models.rake
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# frozen_string_literal: true

# NOTE: only doing this in development as some production environments (Heroku)
# NOTE: are sensitive to local FS writes, and besides -- it's just not proper
# NOTE: to have a dev-mode tool do its thing in production.
if Rails.env.development?
require 'annotate'
task :set_annotation_options do
task set_annotation_options: :environment do
# You can override any of these by setting an environment variable of the
# same name.
Annotate.set_defaults(
Expand Down
2 changes: 1 addition & 1 deletion spec/services/customers/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
expect(result).not_to be_success
expect(result.error).to be_a(BaseService::ValidationFailure)
expect(result.error.messages.keys).to include(:email)
expect(result.error.messages[:email]).to include('invalid_format')
expect(result.error.messages[:email]).to include('invalid_email_format')
end
end
end
Expand Down

0 comments on commit c31f7bb

Please sign in to comment.