Jido.Tools.ZoiExample (Jido Action v2.0.0-rc.0)
View SourceProduction-quality example demonstrating Zoi schema features in Jido actions.
This action showcases best practices for using Zoi schemas:
- Email validation with transformations (trim, downcase)
- Password validation with refinements
- Enums for constrained choices
- Optional fields with defaults
- Nested object schemas
- Output validation
- Custom refinements for business logic
Example Usage
iex> params = %{
...> user: %{
...> email: " JOHN@EXAMPLE.COM ",
...> password: "SecurePass123!",
...> name: "John Doe"
...> },
...> priority: :high,
...> metadata: %{source: "web"}
...> }
iex> {:ok, result} = Jido.Tools.ZoiExample.run(params, %{})
iex> result.user.email
"john@example.com"
iex> result.status
:validatedZoi Features Demonstrated
Transformations
Zoi.trim/1- Remove leading/trailing whitespaceZoi.to_downcase/1- Convert to lowercase
Validators
Zoi.email/1- Email format validationZoi.min/2andZoi.max/2- Length/value constraintsZoi.regex/2- Pattern matching
Refinements
Zoi.refine/2- Custom validation logic
Type System
Zoi.object/1- Nested object schemasZoi.enum/1- Enumerated valuesZoi.optional/1- Optional fieldsZoi.default/2- Default values
Summary
Functions
Returns the Action metadata. Alias for to_json/0.
Returns the category of the Action.
Returns the description of the Action.
Returns the name of the Action.
Lifecycle hook called after Action execution.
Lifecycle hook called after output validation.
Lifecycle hook called after parameter validation.
Lifecycle hook called before output validation.
Lifecycle hook called before parameter validation.
Lifecycle hook called when an error occurs.
Returns the output schema of the Action.
Validates user registration data with comprehensive checks.
Returns the input schema of the Action.
Returns the tags associated with the Action.
Returns the Action metadata as a JSON-serializable map.
Converts the Action to an LLM-compatible tool format.
Validates the output result for the Action.
Validates the input parameters for the Action.
Returns the version of the Action.
Functions
Returns the Action metadata. Alias for to_json/0.
Returns the category of the Action.
Returns the description of the Action.
Returns the name of the Action.
Lifecycle hook called after Action execution.
Lifecycle hook called after output validation.
Lifecycle hook called after parameter validation.
Lifecycle hook called before output validation.
Lifecycle hook called before parameter validation.
Lifecycle hook called when an error occurs.
Returns the output schema of the Action.
Validates user registration data with comprehensive checks.
This action demonstrates how validated and transformed parameters flow through the action pipeline. The input params are automatically:
- Validated against schema constraints
- Transformed (trimmed, case-converted)
- Checked against custom refinements
Parameters
:user- User object with email, password, name, and optional age:priority- Registration priority (defaults to :normal):metadata- Optional metadata about the registration
Returns
{:ok, result}- Validated user data with status and timestamp{:error, reason}- Validation or processing errors
Returns the input schema of the Action.
Returns the tags associated with the Action.
Returns the Action metadata as a JSON-serializable map.
Converts the Action to an LLM-compatible tool format.
Validates the output result for the Action.
Examples
iex> defmodule ExampleAction do
...> use Jido.Action,
...> name: "example_action",
...> output_schema: [
...> result: [type: :string, required: true]
...> ]
...> end
...> ExampleAction.validate_output(%{result: "test", extra: "ignored"})
{:ok, %{result: "test", extra: "ignored"}}
iex> ExampleAction.validate_output(%{extra: "ignored"})
{:error, "Invalid output for Action: Required key :result not found"}
Validates the input parameters for the Action.
Examples
iex> defmodule ExampleAction do
...> use Jido.Action,
...> name: "example_action",
...> schema: [
...> input: [type: :string, required: true]
...> ]
...> end
...> ExampleAction.validate_params(%{input: "test"})
{:ok, %{input: "test"}}
iex> ExampleAction.validate_params(%{})
{:error, "Invalid parameters for Action: Required key :input not found"}
Returns the version of the Action.