fix: infinite loop in inspect

test: test new form errors
This commit is contained in:
Zach Daniel 2022-10-09 23:22:13 -04:00
parent 7f6167ddc5
commit 72b4de3f4f
2 changed files with 21 additions and 8 deletions

View file

@ -158,6 +158,7 @@ defmodule AshPhoenix.Form do
end end
``` ```
""" """
defstruct [ defstruct [
:resource, :resource,
:action, :action,
@ -188,6 +189,8 @@ defmodule AshPhoenix.Form do
just_submitted?: false just_submitted?: false
] ]
@derive {Inspect, except: [:prev_data_trail, :opts]}
alias AshPhoenix.Form.InvalidPath alias AshPhoenix.Form.InvalidPath
@type t :: %__MODULE__{ @type t :: %__MODULE__{
@ -3901,14 +3904,6 @@ defmodule AshPhoenix.Form do
defp form_for_method(:create), do: "post" defp form_for_method(:create), do: "post"
defp form_for_method(_), do: "put" defp form_for_method(_), do: "put"
defimpl Inspect do
import Inspect.Algebra
def inspect(form, opts) do
to_doc(Map.put(form, :prev_data_trail, "..."), opts)
end
end
defimpl Phoenix.HTML.FormData do defimpl Phoenix.HTML.FormData do
import AshPhoenix.FormData.Helpers import AshPhoenix.FormData.Helpers

View file

@ -126,6 +126,24 @@ defmodule AshPhoenix.FormTest do
assert form.valid? == false assert form.valid? == false
end end
test "phoenix forms are accepted as input in some cases" do
form = Form.for_create(PostWithDefault, :create, api: Api)
form = AshPhoenix.Form.validate(form, %{"text" => ""}, errors: form.submitted_once?)
form = form_for(form, "foo")
# This simply shouldn't raise
AshPhoenix.Form.params(form)
end
test "a friendly error is provided if you use a phoenix form where you shouldn't have" do
form = Form.for_create(PostWithDefault, :create, api: Api)
form = AshPhoenix.Form.validate(form, %{"text" => ""}, errors: form.submitted_once?)
form = form_for(form, "foo")
assert_raise ArgumentError, ~r//, fn ->
AshPhoenix.Form.validate(form, %{})
end
end
test "it supports forms with data and a `type: :append_and_remove`" do test "it supports forms with data and a `type: :append_and_remove`" do
post = post =
Post Post