Skip to content

Guidance for how to render Flop validation errors #582

@davydog187

Description

@davydog187

I'm trying to understand the right way to traverse Flop.Meta.errors() to produce good error messages for my OpenAPI json API. The way in which errors are structured is a bit confusing, and there's not a clear way for how to combine errors and data to get a good error message.

For instance, I have this test

    test "returns error for invalid filter parameters", %{conn: conn, key: key} do
      params = %{
        filters: %{
          0 => %{field: :status, value: :invalid_status}
        }
      }

      conn =
        conn
        |> put_req_header("authorization", "Bearer #{key}")
        |> get(~p"/api/v1/sessions?#{params}")

      assert json_response(conn, 422) == %{
               "error" => "ValidationError",
               "message" => "Invalid pagination parameters",
               "details" => %{
                 "filters" => [
                   "status value \"invalid_status\" is invalid"
                 ]
               }
             }
    end

The %Flop.Meta{} looks like

%Flop.Meta{
  backend: nil,
  current_offset: nil,
  current_page: nil,
  end_cursor: nil,
  next_offset: nil,
  next_page: nil,
  page_size: nil,
  previous_offset: nil,
  previous_page: nil,
  schema: Sauron.Devices.Session,
  start_cursor: nil,
  total_count: nil,
  total_pages: nil,
  errors: [filters: [[value: [{"is invalid", []}]]]],
  flop: %Flop{
    after: nil,
    before: nil,
    first: nil,
    last: nil,
    limit: nil,
    offset: nil,
    order_by: nil,
    order_directions: nil,
    page: nil,
    page_size: nil,
    decoded_cursor: nil,
    filters: []
  },
  has_next_page?: false,
  has_previous_page?: false,
  opts: [for: Sauron.Devices.Session],
  params: %{"filters" => [%{"field" => "status", "value" => "invalid_status"}]}
}

To me, there isn't a clear way to traverse these two data structures (errors and params) to provide a coherent error message back to the user. Ideally there would be a Flop function that could help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions