For some reason `cast` in `Ecto.Changeset` is removing `" "` from an array of strings

For some reason cast in Ecto.Changeset is removing " " from an array

Ecto.Changeset.cast(%Scrabble.Games.Player{ name: nil, user_id: nil, rack: nil, score: 0},  %{
                                            name: "AN Other",
                                            user_id: "019d011c-2f62-7339-8615-175e2228a55d",
                                            rack: ["Z", " ", " ", " ", " ", " ", " "],
                                            score: 0
                                          },
                      [:name, :user_id, :rack, :score], [])
#Ecto.Changeset<
  action: nil,
  changes: %{
    name: "AN Other",
    user_id: "019d011c-2f62-7339-8615-175e2228a55d",
    rack: ["Z"]
  },
  errors: [],
  data: #Scrabble.Games.Player<>,
  valid?: true,
  ...
>

Is there something I am missing in the schema?

@primary_key false
  embedded_schema do
    field :user_id, Ecto.UUID
    field :name, :string
    field :rack, {:array, :string}
    field :score, :integer
  end

Such strings are considered empty values and removed. Check the docs as there is a mechanism to change what is considered an empty value/

2 Likes

Thank you