mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 05:23:03 +12:00
fix: handle invalid input in basic actions
This commit is contained in:
parent
2e543bc212
commit
2843224f06
3 changed files with 27 additions and 1 deletions
|
@ -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(),
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue