Skip to content

BadMapError raised when param is not a map but filter specifies it is #2

@antedeguemon

Description

@antedeguemon

Description

When I specify a filter for a map and a string is passed instead, a BadMapError is raised. I think a strong_params error struct should be returned instead.

Scenario

Controller:

defmodule InvalidMapController do
  use Phoenix.Controller, namespace: StrongParams
  use StrongParams

  filter_for(:create, required: [image: [:name, :size]])
end

Request (as in a test):

test "invalid map", %{conn: conn} do
  conn = %{conn | params: %{"image" => "not a map"}}

  InvalidMapController.call(conn, :create)
end

And the stack trace is:

test/strong_params/strong_params_test.exs:148
** (BadMapError) expected a map, got: ""
code: result = FailFallbackController.call(conn, :create) |> IO.inspect()
stacktrace:
  (elixir 1.11.1) lib/map.ex:469: Map.get("", "name", :key_not_found)
  (strong_params 0.0.5) lib/strong_params/filter.ex:36: StrongParams.Filter.reduce_function/3
  (elixir 1.11.1) lib/enum.ex:2181: Enum."-reduce/3-lists^foldl/2-0-"/3
  (strong_params 0.0.5) lib/strong_params/filter.ex:30: StrongParams.Filter.apply_filters/4
  (strong_params 0.0.5) lib/strong_params/filter.ex:45: StrongParams.Filter.reduce_function/3
  (elixir 1.11.1) lib/enum.ex:2181: Enum."-reduce/3-lists^foldl/2-0-"/3
  (strong_params 0.0.5) lib/strong_params/filter.ex:30: StrongParams.Filter.apply_filters/4
  (strong_params 0.0.5) lib/strong_params/filter.ex:13: StrongParams.Filter.apply/2
  (strong_params 0.0.5) lib/strong_params/filter_plug.ex:17: StrongParams.FilterPlug.call/2
  test/strong_params/strong_params_test.exs:49: StrongParamsTest.FailFallbackController.phoenix_controller_pipeline/2
  test/strong_params/strong_params_test.exs:150: (test)

Expected behavior

The expected behavior was that strong_params returned a formatted error message saying that the image key is invalid.

%StrongParams.Error{errors: %{image: "is not a map"}, type: "type"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions