amos-kibet

amos-kibet

Ash Policies: `nil` is not a Spark DSL module

I have a FilterCheck policy that looks like this:

use Ash.Policy.FilterCheck

 def filter(actor, context, options) do
    tenant_id = context.subject.tenant.id

    resource_name =
      options[:resource]
      |> to_string()
      |> String.split(".")
      |> List.last()

    expr(
      exists(
        MyApp.AccessRight,
        tenant_id == ^tenant_id and
          resource_name == ^resource_name and read == true)
      )
    )

    # dbg()
  end

and I use this policy in my resource this way:

  policies do
    policy action(:list_paginated) do
      authorize_if MyFilterCheckPolicy
    end
  end

and here is how I use the resource action in my LiveView:

Posts.list_paginated(actor: current_user, tenant: tenant)

The problem:
For some reason, this setup does not work; it raises this error:

[error] ** (ArgumentError) `nil` is not a Spark DSL module.

    nil.persisted(:data_layer)
    (spark 2.2.31) lib/spark/dsl/extension.ex:138: Spark.Dsl.Extension.persisted!/3
    (ash 3.4.21) lib/ash/filter/filter.ex:3734: Ash.Filter.do_hydrate_refs/2
    ... more stacktraces below

and interestingly, it works if I uncomment the dbg() call at the end of the policy (see code above).

How can I fix the above error?

Most Liked

zachdaniel

zachdaniel

Creator of Ash

exists does not take a resource it takes a relationship or relationship oath.

i.e exists(access_rights, ....

or

exists(access_rights.user, ...

Please open an issue describing that bad error output and I will look to make it clearer.

zachdaniel

zachdaniel

Creator of Ash

Ah, sorry I just looked more closely. You can’t use exists with anything other than a relationship reference. ^value is not a relationship reference.

Here is the related issue for the improvement we’d like to make. Support resources as aggregate targets in expressions · Issue #939 · ash-project/ash · GitHub

So to do this logic that you want you’ll need to do the work entirely in the check module. and return {:ok, true | false}

almirsarajcic

almirsarajcic

Since I needed to do a filter check instead of a simple check, I ended up with a manual relationship and with more knowledge about the powers of Ash. Mind getting blown every day… :exploding_head:

Thanks!

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement