Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions lib/ash/changeset/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6612,29 +6612,33 @@ defmodule Ash.Changeset do

defp do_set_argument(changeset, argument, value, store_casted? \\ false) do
if changeset.action do
argument =
action_argument =
Enum.find(
changeset.action.arguments,
&(&1.name == argument || to_string(&1.name) == argument)
)

if argument do
with value <- Ash.Type.Helpers.handle_indexed_maps(argument.type, value),
if action_argument do
with value <- Ash.Type.Helpers.handle_indexed_maps(action_argument.type, value),
constraints <-
Ash.Type.include_source(argument.type, changeset, argument.constraints),
Ash.Type.include_source(
action_argument.type,
changeset,
action_argument.constraints
),
{:ok, casted} <-
Ash.Type.cast_input(argument.type, value, constraints),
Ash.Type.cast_input(action_argument.type, value, constraints),
{{:ok, casted}, _last_val} <-
{Ash.Type.apply_constraints(argument.type, casted, constraints), casted} do
%{changeset | arguments: Map.put(changeset.arguments, argument.name, casted)}
|> store_casted_argument(argument.name, casted, store_casted?)
{Ash.Type.apply_constraints(action_argument.type, casted, constraints), casted} do
%{changeset | arguments: Map.put(changeset.arguments, action_argument.name, casted)}
|> store_casted_argument(action_argument.name, casted, store_casted?)
else
{:error, error} ->
add_invalid_errors(value, :argument, changeset, argument, error)
add_invalid_errors(value, :argument, changeset, action_argument, error)

{{:error, error}, last_val} ->
add_invalid_errors(value, :argument, changeset, argument, error)
|> store_casted_argument(argument.name, last_val, store_casted?)
add_invalid_errors(value, :argument, changeset, action_argument, error)
|> store_casted_argument(action_argument.name, last_val, store_casted?)
end
else
%{changeset | arguments: Map.put(changeset.arguments, argument, value)}
Expand Down
10 changes: 10 additions & 0 deletions test/changeset/changeset_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,16 @@ defmodule Ash.Test.Changeset.ChangesetTest do
assert Ash.Changeset.get_argument(changeset, :true_optional_argument) == true
assert Ash.Changeset.get_argument(changeset, :false_optional_argument) == false
end

test "arguments not defined on the action are stored under their name" do
changeset =
Category
|> Ash.Changeset.for_create(:create_with_confirmation)
|> Ash.Changeset.force_set_argument(:unknown_argument, "foo")

assert changeset.arguments[:unknown_argument] == "foo"
assert is_nil(changeset.arguments[nil])
end
end

describe "validation keyword errors" do
Expand Down
Loading