Skip to content

Commit

Permalink
improvement: validate require_attributes and allow_nil_input at c…
Browse files Browse the repository at this point in the history
…ompile time
  • Loading branch information
zachdaniel committed Jun 10, 2024
1 parent 22b5c64 commit d5d0b04
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
47 changes: 47 additions & 0 deletions lib/ash/resource/transformers/default_accept.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
4 changes: 0 additions & 4 deletions test/actions/identity_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d5d0b04

Please sign in to comment.