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?






















