bypass required attribute check for atomic_set & remove required attribute errors#2547
Closed
rtorresware wants to merge 1 commit intoash-project:mainfrom
Closed
bypass required attribute check for atomic_set & remove required attribute errors#2547rtorresware wants to merge 1 commit intoash-project:mainfrom
rtorresware wants to merge 1 commit intoash-project:mainfrom
Conversation
attribute errors
Contributor
|
We have a better way that we do this for regular atomics, we just need to adopt a similar strategy for create_atomics 😄 We set the expression itself to @doc false
def handle_allow_nil_atomics(changeset, actor) do
changeset.atomics
|> Enum.reduce(changeset, fn {key, value}, changeset ->
attribute = Ash.Resource.Info.attribute(changeset.resource, key)
if attribute.primary_key? do
changeset
else
allow_nil? =
attribute.allow_nil? and attribute.name not in changeset.action.require_attributes
if allow_nil? || not Ash.Expr.can_return_nil?(value) do
value
else
if Ash.DataLayer.data_layer_can?(changeset.resource, :expr_error) do
expr(
if is_nil(type(^value, ^attribute.type, ^attribute.constraints)) do
error(
^Ash.Error.Changes.Required,
%{
field: ^attribute.name,
type: ^:attribute,
resource: ^changeset.resource
}
)
else
^value
end
)
else
{:not_atomic,
"Failed to validate expression #{inspect(value)}: data layer `#{Ash.DataLayer.data_layer(changeset.resource)}` does not support the expr_error"}
end
end
|> case do
{:not_atomic, error} ->
Ash.Changeset.add_error(changeset, error)
value ->
%{changeset | atomics: Keyword.put(changeset.atomics, key, value)}
end
end
end)
|> Ash.Changeset.hydrate_atomic_refs(actor, eager?: true)
|> then(fn
{:not_atomic, value} ->
{:not_atomic, value}
%Ash.Changeset{} = changeset ->
if changeset.action.type == :update do
attributes =
changeset.attributes
|> Map.keys()
|> Enum.reject(&Ash.Resource.Info.attribute(changeset.resource, &1).allow_nil?)
require_values(changeset, :update, false, attributes)
else
changeset
end
end)
endThe correct answer here would be to adapt this. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
My clanker's justification of this change vs a bigger refactor:
Opencode session: https://opncd.ai/share/pWJqbSRE
Regression tests live in ash_postgres PR, as this bug can only be replicated in sql backed data layers: ash-project/ash_postgres#684
Contributor checklist
Leave anything that you believe does not apply unchecked.