AbsintheErrorPayload.ChangesetParser (AbsintheErrorPayload v1.2.0) 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 single AbsintheErrorPayload.ValidationMessage struct from a changeset.
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
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)
Generate a list of AbsintheErrorPayload.ValidationMessage structs from changeset errors
For examples, please see the test cases in the github repo.
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
:cast- generated byEcto.Changeset.cast/3:association- generated byEcto.Changeset.assoc_constraint/3,Ecto.Changeset.cast_assoc/3,Ecto.Changeset.put_assoc/3,Ecto.Changeset.cast_embed/3,Ecto.Changeset.put_embed/3:acceptance- generated byEcto.Changeset.validate_acceptance/3:confirmation- generated byEcto.Changeset.validate_confirmation/3:length- generated byEcto.Changeset.validate_length/3when the:isoption fails validation:min- generated byEcto.Changeset.validate_length/3when the:minoption fails validation:max- generated byEcto.Changeset.validate_length/3when the:maxoption fails validation:less_than_or_equal_to- generated byEcto.Changeset.validate_length/3when the:less_than_or_equal_tooption fails validation:less_than- generated byEcto.Changeset.validate_length/3when the:less_thanoption fails validation:greater_than_or_equal_to- generated byEcto.Changeset.validate_length/3when the:greater_than_or_equal_tooption fails validation:greater_than- generated byEcto.Changeset.validate_length/3when the:greater_thanoption fails validation:equal_to- generated byEcto.Changeset.validate_length/3when the:equal_tooption fails validation:exclusion- generated byEcto.Changeset.validate_exclusion/4:inclusion- generated byEcto.Changeset.validate_inclusion/4:required- generated byEcto.Changeset.validate_required/3:subset- generated byEcto.Changeset.validate_subset/4:unique- generated byEcto.Changeset.unique_constraint/3:foreign- generated byEcto.Changeset.foreign_key_constraint/3:no_assoc_constraint- generated byEcto.Changeset.no_assoc_constraint/3:unknown- supplied when validation cannot be matched. This will also match any custom errors added throughEcto.Changeset.add_error/4,Ecto.Changeset.validate_change/3, andEcto.Changeset.validate_change/4