mysterymt

mysterymt

I’m continuing to work on a small Ash project and have a situation where I’m building out a validator to verify a user’s password before allowing changes to be made on the User’s password. However, if there are other errors on the form, I don’t want it running the hashing algorithm since it’s expensive. The action on the resource looks something like this:

update :update_password do
  # ... code here

  # Run validations
  validate {Validators.VerifyPassword, password_argument: :current_password}

  # ... more code here
end

When doing it this way, I have to check if the changeset is valid inside the validator itself before running. Something along the lines of this:

@impl true
def validate(%{valid?: false}, _opts, _context), do: :ok

@impl true
def validate(changeset, opts, _context) do
  # ... do actual validation here
end

The problem with this is I’m returning :ok even though the validation hasn’t been run, which feels wrong to me. I know there are where clauses that can be run like so:

# Run validations
validate {Validators.VerifyPassword, password_argument: :current_password} do
  where # ... some condition here
end

My question is, is there way to make that where clause check to see if the changeset itself is valid before running this validation? That feels a little more clean and logical to me than what I’m currently doing in the validator module for it. I checked the documentation and there are a few examples of using where, but I still don’t fully understand all the possible options for it are. Would I have to create another validator that checks to see if the changeset is valid and then have the where run that validator? Is there something easier and already built into Ash that I can use?

Showing Posts 1 to 1

zachdaniel

zachdaniel

Creator of Ash

You could do it in the way you described yes, but there is an option for it, only_when_valid?: true. This can be added to both validations and changes. In 4.0 we may swap the default of that (and provide an upgrade script that preserves the old behavior for all existing validations and changes).

See the docs for more :grin:

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews