Skip to content

Commit

Permalink
Validate email format in Person model (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
krschacht authored May 27, 2024
1 parent 1982578 commit b9dd783
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Person < ApplicationRecord

validate :personable_id_unchanged, on: :update
validates_associated :personable
validates :email, presence: true, uniqueness: true
validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }
validate :proper_personable_id, on: :update

scope :ordered, -> { order(:created_at) }
Expand Down
7 changes: 7 additions & 0 deletions test/models/person_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@ class PersonTest < ActiveSupport::TestCase
assert person.save
assert_instance_of User, person.personable
end

test "person with invalid email format validation" do
person = Person.new(email: "invalid_email")
refute person.valid?
assert_includes person.errors[:email], "is invalid"
refute person.save, "Person with invalid email was saved"
end
end

0 comments on commit b9dd783

Please sign in to comment.