jdobrzanski

jdobrzanski

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.

Showing Posts 1 to 5

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
peerreynders

peerreynders

I think that is usually referred to as cross-field validation (in contrast to field level validation).

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

+1


Ecto.Changeset.get_field/3:

While get_change/3 only looks at the current changes to retrieve a value, this function looks at the changes and then falls back on the data, finally returning default if no value is available.

jdobrzanski

jdobrzanski OP

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.

OvermindDL1

OvermindDL1

Or go the python way and add a Future module built-in to Elixir or so that when used like use Future, otp: 21 then it compiles with OTP 21 features or so, though this might require the Elixir source itself to be built for multiple OTP versions and to load them based on the declaration (thus the use Future, ... bit might have to be in the mix.exs file?).

— 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
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
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
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
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
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

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
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