Fixes email validation (#74)

Co-authored-by: Parasprite <foalspeedahead@gmail.com>
This commit is contained in:
parasprite 2020-11-27 19:14:26 -06:00 committed by GitHub
parent 7a6582bbc7
commit b79f07b86d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 7 additions and 7 deletions

View file

@ -161,7 +161,7 @@ defmodule Philomena.Users.User do
defp validate_email(changeset) do defp validate_email(changeset) do
changeset changeset
|> validate_required([:email]) |> validate_required([:email])
|> validate_format(:email, ~r/^[^\s]+@[^\s]+$/, message: "must have the @ sign and no spaces") |> validate_format(:email, ~r/^[^\s]+@[^\s]+\.[^\s]+$/, message: "must be valid (e.g., user@example.com)")
|> validate_length(:email, max: 160) |> validate_length(:email, max: 160)
|> unsafe_validate_unique(:email, Philomena.Repo) |> unsafe_validate_unique(:email, Philomena.Repo)
end end

View file

@ -35,7 +35,7 @@ h3 Change email
p Oops, something went wrong! Please check the errors below. p Oops, something went wrong! Please check the errors below.
.field .field
= email_input f, :email, class: "input", placeholder: "Email", required: true, pattern: ".*@.*" = email_input f, :email, class: "input", placeholder: "Email", required: true, pattern: ~S/[^\s]+@[^\s]+\.[^\s]+/
= error_tag f, :email = error_tag f, :email
.field .field

View file

@ -16,7 +16,7 @@ h1 Register
' You'll use your email address to log in, and we'll use this to get in ' You'll use your email address to log in, and we'll use this to get in
' touch if we need to. Don't worry, we won't share this or spam you. ' touch if we need to. Don't worry, we won't share this or spam you.
.field .field
= email_input f, :email, class: "input", placeholder: "Email", required: true, pattern: ".*@.*" = email_input f, :email, class: "input", placeholder: "Email", required: true, pattern: ~S/[^\s]+@[^\s]+\.[^\s]+/
= error_tag f, :email = error_tag f, :email
.fieldlabel .fieldlabel

View file

@ -10,7 +10,7 @@ h1 Sign in
p = link "Forgot your password?", to: Routes.password_path(@conn, :new) p = link "Forgot your password?", to: Routes.password_path(@conn, :new)
.field .field
= email_input f, :email, class: "input", required: true, placeholder: "Email", autofocus: true, pattern: ".*@.*" = email_input f, :email, class: "input", required: true, placeholder: "Email", autofocus: true, pattern: ~S/[^\s]+@[^\s]+\.[^\s]+/
= error_tag f, :email = error_tag f, :email
.field .field

View file

@ -97,7 +97,7 @@ defmodule Philomena.UsersTest do
{:error, changeset} = Users.register_user(%{email: "not valid", password: "not valid"}) {:error, changeset} = Users.register_user(%{email: "not valid", password: "not valid"})
assert %{ assert %{
email: ["must have the @ sign and no spaces"], email: ["must be valid (e.g., user@example.com)"],
password: ["should be at least 12 character(s)"] password: ["should be at least 12 character(s)"]
} = errors_on(changeset) } = errors_on(changeset)
end end
@ -167,7 +167,7 @@ defmodule Philomena.UsersTest do
{:error, changeset} = {:error, changeset} =
Users.apply_user_email(user, valid_user_password(), %{email: "not valid"}) Users.apply_user_email(user, valid_user_password(), %{email: "not valid"})
assert %{email: ["must have the @ sign and no spaces"]} = errors_on(changeset) assert %{email: ["must be valid (e.g., user@example.com)"]} = errors_on(changeset)
end end
test "validates maximum value for email for security", %{user: user} do test "validates maximum value for email for security", %{user: user} do

View file

@ -41,7 +41,7 @@ defmodule PhilomenaWeb.RegistrationControllerTest do
}) })
response = html_response(conn, 200) response = html_response(conn, 200)
assert response =~ "must have the @ sign and no spaces" assert response =~ "must be valid (e.g., user@example.com)"
assert response =~ "should be at least 12 character" assert response =~ "should be at least 12 character"
end end
end end