Skip to content

Commit

Permalink
pass empty map, when embed is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Grigory Antonov committed Jul 8, 2022
1 parent c33f7ef commit a8bdbf4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/tarams.ex
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ defmodule Tarams do
end
end

defp cast_value("", %{} = type), do: cast_value(%{}, type)
defp cast_value(nil, %{} = type), do: cast_value(%{}, type)
defp cast_value(nil, _), do: {:ok, nil}

# cast array of custom map
Expand Down
24 changes: 24 additions & 0 deletions test/tarams_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,30 @@ defmodule ParamTest do
Tarams.cast(data, @schema)
end

test "cast empty embed type should error" do
data = %{
user: ""
}

assert {:error, %{user: %{name: ["is required"]}}} = Tarams.cast(data, @schema)
end

test "cast empty map embed type should error" do
data = %{
user: %{}
}

assert {:error, %{user: %{name: ["is required"]}}} = Tarams.cast(data, @schema)
end

test "cast nil embed type should error" do
data = %{
user: nil
}

assert {:error, %{user: %{name: ["is required"]}}} = Tarams.cast(data, @schema)
end

@tag :only
test "cast missing required value should error" do
data = %{
Expand Down

0 comments on commit a8bdbf4

Please sign in to comment.