fix: Fix error with nil value on structure types (#1380)

* Fix no cond clause evaluated to a truthy value error

* Fix error with nil value

* Add tests
This commit is contained in:
Jinkyou Son 2024-08-09 09:15:20 +09:00 committed by GitHub
parent ba6b7b708a
commit 2fc934fb61
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 2 deletions

View file

@ -246,6 +246,8 @@ defmodule Ash.Type.Struct do
end
end
defp handle_instance_of(nil, _), do: {:ok, nil}
defp handle_instance_of(value, constraints) do
case Keyword.fetch(constraints, :instance_of) do
{:ok, struct} ->
@ -256,7 +258,7 @@ defmodule Ash.Type.Struct do
is_struct(value) ->
:error
value ->
true ->
if constraints[:fields] do
{:ok, struct(struct, value)}
else

View file

@ -17,7 +17,14 @@ defmodule Type.StructTest do
actions do
default_accept :*
defaults [:read, :destroy, create: :*, update: :*]
defaults [:read, :destroy, update: :*]
create :create do
primary? true
accept [:*]
argument :dummy_metadata, :struct, constraints: [instance_of: Metadata], allow_nil?: true
end
end
attributes do
@ -185,4 +192,12 @@ defmodule Type.StructTest do
}
] = changeset.errors
end
test "it handles nil argument" do
changeset =
Post
|> Ash.Changeset.for_create(:create, %{dummy_metadata: nil})
assert changeset.valid?
end
end