anagrius

anagrius

Hi,

I have a a complex set of policies and validations in my resource. I would like to enable or disable a button depending on if:

A. The record is validate
B. The policies allow the actor to perform the action.

When I was initially starting out I had implemented much of the validation logic as policies.
Now I have migrated many of the checks to validation, as that is what they really are. Violations would bring the data into an illegal state.

The issue is that now my:

<button disabled={
    Resource.can_perform_action?(@user, @resource)} >...</button>

Always returns true as the actor is allowed even if the resource is not valid.
I assume there is a function for a dry-run validation of the action, but I cannot find it.

<button disabled={
  Resource.valid_for_action?(@user, @resource) 
  && Resource.can_perform_action?(@user, @resource)} >...</button>

Something like that. Am I off on the wrong path, should the can_perform_action? also be performing validations?

Showing Posts 1 to 10

olivermt

olivermt

No, what you want here is a calculated property that tells you if this is allowed or not I think.
This calculation should/could probably share code with your policies to not repeat yourself.

This was discussed on slack or discord the other day, but I can’t find it now.

anagrius

anagrius OP

Hm, maybe you could give an example of how that might look?

There are like 10-20 validations in some actions that feed into if an action being valid or not.

zachdaniel

zachdaniel

Creator of Ash

The way to run validations would be to build a changeset or query for the action and check query_or_changeset.valid?

However, what you’re looking to do can make sense as a new option to can. I’ve pushed support for this to ash main if you’d like to try it out.

can_perform_action?(@user, @resource, validate?: true)

olivermt

olivermt

I absolutely misread your code (and misunderstood your intention). Apologies

anagrius

anagrius OP

Thanks Zach, that is exactly what I am looking for. I will try it out and report back.

anagrius

anagrius OP

So, I had a change to test out :validate? on the main branch, and it almost does what I would want except it actually raises an error in case of validation errors.

  (elixir 1.17.2) lib/process.ex:864: Process.info/2
  (ash 3.4.20) lib/ash/error/changes/invalid_attribute.ex:4: Ash.Error.Changes.InvalidAttribute.exception/1
  (ash 3.4.20) lib/ash/changeset/changeset.ex:2988: anonymous fn/3 in Ash.Changeset.do_validation/5
  (ash 3.4.20) lib/ash/changeset/changeset.ex:2968: Ash.Changeset.do_validation/5
  (elixir 1.17.2) lib/enum.ex:2531: Enum."-reduce/3-lists^foldl/2-0-"/3
  (ash 3.4.20) lib/ash/changeset/changeset.ex:2329: Ash.Changeset.run_action_changes/6
  (ash 3.4.20) lib/ash/changeset/changeset.ex:1819: Ash.Changeset.do_for_action/4

I would probably expect it to just answer false, and in cases return_errors? is set i get the list so I can display to they user why it cannot perform an action.

It might an idea to slight change can_* functions to be either validation, authorization or both. Something like checks: [:validation, :authorization], and a return_errors?` flag.
I could see use cases for seeing if an action COULD be done validly if the user had permission, or if the action was permitted.

On the other hand it might just as well be two functions authorized?() and valid?() at that point maybe.

zachdaniel

zachdaniel

Creator of Ash

Ah, right that isn’t what I was going for. Can you update to latest main? I’ve made it treat invalid as a false result.

anagrius

anagrius OP

Works like a charm now :heart:

It would be very cool to be able to display “why” an button is disabled for validations as well just having the disabled button is not nice UX. I don’t know if can_* should be the functions for that though… In any case this is already much nicer to use.

zachdaniel

zachdaniel

Creator of Ash

If you use can instead of can? you can get the error back, which will be Forbidden for forbidden errors and invalid for changeset errors.

onetwothree

onetwothree

Hey, I was looking for some docs regarding it and I can find nothing if I search for can_perform_action in ash organisation, could anyone point me to proper place?

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews