improvement: validate require_attributes and allow_nil_input at compile time

This commit is contained in:
Zach Daniel 2024-06-10 17:43:15 -04:00
parent 22b5c644b8
commit d5d0b04bb6
2 changed files with 47 additions and 4 deletions

View file

@ -72,6 +72,53 @@ defmodule Ash.Resource.Transformers.DefaultAccept do
"""
end
action
|> Map.get(:require_attributes, [])
|> Enum.reject(&(&1 in accept))
|> case do
[] ->
:ok
invalid_required_attributes ->
raise Spark.Error.DslError,
module: Spark.Dsl.Transformer.get_persisted(dsl_state, :module),
path: [:actions, action.name, :require_attributes],
message: """
Cannot require #{inspect(invalid_required_attributes)}, because they are not accepted. You must accept in addition to requiring."
"""
end
action
|> Map.get(:allow_nil_input, [])
|> Enum.reject(&(&1 in accept))
|> case do
[] ->
:ok
invalid_allow_nil_inputs ->
raise Spark.Error.DslError,
module: Spark.Dsl.Transformer.get_persisted(dsl_state, :module),
path: [:actions, action.name, :allow_nil_input],
message: """
It is not necessary to allow nil inputs that are not accepted, got: #{inspect(invalid_allow_nil_inputs)}
"""
end
accept
|> Enum.reject(&Ash.Resource.Info.attribute(dsl_state, &1))
|> case do
[] ->
:ok
invalid_attrs ->
raise Spark.Error.DslError,
module: Spark.Dsl.Transformer.get_persisted(dsl_state, :module),
path: [:actions, action.name, :accept],
message: """
Cannot accept #{inspect(invalid_attrs)}, because they are not attributes."
"""
end
accept =
Enum.reject(accept, &(&1 in argument_names))

View file

@ -32,10 +32,6 @@ defmodule Ash.Test.Actions.IdentityTest do
actions do
default_accept :*
defaults [:read, :destroy, create: :*, update: :*]
create :create_with_required do
require_attributes [:tag]
end
end
calculations do