Skip to content

Commit

Permalink
Merge pull request #37 from eduardoscottini/fix-translate-errors
Browse files Browse the repository at this point in the history
Fix translate errors
  • Loading branch information
gabrielpra1 authored Nov 20, 2019
2 parents 9c17ee8 + a8199dc commit 1306d19
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
12 changes: 8 additions & 4 deletions lib/middlewares/translate_errors.ex
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,14 @@ defmodule Crudry.Middlewares.TranslateErrors do
defp message_to_string(key, value, translator, locale) when is_list(value) do
value
|> Enum.reject(fn x -> x == %{} end)
|> Enum.map(fn x ->
Enum.map(x, fn {inner_key, inner_value} ->
message_to_string(key, Map.new([{inner_key, inner_value}]), translator, locale)
end)
|> Enum.map(fn
string when is_binary(string) ->
message_to_string(key, [string], translator, locale)

enumerable when is_list(enumerable) or is_map(enumerable) ->
Enum.map(enumerable, fn {inner_key, inner_value} ->
message_to_string(key, Map.new([{inner_key, inner_value}]), translator, locale)
end)
end)
end

Expand Down
1 change: 1 addition & 0 deletions priv/repo/migrations/20190116115123_create_tables.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Crudry.Repo.Migrations.CreateTables do
create table(:users) do
add :username, :string
add :age, :integer
add :password, :string
add :company_id, references(:companies)

timestamps()
Expand Down
8 changes: 8 additions & 0 deletions test/middlewares/translate_errors_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ defmodule TranslateErrorsTest do
build_resolution("username should be at least 2 character(s)")
end

test "translate multiple changeset errors that belongs to only one field" do
changeset = User.changeset(%User{}, %{username: "username", password: "a"})
resolution = build_resolution(changeset)

assert TranslateErrors.call(resolution, :_) ==
build_resolution(["password must contain an upper-case letter", "password must contain a number", "password should be at least 8 character(s)"])
end

test "translate validate_number changeset error" do
changeset = User.changeset(%User{}, %{username: "name", age: -5})
resolution = build_resolution(changeset)
Expand Down
7 changes: 6 additions & 1 deletion test/support/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Crudry.User do
schema "users" do
field(:username, :string)
field(:age, :integer)
field(:password, :string)

belongs_to(:company, Crudry.Company)
has_many(:posts, Crudry.Post)
Expand Down Expand Up @@ -34,9 +35,13 @@ defmodule Crudry.User do

defp base_changeset(user, attrs) do
user
|> cast(attrs, [:username, :age, :company_id])
|> cast(attrs, [:username, :age, :password, :company_id])
|> validate_required([:username])
|> validate_length(:username, min: 2)
|> validate_number(:age, greater_than: 0)
|> validate_length(:password, min: 8)
|> validate_format(:password, ~r/[0-9]+/, message: "must contain a number")
|> validate_format(:password, ~r/[A-Z]+/, message: "must contain an upper-case letter")
|> validate_format(:password, ~r/[a-z]+/, message: "must contain a lower-case letter")
end
end

0 comments on commit 1306d19

Please sign in to comment.