View Source Peri.Error (peri v0.2.3)
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 nestedPeri.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
error_to_map/1
- Converts aPeri.Error
struct to a map, including nested errors.
Summary
Functions
Recursively converts a Peri.Error
struct into a map.
Types
Functions
Recursively converts a Peri.Error
struct into a map.
Parameters
error
- APeri.Error
struct to be transformed.
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
}
]
}