mirror of
https://github.com/ash-project/ash.git
synced 2024-09-19 13:03:02 +12:00
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:
parent
ba6b7b708a
commit
2fc934fb61
2 changed files with 19 additions and 2 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue