Ash FrameworkAF
Ash Framework4mo ago
13 replies
burekogorek

Modifying the argument clashes with AshCloak changing the argument's type

Hello! I have a resource Platform with an attribute called credentials I want to have encrypted. This attribute is a union of embedded resources (representing credentials for different platforms).

I have a change that sets the type attribute on the credentials union changeset to the same value as the platform.type is set - to avoid having to pass the same value twice and ensure the credentials type matches the platform type.

After adding the AshCloak encryption, it replaces my argument :credentials, :map with its own argument typed as a union.This causes my change to fail, because:
1. AshCloak's argument expects a properly-typed union value
2. My change can't set the type before Ash attempts to cast the argument
3. Without the type field, Ash considers the argument invalid when casting to the union

How can I either apply my change before AshCloak's union casting, or configure AshCloak to somehow work with my existing argument definition?

I was thinking of renaming my argument to credentials_input an setting the value of AshCloaks credentials argument inside the change - but I feel this duplicates the logic and the action now accepts two arguments for credentials which might be confusing.
defmodule Intranet.Synchronizer.Platform do
  cloak do
    ...
    attributes [:credentials]
  end

  actions do
    ...
    create :create do
      accept [:name, :type]
      argument :credentials, :map
    end

    update :update do
      # similar to above
    end
  end

  changes do
    ...
    change {Intranet.Synchronizer.Changes.SetCredentialsType, []}
  end

  attributes do
    uuid_primary_key :id

    attribute :type, Intranet.Synchronizer.Types.PlatformType do
      allow_nil? false
      public? true
    end

    attribute :credentials, Intranet.Synchronizer.Types.PlatformCredentials do
      allow_nil? false
      public? true
    end

    timestamps(public?: true)
  end
end
Was this page helpful?