Error shapes and actions

v0.1.4

Every failure in GuardedStruct is a map inside a list: {:error, [map, ...]}. This page lists the shapes those maps take and every action that is not the name of a derive op. For the actions of derive ops, see Validate ops.

Shapes

ShapeKeysProduced by
Field errorfield, action, messageEvery check on one field.
Nested errorfield, errorsA failed sub_field, struct: or structs: value. errors holds the inner error maps. The map has no action.
Conditional errorfield, action (conditionals), errorsA conditional_field where no child accepted the value.

A field error can carry extra keys.

KeyPresent when
__hint__The error comes from a child of a conditional field that has a hint:.
__index__The error comes from a list item checked by each. It holds the zero-based position.
field_pathThe action is domain_parameters. It holds the path that failed.
keyThe error comes from a pattern-keyed map. It holds the key.

Actions

ActionMeaningFieldDefault message
required_fieldsA key in enforce_keys is missing. One error per missing key.The key.Please submit required fields.
authorized_fieldsThe input has a key that is not a field, and authorized_fields: true is set. One error per key.The key.Unauthorized keys are present in the sent data.
bad_parametersThe input is not a map or one of the accepted tuples, a conditional child cannot build a non-map, or Validate.run/2 got a derive string it cannot read or with a wrong operand. In that last case the field is :__value__ and the message names the problem.:__root__Your input must be a map or list of maps
dependent_keysAn on: path has no value.The field with on:.The required dependency for field <field> has not been submitted. and, on a second line, You must have field <last key of the path> in your input.
domain_parametersA domain: rule failed.The field with domain:.Based on field <field> input you have to send authorized data, or the same text ending in and required key when a required path is missing.
validatorA validator: function or validator/2 returned {:error, field, message}.The field.Your message.
conditionalsNo child of a conditional field accepted the value.The conditional field.No message. The inner errors carry theirs.
typeAn op name is not defined, or an op got a kind of value it does not handle.The field.Unexpected type error in <field> field
eacheach=[...] got a value that is not a list.The field.One or more items in the <field> field failed validation
eitherNo inner op of either=[...] passed.The field.None of the conditions for checking the <field> field is not correct
customA custom=[Module, function] function did not return true.The field.The condition for checking the <field> field is not correct
key_patternA key of a pattern-keyed map matches no pattern.:__map__key "<key>" does not match any declared pattern
unknown_fieldValidate.field/3,4 got a name that is not a field.The name.field :<name> is not defined on <Module>

The messages of key_pattern and unknown_field, the input must be a map message of Validate.partial/2, and the bad_parameters message of Validate.run/2, are fixed English text. The other messages come from GuardedStruct.Messages and can be replaced, as Handle and translate errors shows.

Fields

  • :__root__ is the field of an error that is not about one field, such as bad_parameters.
  • :__value__ is the field name used by Validate.run/2, because it has no module to take a name from.
  • :__map__ is the field of errors about a whole pattern-keyed map, such as key_pattern and a validator: error of a pattern field.

Order

  • Checks that run before nested values are built stop the build, and only their errors come back. See How the builder works.
  • When several ops of one field fail, there is one error per op, with the last op first.
  • The order of errors that belong to different fields is not defined.