Ash.ActionInput (ash v3.5.9)

View Source

Input for a custom action

Much like an Ash.Query and Ash.Changeset are used to provide inputs into CRUD actions, this struct provides the inputs required to execute a generic action.

Summary

Functions

Add an error to the errors list and mark the action input as invalid.

Fetches the value of an argument provided to the input or :error.

Creates a new input for a generic action

Gets the value of an argument provided to the input.

Create a new action input from a resource.

Set an argument value

Deep merges the provided map into the input context that can be used later

Sets a private argument value

Set the tenant to use when calling the action.

Types

t()

@type t() :: %Ash.ActionInput{
  action: Ash.Resource.Actions.Action.t() | nil,
  arguments: map(),
  context: map(),
  domain: Ash.Domain.t(),
  errors: term(),
  invalid_keys: MapSet.t(),
  params: map(),
  resource: Ash.Resource.t(),
  tenant: term(),
  to_tenant: term(),
  valid?: boolean()
}

Functions

add_error(input, errors, path \\ [])

@spec add_error(
  t(),
  Ash.Error.error_input() | [Ash.Error.error_input()],
  Ash.Error.path_input()
) :: t()

Add an error to the errors list and mark the action input as invalid.

See Ash.Error.to_ash_error/3 for more on supported values for error

fetch_argument(input, argument)

@spec fetch_argument(t(), atom() | String.t()) :: {:ok, term()} | :error

Fetches the value of an argument provided to the input or :error.

Examples

iex> Post
...> |> Ash.ActionInput.for_action(:example, %{arg: "banana"})
...> |> Ash.ActionInput.fetch_argument(:arg)
{:ok, "banana"}

iex> Post
...> |> Ash.ActionInput.for_action(:example, %{})
...> |> Ash.ActionInput.fetch_argument(:banana)
:error

for_action(resource_or_input, action, params, opts \\ [])

@spec for_action(
  resource_or_input :: Ash.Resource.t() | t(),
  action :: atom(),
  params :: map(),
  opts :: Keyword.t()
) :: t()

Creates a new input for a generic action

Options

  • :domain (Ash.Domain) - The domain to use for the action. The resource's domain is used by default.

  • :context (map/0) - Context to set on the action input. The default value is %{}.

  • :authorize? - Whether or not to run authorization on the action. Default behavior of this option is controlled by the domain.

  • :tenant (term/0) - The tenant to use for the action.

  • :actor (term/0) - The actor performing the action

  • :skip_unknown_inputs - A list of unknown inputs to skip. Use :* to skip all unknown inputs.

  • :tracer (term/0) - A tracer or list of tracers to trace action execution.

  • :private_arguments (map/0) - A list of private arguments to be set before the action is invoked. The default value is %{}.

Examples

iex> Post
...> |> Ash.ActionInput.for_action(:example, %{})
...> |> then(& &1.action.name)
:example

get_argument(input, argument)

@spec get_argument(t(), atom() | String.t()) :: term()

Gets the value of an argument provided to the input.

Example

iex> Post
...> |> Ash.ActionInput.for_action(:example, %{arg: "banana"})
...> |> Ash.ActionInput.get_argument(:arg)
"banana"

new(resource, domain \\ nil)

@spec new(Ash.Resource.t(), Ash.Domain.t()) :: t()

Create a new action input from a resource.

Examples

iex> Ash.ActionInput.new(Post)
%Ash.ActionInput{resource: Post}

iex> Ash.ActionInput.new(Post, Domain)
%Ash.ActionInput{resource: Post, domain: Domain}

set_argument(input, argument, value)

@spec set_argument(input :: t(), name :: atom(), value :: term()) :: t()

Set an argument value

Example

iex> Post
...> |> Ash.ActionInput.for_action(:example, %{})
...> |> Ash.ActionInput.set_argument(:arg, "banana")
...> |> Ash.ActionInput.get_argument(:arg)
"banana"

set_context(input, map)

@spec set_context(t(), map() | nil) :: t()

Deep merges the provided map into the input context that can be used later

Do not use the private key in your custom context, as that is reserved for internal use.

Example

iex> Post
...> |> Ash.ActionInput.new()
...> |> Ash.ActionInput.set_context(%{favourite_fruit: :banana})
...> |> then(& &1.context.favourite_fruit)
:banana

set_private_argument(input, name, value)

@spec set_private_argument(input :: t(), name :: atom(), value :: term()) :: t()

Sets a private argument value

Example

iex> Post
...> |> Ash.ActionInput.for_action(:example, %{})
...> |> Ash.ActionInput.set_private_argument(:private_arg, "banana")
...> |> Ash.ActionInput.get_argument(:private_arg)
"banana"

set_tenant(input, tenant)

@spec set_tenant(t(), Ash.ToTenant.t()) :: t()

Set the tenant to use when calling the action.

Example

iex> Post
...> |> Ash.ActionInput.new()
...> |> Ash.ActionInput.set_tenant("banana")
...> |> then(& &1.tenant)
"banana"