Skip to content

Conversation

@Kuret
Copy link
Contributor

@Kuret Kuret commented Jan 14, 2025

Closes #54

Previously, the documentation stated a default of :invalid for the changeset function (and in extension the cast_attachments function).

def changeset(user, params \\ :invalid) do

The cast_attachments function would then check the params for :invalid and return :invalid, which would get passed to the Ecto.Changeset.cast function as the params argument.

waffle_params =
  case params do
    :invalid ->
      :invalid

[...]

Ecto.Changeset.cast(changeset_or_data, waffle_params, allowed)

Since the improved type checking of Elixir 1.18 this causes a typespec warning, since the Changeset.cast function's signature does not specify :invalid as a valid type for the params.

Solved by checking if the params are a non-empty map and returning a proper Changeset error:

case params do
  params when is_map(params) and map_size(params) > 0 ->
    [...]
    Ecto.Changeset.cast(changeset_or_data, waffle_params, allowed)

  _ ->
    Ecto.Changeset.add_error(
      changeset_or_data,
      :missing_attachment,
      "no attachment(s) given to cast"
    )

@Kuret Kuret changed the title Check for non-empty params and return changeset error in cast_attachments Check for non-empty params and return changeset in cast_attachments Jan 14, 2025
@Kuret
Copy link
Contributor Author

Kuret commented Jan 14, 2025

Updated the code, I incorrectly assumed there should always be something in the params to cast, while attachments may as well be optional.

Just returning the changeset as-is if there is nothing to cast now.

@philipgiuliani
Copy link

Works & solves the warning for us!

@achempion achempion merged commit f8c83c1 into elixir-waffle:master Mar 21, 2025
1 check passed
@achempion
Copy link
Member

Thanks for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Elixir 1.18 type checking warning on cast_attachments

3 participants