AbsintheErrorPayload.ChangesetParser (AbsintheErrorPayload v1.1.4) View Source

Converts an ecto changeset into a list of validation errors structs. Currently does not support nested errors

Link to this section Summary

Functions

Generate a list of AbsintheErrorPayload.ValidationMessage structs from changeset errors

Inserts message variables into message. Code inspired by Phoenix DataCase.on_errors/1 boilerplate.

Generate unique code for each validation type.

Link to this section Functions

Link to this function

construct_message(field, error_tuple)

View Source

Generate a single AbsintheErrorPayload.ValidationMessage struct from a changeset.

This method is designed to be used with Ecto.Changeset.traverse_errors to generate a map of structs.

## Examples

```
error_map = Changeset.traverse_errors(fn(changeset, field, error) ->
  AbsintheErrorPayload.ChangesetParser.construct_message(field, error)
end)
error_list = Enum.flat_map(error_map, fn({_, messages}) -> messages end)

```
Link to this function

extract_messages(changeset)

View Source

Generate a list of AbsintheErrorPayload.ValidationMessage structs from changeset errors

For examples, please see the test cases in the github repo.

Link to this function

interpolate_message(arg)

View Source

Inserts message variables into message. Code inspired by Phoenix DataCase.on_errors/1 boilerplate.

Examples

iex> interpolate_message({"length should be between %{one} and %{two}", %{one: "1", two: "2", three: "3"}})
"length should be between 1 and 2"
iex> interpolate_message({"is already taken: %{fields}", %{fields: [:one, :two]}})
"is already taken: one,two"

Generate unique code for each validation type.

Expects an array of validation options such as those supplied by Ecto.Changeset.traverse_errors/2, with the addition of a message key containing the message string. Messages are required for several validation types to be identified.

Supported