From 2843224f06efc695ce053df9bb29ae6604ea12ae Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Tue, 2 May 2023 02:07:53 -0400 Subject: [PATCH] fix: handle invalid input in basic actions --- lib/ash/action_input.ex | 11 ++++++++++- lib/ash/actions/action.ex | 4 ++++ test/actions/basic_test.exs | 13 +++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/ash/action_input.ex b/lib/ash/action_input.ex index bfc23450..2e8600c8 100644 --- a/lib/ash/action_input.ex +++ b/lib/ash/action_input.ex @@ -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(), diff --git a/lib/ash/actions/action.ex b/lib/ash/actions/action.ex index 4d188f6d..6f58e67f 100644 --- a/lib/ash/actions/action.ex +++ b/lib/ash/actions/action.ex @@ -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) diff --git a/test/actions/basic_test.exs b/test/actions/basic_test.exs index 18a944b0..ac74b008 100644 --- a/test/actions/basic_test.exs +++ b/test/actions/basic_test.exs @@ -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