fix: fix some error transforming logic

This commit is contained in:
Zach Daniel 2021-10-21 18:42:09 -04:00
parent 8f5b0f9df7
commit be5c0de5f1

View file

@ -33,12 +33,23 @@ defmodule AshPhoenix.FormData.Helpers do
|> Macro.underscore() |> Macro.underscore()
end end
defp match_path_filter?(path, path_filter, form_keys) do defp match_path_filter?(path, path_filter, form_keys \\ []) do
path == path_filter || path == path_filter ||
(List.starts_with?(path, path_filter) && (!Enum.empty?(form_keys) &&
!Enum.any?(form_keys, fn {key, _} -> !form_would_capture?(form_keys, path, path_filter))
List.starts_with?(path, path ++ [key]) end
end))
defp form_would_capture?(form_keys, path, path_filter) do
Enum.any?(form_keys, fn {key, config} ->
key = config[:for] || key
if config[:type] == :list do
List.starts_with?(path, path_filter ++ [key]) &&
is_integer(path |> Enum.drop(Enum.count(path_filter) + 1) |> Enum.at(0))
else
List.starts_with?(path, path_filter ++ [key])
end
end)
end end
def transform_errors(form, errors, path_filter \\ nil, form_keys \\ []) do def transform_errors(form, errors, path_filter \\ nil, form_keys \\ []) do
@ -52,12 +63,10 @@ defmodule AshPhoenix.FormData.Helpers do
errors errors
|> Enum.filter(fn error -> |> Enum.filter(fn error ->
if Map.has_key?(error, :path) && path_filter do if Map.has_key?(error, :path) && path_filter do
Enum.any?( match_path_filter?(error.path, path_filter, form_keys) ||
[path_filter | additional_path_filters], Enum.any?(additional_path_filters, &match_path_filter?(error.path, &1))
&match_path_filter?(error.path, &1, form_keys)
)
else else
true false
end end
end) end)
|> Enum.map(fn error -> |> Enum.map(fn error ->