Skip to content

Commit

Permalink
Fix: do_decode_path for string paths (#127)
Browse files Browse the repository at this point in the history
* Raise InvalidPath exception instead of "Invalid Path: #{original_path}" string

* Add regression test
  • Loading branch information
NduatiK authored Jan 14, 2024
1 parent 606336c commit ae1a2d0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/ash_phoenix/form/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3674,7 +3674,7 @@ defmodule AshPhoenix.Form do
defp do_decode_path(_, _, [], _), do: []

defp do_decode_path([], original_path, _, _) do
raise "Invalid Path: #{original_path}"
raise InvalidPath, path: original_path
end

defp do_decode_path(forms, original_path, [key | rest], sparse?) when is_list(forms) do
Expand All @@ -3691,7 +3691,7 @@ defmodule AshPhoenix.Form do

case matching_form do
nil ->
raise "Invalid Path: #{original_path}"
raise InvalidPath, path: original_path

form ->
case Enum.at(rest, 0) do
Expand All @@ -3711,7 +3711,7 @@ defmodule AshPhoenix.Form do
end

_ ->
raise "Invalid Path: #{original_path}"
raise InvalidPath, path: original_path
end
end

Expand All @@ -3724,7 +3724,7 @@ defmodule AshPhoenix.Form do
end)
|> case do
nil ->
raise "Invalid Path: #{original_path}"
raise InvalidPath, path: original_path

{key, config} ->
if Keyword.get(config, :type, :single) == :single do
Expand Down
46 changes: 46 additions & 0 deletions test/form_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,52 @@ defmodule AshPhoenix.FormTest do
end
end

describe "has_form?" do
test "checks for the existence of a list of forms" do
form =
Post
|> Form.for_create(:create,
api: Api,
forms: [
comments: [
type: :list,
resource: Comment,
create_action: :create
]
]
)
|> Form.add_form([:comments])

# assert Form.has_form?(form, [:comments])
assert Form.has_form?(form, [:comments, 0])
assert Form.has_form?(form, "form[comments][0]")

refute Form.has_form?(form, [:comments, 1])
refute Form.has_form?(form, "form[comments][1]")
end

test "checks for the existence of a single form" do
form =
Comment
|> Form.for_create(:create,
forms: [
post: [
type: :single,
resource: Post,
create_action: :create
]
]
)
|> Form.add_form([:post])

assert Form.has_form?(form, [:post])
assert Form.has_form?(form, "form[post]")

refute Form.has_form?(form, [:unknown])
refute Form.has_form?(form, "form[unknown]")
end
end

describe "form_for fields" do
test "it should show simple field values" do
form =
Expand Down

0 comments on commit ae1a2d0

Please sign in to comment.