View Source Peri.Error (peri v0.2.6)

Defines the structure and functions for handling validation errors in the Peri schema validation library.

The Peri.Error module encapsulates information about validation errors that occur during schema validation. Each error contains details about the path to the invalid data, the type of error, and any nested errors for complex or deeply nested schemas.

Attributes

  • :message - A human-readable message describing the error.
  • :content - Additional information about the error, such as expected and actual values.
  • :path - A list representing the path to the invalid data within the structure being validated.
  • :key - The specific key or field that caused the error.
  • :errors - A list of nested Peri.Error structs for detailed information about nested validation errors.

Example

iex> error = %Peri.Error{
...>   message: "Validation failed",
...>   content: %{expected: :string, actual: :integer},
...>   path: [:user, :age],
...>   key: :age,
...>   errors: [
...>     %Peri.Error{
...>       message: "Expected type string, got integer",
...>       content: nil,
...>       path: [:user, :age],
...>       key: :age,
...>       errors: nil
...>     }
...>   ]
...> }
%Peri.Error{
  message: "Validation failed",
  content: %{expected: :string, actual: :integer},
  path: [:user, :age],
  key: :age,
  errors: [
    %Peri.Error{
      message: "Expected type string, got integer",
      content: nil,
      path: [:user, :age],
      key: :age,
      errors: nil
    }
  ]
}

Functions

Summary

Functions

Recursively converts a Peri.Error struct into a map.

Creates a new child error with a path, key, message, and context.

Creates a new parent error with nested errors.

Creates a new single error with a formatted message and context.

Types

@type t() :: %Peri.Error{
  content: keyword(),
  errors: [t()] | nil,
  key: atom() | nil,
  message: String.t(),
  path: [atom()]
}

Functions

Recursively converts a Peri.Error struct into a map.

Parameters

Examples

iex> error = %Peri.Error{
...>   message: "Validation failed",
...>   content: %{expected: :string, actual: :integer},
...>   path: [:user, :age],
...>   key: :age,
...>   errors: [
...>     %Peri.Error{
...>       message: "Expected type string, got integer",
...>       content: nil,
...>       path: [:user, :age],
...>       key: :age,
...>       errors: nil
...>     }
...>   ]
...> }
iex> Peri.Error.error_to_map(error)
%{
  message: "Validation failed",
  content: %{expected: :string, actual: :integer},
  path: [:user, :age],
  key: :age,
  errors: [
    %{
      message: "Expected type string, got integer",
      content: nil,
      path: [:user, :age],
      key: :age,
      errors: nil
    }
  ]
}
Link to this function

format_error_message(reason, context)

View Source
Link to this function

new_child(path, key, message, context)

View Source

Creates a new child error with a path, key, message, and context.

Parameters

  • path - A list representing the path to the invalid data.
  • key - The specific key or field that caused the error.
  • message - A string template for the error message.
  • context - A list of key-value pairs to replace in the message template.

Examples

iex> Peri.Error.new_child([:user], :age, "Invalid value for %{field}", [field: "age"])
%Peri.Error{
  path: [:user, :age],
  key: :age,
  message: "Invalid value for age",
  content: %{field: "age"}
}
Link to this function

new_parent(path, key, errors)

View Source

Creates a new parent error with nested errors.

Parameters

  • path - A list representing the path to the invalid data.
  • key - The specific key or field that caused the error.
  • errors - A list of nested Peri.Error structs.

Examples

iex> Peri.Error.new_parent([:user], :age, [%Peri.Error{message: "Invalid age"}])
%Peri.Error{
  path: [:user, :age],
  key: :age,
  errors: [%Peri.Error{message: "Invalid age"}]
}
Link to this function

new_single(message, context)

View Source

Creates a new single error with a formatted message and context.

Parameters

  • message - A string template for the error message.
  • context - A list of key-value pairs to replace in the message template.

Examples

iex> Peri.Error.new_single("Invalid value for %{field}", [field: "age"])
%Peri.Error{
  message: "Invalid value for age",
  content: %{field: "age"}
}
Link to this function

update_error_paths(error, new_path)

View Source