View Source Peri.Error (peri v0.2.11)
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.
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
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
}
]
}
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"}
}
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 nestedPeri.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"}]
}
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"}
}