json_patch v0.8.0 JSONPatch View Source
JSONPatch is an Elixir implementation of the JSON Patch format, described in RFC 6902.
Examples
iex> JSONPatch.patch(%{"a" => 1}, [
...> %{"op" => "add", "path" => "/b", "value" => %{"c" => true}},
...> %{"op" => "test", "path" => "/a", "value" => 1},
...> %{"op" => "move", "from" => "/b/c", "path" => "/c"}
...> ])
{:ok, %{"a" => 1, "b" => %{}, "c" => true}}
iex> JSONPatch.patch(%{"a" => 22}, [
...> %{"op" => "add", "path" => "/b", "value" => %{"c" => true}},
...> %{"op" => "test", "path" => "/a", "value" => 1},
...> %{"op" => "move", "from" => "/b/c", "path" => "/c"}
...> ])
{:error, :test_failed, ~s|test failed (patches[1], %{"op" => "test", "path" => "/a", "value" => 1})|}
Installation
# mix.exs
def deps do
[
{:json_patch, "~> 0.8.0"}
]
end
Link to this section Summary
Functions
Converts an error_type/0 into an HTTP status code
Applies JSON Patch (RFC 6902) patches to the given JSON document.
Returns {:ok, patched_map} or {:error, error_type, description}
Converts a return_value/0 to an HTTP status code
Link to this section Types
Link to this type
error_type()
View Source
error_type() :: :test_failed | :syntax_error | :path_error
Link to this type
json_encodable()
View Source
json_encodable() :: json_object() | json_array() | String.t() | number() | true | false | nil
Link to this type
json_object()
View Source
json_object() :: %{optional(String.t()) => json_encodable()}
Link to this type
return_value()
View Source
return_value() :: {:ok, json_encodable()} | {:error, error_type(), String.t()}
Link to this section Functions
Link to this function
error_type_to_status_code(error_type)
View Source
error_type_to_status_code(error_type()) :: status_code()
Converts an error_type/0 into an HTTP status code.
Examples:
iex> JSONPatch.error_type_to_status_code(:test_failed)
409
iex> JSONPatch.error_type_to_status_code(:path_error)
422
iex> JSONPatch.error_type_to_status_code(:syntax_error)
400
Applies JSON Patch (RFC 6902) patches to the given JSON document.
Returns {:ok, patched_map} or {:error, error_type, description}.
Examples:
iex> %{"foo" => "bar"} |> JSONPatch.patch([%{"op" => "replace", "path" => "/foo", "value" => 2}])
{:ok, %{"foo" => 2}}
iex> %{"foo" => "bar"} |> JSONPatch.patch([%{"op" => "test", "path" => "/foo", "value" => 2}])
{:error, :test_failed, ~s|test failed (patches[0], %{"op" => "test", "path" => "/foo", "value" => 2})|}
iex> %{"foo" => "bar"} |> JSONPatch.patch([%{"op" => "remove", "path" => "/foo"}])
{:ok, %{}}
Link to this function
status_code(return_value)
View Source
status_code(return_value()) :: status_code()
Converts a return_value/0 to an HTTP status code.
Example:
iex> JSONPatch.patch(%{"a" => 1}, [%{"op" => "test", "path" => "/a", "value" => 1}]) |> JSONPatch.status_code
200
iex> JSONPatch.patch(%{"a" => 1}, [%{"op" => "test", "path" => "/a", "value" => 22}]) |> JSONPatch.status_code
409