klo

klo

I have a schema that uses embeds_many and would like it to also allow for the list to be empty if there is no Actors. e.g.

id: "The Matrix",
actors: []

my schema:

    field(:id, :string, primary_key: true)
    embeds_many(:actors, Actor, on_replace: :delete)
    field(:updated_timestamp, :map)
    field(:updated_at, :utc_datetime)
  end

my changeset:

  def changeset(movie, %{} = params, opts \\ []) do
    now = Keyword.get(opts, :now, DateTime.utc_now())

    movie
    |> cast(params, [
      :id
    ])
    |> cast_embed(:actors, with: &Actor.changeset/2, required: true)
    |> Schema.put_updated_bson_timestamp(now)
    |> Schema.put_updated_at(now)
    |> validate_required(@required_fields)
  end

When supplying required: true to cast_embed and the movie does not have any actors, we get a {:error, "actors can not be blank"}. When taking off the required: true the actors field can be missing completely and it is still valid. Is this an edge case where I would have to write my own validation for the :actors field?

Showing Posts 1 to 7

joey_the_snake

joey_the_snake

What kind of issue are you running into by keeping it optional?

embeds_many fields have a default of empty list so if they’re not provided they will be set to []. Is there some kind of edge case you’re running into that isn’t working as expected?

klo

klo OP

i would like to enforce that the field should be there even if it is empty. If required: true is set and the actors list is [] it will say the changeset is invalid because the actors list is empty..

joey_the_snake

joey_the_snake

The field will have a default value of [] because it’s an embeds_many. So if you make it optional it won’t raise and it will be set to [] automatically if not there. This should suffice? Or am I mistaken?

klo

klo OP

When i wrote a test for it and deleted the :actors key from the Movie schema the changeset is still valid but my intentions is for the changeset to not be valid when the :actors key is missing. It should still be valid when actors is a empty list though.

Keeping it optional will allow for the key to not be there at which is not what i am after. However using the require: true i am not able to leave the list as empty as it detects it is empty

joey_the_snake

joey_the_snake

Oh ok I think I see now. You want to protect against someone manually overwriting it to nil? You won’t be able to do it with cast_embed so you’ll have to make your own function. You can use get_field to see if it is nil in the changes or the original data.

klo

klo OP

ahh ok ok ill give it a shot and report back.

klo

klo OP

Ended up writing my own function to check if the field is included inside of the params and validating it that way.

— All posts loaded —

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
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
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
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 & 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