fix: handle invalid input in basic actions

This commit is contained in:
Zach Daniel 2023-05-02 02:07:53 -04:00
parent 2e543bc212
commit 2843224f06
3 changed files with 27 additions and 1 deletions

View file

@ -5,7 +5,16 @@ defmodule Ash.ActionInput do
alias Ash.Error.Action.InvalidArgument
defstruct [:action, :api, :resource, arguments: %{}, params: %{}, context: %{}, valid?: true]
defstruct [
:action,
:api,
:resource,
arguments: %{},
params: %{},
context: %{},
valid?: true,
errors: []
]
@type t :: %__MODULE__{
arguments: map(),

View file

@ -3,6 +3,10 @@ defmodule Ash.Actions.Action do
require Ash.Tracer
def run(_api, %{valid?: false, errors: errors}, _opts) do
{:error, Ash.Error.to_error_class(errors)}
end
def run(api, input, opts) do
{input, opts} = Ash.Actions.Helpers.add_process_context(api, input, opts)

View file

@ -71,6 +71,19 @@ defmodule Ash.Test.Actions.BasicTest do
|> Ash.ActionInput.for_action(:hello, %{name: "fred"})
|> Api.run_action!()
end
test "basic actions validate their input" do
assert {:error, %Ash.Error.Invalid{}} =
Post
|> Ash.ActionInput.for_action(:hello, %{name: %{a: 10}})
|> Api.run_action()
assert_raise Ash.Error.Invalid, ~r/Input Invalid/, fn ->
Post
|> Ash.ActionInput.for_action(:hello, %{name: %{a: 10}})
|> Api.run_action!()
end
end
end
describe "authorization" do