Treat empty maps as nils

This commit is contained in:
tusooa 2022-12-28 15:39:37 -05:00 committed by marcin mikołajczak
parent d86a1e73e9
commit 4bcae3597f
3 changed files with 22 additions and 2 deletions

View file

@ -22,7 +22,11 @@ def cast(object) when is_map(object) do
acc
end)
{:ok, data}
if data == %{} do
{:ok, nil}
else
{:ok, data}
end
end
def cast(_), do: :error

View file

@ -345,7 +345,8 @@ def fix_tag(object), do: object
def fix_content_map(%{"content" => content} = object) when not_empty_string(content), do: object
# content map usually only has one language so this will do for now.
def fix_content_map(%{"contentMap" => content_map} = object) do
def fix_content_map(%{"contentMap" => content_map} = object)
when is_map(content_map) and content_map != %{} do
content_groups = Map.to_list(content_map)
{_, content} = Enum.at(content_groups, 0)

View file

@ -62,6 +62,21 @@ test "Note with contentMap and summaryMap", %{note: note} do
}
} = ArticleNotePageValidator.cast_and_validate(note)
end
test "Note with empty *Map", %{note: note} do
note =
note
|> Map.put("summaryMap", %{"und" => "mew"})
|> Map.put("contentMap", %{})
assert %{
valid?: true,
changes: changes
} = ArticleNotePageValidator.cast_and_validate(note)
assert Map.has_key?(changes, :summaryMap)
refute Map.has_key?(changes, :contentMap)
end
end
describe "Note with history" do