fix: show proper error message when trying to accept non-writable attributes

This commit is contained in:
Zach Daniel 2024-08-08 13:47:37 -04:00
parent 793cd715a7
commit e44d97bd25

View file

@ -13,17 +13,22 @@ defmodule Ash.Resource.Verifiers.ValidateAccept do
attribute_names = MapSet.new(attributes, & &1.name)
initial_errors = %{not_attribute: []}
initial_errors = %{not_attribute: [], not_writable: []}
result =
Verifier.get_entities(dsl_state, [:actions])
|> Enum.reduce(%{}, fn
%{name: action_name, accept: accept}, acc ->
validate_attribute_name = fn attribute_name ->
if MapSet.member?(attribute_names, attribute_name) do
:ok
else
:not_attribute
cond do
!MapSet.member?(attribute_names, attribute_name) ->
:not_attribute
!Ash.Resource.Info.attribute(dsl_state, attribute_name).writable? ->
:not_writable
true ->
:ok
end
end
@ -31,11 +36,14 @@ defmodule Ash.Resource.Verifiers.ValidateAccept do
Enum.reduce(
accept,
initial_errors,
fn attribute, %{not_attribute: not_attribute} = acc ->
fn attribute, %{not_attribute: not_attribute, not_writable: not_writable} = acc ->
case validate_attribute_name.(attribute) do
:ok ->
acc
:not_writable ->
%{not_writable: [attribute | not_writable]}
:not_attribute ->
%{not_attribute: [attribute | not_attribute]}
end
@ -53,9 +61,11 @@ defmodule Ash.Resource.Verifiers.ValidateAccept do
end)
|> Enum.map(fn {action, %{accept: accept}} ->
accept_not_attribute = accept.not_attribute |> Enum.reverse()
accept_not_writable = accept.not_writable |> Enum.reverse()
[
message(accept_not_attribute, "are not attributes", [:actions, action, :accept])
message(accept_not_attribute, "are not attributes", [:actions, action, :accept]),
message(accept_not_writable, "are not writable", [:actions, action, :accept])
]
|> Enum.reject(&(&1 == ""))
|> Enum.join("\n")