ja_serializer v0.12.0 JaSerializer.Params

Functions to help when working with json api params.

Summary

Functions

Takes the entire params passed in, and merges relationships and attributes

Functions

to_attributes(data)

Takes the entire params passed in, and merges relationships and attributes.

Note, this expects to receive the json api “data” param.

Example functionality:

JaSerializer.Params.to_attributes(%{
  "type" => "person",
  "attributes" => %{"first" => "Jane", "last" => "Doe", "type" => "anon"},
  "relationships" => %{"user" => %{"data" => %{"id" => 1}}}
})
%{
  "first" => "Jane",
  "last" => "Doe",
  "type" => "anon",
  "user_id" => 1
}

Example usage:

def create(conn, %{"data" => data}) do
  %Comment{}
  |> Comment.changeset(create_params(data))
  |> Repo.insert
  |> case do
    {:ok, my_model} ->
      # etc
    {:error, changeset} -> etc
      # etc
  end
end

defp create_params(data) do
  data
  |> JaSerializer.Params.to_attributes
  |> Map.take(["name", "body", "post_id"])
end