Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format required checks for relationships #299

Conversation

matt-glover
Copy link
Contributor

This PR updates the JSON:API format verification plug to include checks for the relationships key when creating and updating resources.

This is intended to address: #294

Additionally this PR:

  1. Adjusts the list behavior in ErrorView.send_error/2 because I don't think it extracted the status code previously
  2. Added support for serializing a list of formatting errors to handle multiple malformed relationship entries

@matt-glover matt-glover requested a review from a team as a code owner June 28, 2023 14:14
lib/jsonapi/error_view.ex Outdated Show resolved Hide resolved
@spec send_error(Plug.Conn.t(), term()) :: term()
def send_error(conn, %{errors: [%{status: status}]} = error),
do: send_error(conn, status, error)

def send_error(conn, %{errors: errors} = error) when is_list(errors) do
status = Enum.max_by(errors, &Map.get(&1, :status))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best I can tell this was not called on the latest stable version prior to this PR. I think this is a safe change as long as it works in the new workflows.

lib/jsonapi/plugs/format_required.ex Show resolved Hide resolved
test/jsonapi/plugs/format_required_test.exs Outdated Show resolved Hide resolved
@mattpolzin mattpolzin self-requested a review June 29, 2023 17:44
Copy link
Member

@mattpolzin mattpolzin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great improvement to error handling. Excited to merge once we've settled the last couple of things!

lib/jsonapi/error_view.ex Outdated Show resolved Hide resolved
lib/jsonapi/plugs/format_required.ex Show resolved Hide resolved
test/jsonapi/plugs/format_required_test.exs Outdated Show resolved Hide resolved
Uses a new `error_attrs` type to represent the raw output of
`build_error`. This is the output of some of the newer error functions.

Also includes an `error_attrs` suffix on those functions to highlight
that they return the raw `build_error` output structure instead of the
JSON:API-friendly error structure returned by most of the other
ErrorView functions.
@matt-glover
Copy link
Contributor Author

Best I can tell some sort of build cache error caused linting to fail. Seems to lint fine for me locally.

Copy link
Member

@mattpolzin mattpolzin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Big improvement to error handling in the given malformed relationship cases.

@mattpolzin mattpolzin merged commit 6b7f3ef into beam-community:main Jul 3, 2023
16 checks passed
@matt-glover matt-glover deleted the format-required-check-relationships branch July 3, 2023 17:30
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.

None yet

2 participants