How to validate if any value in a given list is empty?

Hi, I’m learning Elixir and trying to make a custom changeset validation to check if a given list has any empty values.

I am piping this into my changeset
|> validate_array_field(:array_field)

And here is where I want to do the validation

defp validate_array_field(changeset, field, opts \\ []) do
    # ???
end

I can’t figure out a way to validate if ANY value in the list is empty. For example,

["a", "b", "", "c"] should return errors because one value is empty

["a", "b", " ", "c"] should also return errors even if there is whitespace

Only when the array field looks like this: ["a", "b", "c", "d"] it should pass validation.

How would I be able to do this?

Use Ecto.Changeset.validate_change/3 and inside callback function check if Enum.any?(list, fn element -> element == "" end) for response with an error.

4 Likes