Ash FrameworkAF
Ash Framework5mo ago
10 replies
E

How to run custom resource extension before validators

Hi there,

I'm writing my first Ash.Resource extension and having some issues I don't know how to solve. This is my simple extension so far:

defmodule EKG.Extensions.ScenePlaceable do
  use Spark.Dsl.Extension, transformers: [EKG.Extensions.ScenePlaceable.AddAttributes]
end

defmodule EKG.Extensions.ScenePlaceable.AddAttributes do
  use Spark.Dsl.Transformer

  def transform(dsl_state) do
    dsl_state
    |> Ash.Resource.Builder.add_new_attribute(:x, :integer,
      description: "X coordinate position",
      allow_nil?: false,
      public?: true,
      constraints: [min: 0]
    )
    # ... other attrs
  end
end


All it does is add some attributes to the resource. But when I add EKG.Extensions.ScenePlaceable to the list of extensions of my resource I'm now getting this compile error:

== Compilation error in file lib/ekg/scenes/scene_widget.ex ==
** (Spark.Error.DslError) [EKG.Scenes.SceneWidget]
actions -> update -> accept:
  Cannot accept [:x, :y, :width, :height, :z_index], because they are not attributes."


So looks like that validation is running before my extension has a chance to add the attributes? As far as I'm aware the docs don't mention how to configure an extension to get around this issue, but I'm sure the fix is easy. Thank you for your help!
Solution
You can do this in your transformer:

def before?(Ash.Resource.Transformers.DefaultAccept), do: true
def before?(_), do: false
Was this page helpful?