View Source Jido.Util (Jido v1.0.0)
A collection of utility functions for the Jido framework.
This module provides various helper functions that are used throughout the Jido framework, including:
- ID generation
- Name validation
- Error handling
- Logging utilities
These utilities are designed to support common operations and maintain consistency across the Jido ecosystem. They encapsulate frequently used patterns and provide a centralized location for shared functionality.
Many of the functions in this module are used internally by other Jido modules, but they can also be useful for developers building applications with Jido.
Summary
Functions
Generates a unique ID.
Validates that all modules in a list implement the Jido.Action behavior. Used as a custom validator for NimbleOptions.
Validates the name of a Action.
Validates that a module implements the Jido.Runner behavior.
Functions
@spec generate_id() :: String.t()
Generates a unique ID.
Validates that all modules in a list implement the Jido.Action behavior. Used as a custom validator for NimbleOptions.
This function ensures that all provided modules are valid Jido.Action implementations by checking that they:
- Are valid Elixir modules that can be loaded
- Export the required action_metadata/0 function that indicates Jido.Action behavior
Parameters
actions
: A list of module atoms or a single module atom to validate
Returns
{:ok, actions}
if all modules are valid Jido.Action implementations{:error, reason}
if any module is invalid
Examples
iex> defmodule ValidAction do
...> use Jido.Action,
...> name: "valid_action"
...> end
...> Jido.Util.validate_actions([ValidAction])
{:ok, [ValidAction]}
iex> Jido.Util.validate_actions([InvalidModule])
{:error, "All actions must implement the Jido.Action behavior"}
# Single module validation
iex> Jido.Util.validate_actions(ValidAction)
{:ok, [ValidAction]}
@spec validate_name(any()) :: {:ok, String.t()} | {:error, Jido.Error.t()}
Validates the name of a Action.
The name must contain only letters, numbers, and underscores.
Parameters
name
: The name to validate.
Returns
{:ok, name}
if the name is valid.{:error, reason}
if the name is invalid.
Examples
iex> Jido.Action.validate_name("valid_name_123")
{:ok, "valid_name_123"}
iex> Jido.Action.validate_name("invalid-name")
{:error, "The name must contain only letters, numbers, and underscores."}
@spec validate_runner(module()) :: {:ok, module()} | {:error, Jido.Error.t()}
Validates that a module implements the Jido.Runner behavior.
This function ensures that the provided module is a valid Jido.Runner implementation by checking that it:
- Is a valid Elixir module that can be loaded
- Exports the required run/2 function that indicates Jido.Runner behavior
Parameters
module
: The module atom to validate
Returns
{:ok, module}
if the module is a valid Jido.Runner implementation{:error, Jido.Error.t()}
if the module is invalid
Examples
iex> defmodule ValidRunner do
...> @behaviour Jido.Runner
...> def run(agent, opts), do: {:ok, agent}
...> end
...> Jido.Util.validate_runner(ValidRunner)
{:ok, ValidRunner}
iex> Jido.Util.validate_runner(InvalidModule)
{:error, %Jido.Error{type: :validation_error, message: "Module InvalidModule must implement run/2"}}