jdobrzanski

jdobrzanski

Validating changeset params with the existing fields

My question is around Ecto.Changeset and the need to validate entire models, not just the passed in params. Basically, I’ve found a useful pattern is to write a set of validations; for a specific action; against the model after the params are filter; and after the params are applied. The first two items were awkward to do in rails 4 while the later easy. So if a model is loaded from the database, the passed in params in combination to the fields already set on the model (or perhaps in the session) are what are validated. Validating just the passed in params is also useful (eg. enforcing that the password was sent along with the sensitive account update request) but done less often.

Ecto.Changest is exciting because it decouples the changes from the model, however, most of the validations run only against the params, not the fields in the data after the params are applied. What I want is the option to validate_change, etc using not get_change nor really even get_field but just the data field after the params are filtered and cast. Obviously if the changes make the model invalid then the model won’t be saved (and one could image a copy of the un-modified struct along with the modified version anyways). Technically using get_field could work as well.

Rewriting new functions that apply the changes and then validate the model seem to not be in spirit with Ecto.Changeset and I wish there is a solution that could work with existing models and libraries that leverage it.
I am looking for thoughts around how these types of changes are meant to be handled in elixir (especially during a typical request to update a resource in Phoenix) and how that pattern can fit into this. Radical departures to what I am looking for are great to hear but having a specific answer is also appreciated.

Most Liked

LostKobrakai

LostKobrakai

get_change(changeset, :field) is basically the same as changeset |> apply_changes() |> Map.get(:field), so I’m not really sure what you’re missing for it. Also Ecto.Changeset does validate just the changes, as the idea is that a given source struct is already valid and only the params need to be validated, but you can just as well validate any given change against it’s consequences for the validity of the whole struct.

NobbZ

NobbZ

Can you give a more concrete example of what you want to achieve but currently is not possible?

I’m not sure if I understand what you want to say.

But it sounds as if you want to validate if a combination of fields is still valid even when only one has been changed?

Lets say the sum of :a and :b always have to be 10?

Shoouldn’t that be easily possible by a custom validator?

Something like:

if get_field(cs, :a) + get_field(cs, :b) != 10 do
  cs
  |> add_error(:a, "Must sum to 10", [other: :b])
  |> add_error(:b, "Must sum to 10", [other: :a])
else
  cs
end
jdobrzanski

jdobrzanski

Thanks for the help! I’ve had to move ahead today with progress so I’m basically taking @NobbZ advice. Soon I want to take the time to post concrete examples with embedded form schemas, ecto schemas, hybrids, etc. Once all of the examples are laid out it should be clear what the general pattern that I am looking for is that works well with each example.

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
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vegabook
I'm brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41454 115
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30840 112
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

We're in Beta

About us Mission Statement