lpmay
lpmay
AEAsh Elixir
Created by lpmay on 7/18/2023 in #support
Confused about `one_of` vs `attribute_equals`
Still messing around and encountered a confusing behavior with validations. I have a :verify action on a resource, which I only want to run on resources which have :role set to :unverified The below works as expected (produces valid/invalid changesets as expected):
update :verify do #custom action to verify an unverified user
validate attribute_equals(:role, :unverified) #only verify unverified users
change set_attribute(:role, :verified)
accept []
end
update :verify do #custom action to verify an unverified user
validate attribute_equals(:role, :unverified) #only verify unverified users
change set_attribute(:role, :verified)
accept []
end
What confuses me is that the below version using one_of instead of attribute_equals seems to me like it should do the same thing, but it never seems to produce an invalid changeset:
update :verify do #custom action to verify an unverified user
validate one_of(:role, [:unverified]) #only verify unverified users
change set_attribute(:role, :verified)
accept []
end
update :verify do #custom action to verify an unverified user
validate one_of(:role, [:unverified]) #only verify unverified users
change set_attribute(:role, :verified)
accept []
end
Can someone point out what I am misunderstanding?
36 replies
AEAsh Elixir
Created by lpmay on 7/17/2023 in #support
Can You Add Additional Attributes to ash_authentication_phoenix?
I went through the tutorial for and have password/email auth working. I'd like to add a role attribute to my user resource so I did the below:
attributes do
uuid_primary_key :id
attribute :email, :ci_string, allow_nil?: false
attribute :hashed_password, :string, allow_nil?: false, sensitive?: true
attribute :role, :atom do
constraints [one_of: [:admin, :user]]
default :user
allow_nil? false
end
end
attributes do
uuid_primary_key :id
attribute :email, :ci_string, allow_nil?: false
attribute :hashed_password, :string, allow_nil?: false, sensitive?: true
attribute :role, :atom do
constraints [one_of: [:admin, :user]]
default :user
allow_nil? false
end
end
At runtime, the form seems to have errors like this:
ERROR: column u0.role does not exist at character 51
ERROR: column u0.role does not exist at character 51
Is the intent that I can have arbitrary attributes on a user resources used with Ash Authentication like this, or is there another approach I should be looking into? Ultimately, I'd like to add some code to seeds.exs which creates a couple :admin accounts and gate certain features behind only that role, but I'm very new to Ash, Phoenix and Elixir so I'm not sure if I'm heading in the right general direction or not.
6 replies