Jido.Action.Schema (Jido Action v2.0.0-rc.0)

View Source

Unified schema validation interface supporting both NimbleOptions and Zoi.

This adapter provides a consistent API for:

  • Schema validation
  • Key introspection
  • Error formatting
  • JSON Schema generation (for AI tools)

Summary

Functions

Formats validation errors into Jido.Action.Error structs.

Extracts all known keys from a schema.

Detects the type of schema.

Converts a schema to JSON Schema format for AI tools.

Validates data against a schema.

Validates a schema value for use in configuration.

Types

t()

@type t() :: NimbleOptions.schema() | struct() | []

Functions

format_error(error, context, module)

@spec format_error(term(), String.t(), module()) ::
  Jido.Action.Error.InvalidInputError.t()

Formats validation errors into Jido.Action.Error structs.

Parameters

  • error - The error from validation (NimbleOptions.ValidationError, Zoi.Error, or list)
  • context - Context string describing where the error occurred
  • module - The module where the error occurred

Returns

  • Jido.Action.Error.InvalidInputError.t() - Formatted error struct

known_keys(schema)

@spec known_keys(t()) :: [atom()]

Extracts all known keys from a schema.

Parameters

  • schema - NimbleOptions schema or Zoi schema

Returns

  • List of atom keys defined in the schema

schema_type(schema)

@spec schema_type(t()) :: :nimble | :zoi | :empty | :unknown

Detects the type of schema.

Returns :nimble for NimbleOptions keyword list schemas, :zoi for Zoi schemas, :empty for empty lists, or :unknown for unsupported types.

to_json_schema(schema)

@spec to_json_schema(t()) :: map()

Converts a schema to JSON Schema format for AI tools.

For NimbleOptions schemas, converts to OpenAI-compatible JSON Schema. For Zoi schemas, uses Zoi's built-in JSON Schema conversion.

Parameters

  • schema - NimbleOptions schema or Zoi schema

Returns

  • Map representing the JSON Schema

validate(schema, data)

@spec validate(t(), map() | keyword()) :: {:ok, map()} | {:error, term()}

Validates data against a schema.

For NimbleOptions schemas, returns {:ok, map()} with validated data as a map. For Zoi schemas, returns {:ok, struct()} with the validated struct. For empty schemas, returns the data unchanged.

Parameters

  • schema - NimbleOptions schema (keyword list) or Zoi schema
  • data - Data to validate (map or keyword list)

Returns

  • {:ok, validated_data} - Validation succeeded
  • {:error, error} - Validation failed

validate_config_schema(value, opts \\ [])

@spec validate_config_schema(
  term(),
  keyword()
) :: :ok | {:error, String.t()}

Validates a schema value for use in configuration.

Used during compilation to ensure schema configuration is valid.

Parameters

  • value - The schema value to validate
  • _opts - Options (unused, for Zoi refine compatibility)

Returns

  • :ok - Schema is valid
  • {:error, message} - Schema is invalid