Crucible.Stage.Schema (CrucibleFramework v0.5.2)

View Source

Canonical schema definition for stage describe/1 callback.

This module defines the structure and validation rules for stage schemas. All stages implementing the Crucible.Stage behaviour must return a schema conforming to this specification from their describe/1 callback.

Schema Structure

A valid schema must contain:

  • :name - Stage identifier (atom, required)
  • :description - Human-readable description (non-empty string, required)
  • :required - List of required option keys (list of atoms, required)
  • :optional - List of optional option keys (list of atoms, required)
  • :types - Map of option keys to type specifications (map, required)

Optional fields:

  • :__schema_version__ - Schema version (semver string, e.g., "1.0.0")
  • :defaults - Default values for optional fields (map)
  • :version - Stage version (semver string)
  • :__extensions__ - Domain-specific metadata (map)

Type Specifications

Supported type specs:

  • Primitives: :string, :integer, :float, :boolean, :atom, :map, :list, :module, :any
  • Struct: {:struct, Module}
  • Enum: {:enum, [values]}
  • Typed list: {:list, inner_type}
  • Typed map: {:map, key_type, value_type}
  • Function: {:function, arity}
  • Union: {:union, [types]}
  • Tuple: {:tuple, [types]}

Example

%{
  __schema_version__: "1.0.0",
  name: :my_stage,
  description: "Processes data according to configuration",
  required: [:input_path],
  optional: [:format, :batch_size],
  types: %{
    input_path: :string,
    format: {:enum, [:json, :csv]},
    batch_size: :integer
  },
  defaults: %{
    format: :json,
    batch_size: 100
  }
}

Summary

Types

t()

Canonical schema format for stage describe/1 callback.

Type specification for stage options.

Functions

Checks if a type specification is valid.

Validates a schema map for conformance to the canonical format.

Types

t()

@type t() :: %{
  :name => atom(),
  :description => String.t(),
  :required => [atom()],
  :optional => [atom()],
  :types => %{optional(atom()) => type_spec()},
  optional(:__schema_version__) => String.t(),
  optional(:defaults) => %{optional(atom()) => term()},
  optional(:version) => String.t(),
  optional(:__extensions__) => map()
}

Canonical schema format for stage describe/1 callback.

type_spec()

@type type_spec() ::
  :string
  | :integer
  | :float
  | :boolean
  | :atom
  | :map
  | :list
  | :module
  | :any
  | {:struct, module()}
  | {:enum, [term()]}
  | {:list, type_spec()}
  | {:map, type_spec(), type_spec()}
  | {:function, non_neg_integer()}
  | {:union, [type_spec()]}
  | {:tuple, [type_spec()]}

Type specification for stage options.

Functions

valid_type_spec?(spec)

@spec valid_type_spec?(term()) :: boolean()

Checks if a type specification is valid.

Examples

iex> Crucible.Stage.Schema.valid_type_spec?(:string)
true

iex> Crucible.Stage.Schema.valid_type_spec?({:enum, [:a, :b]})
true

iex> Crucible.Stage.Schema.valid_type_spec?(:invalid)
false

validate(schema)

@spec validate(map()) :: :ok | {:error, [String.t()]}

Validates a schema map for conformance to the canonical format.

Returns :ok if the schema is valid, or {:error, [String.t()]} with a list of error messages if validation fails.

Examples

iex> Crucible.Stage.Schema.validate(%{
...>   name: :test,
...>   description: "Test stage",
...>   required: [],
...>   optional: [],
...>   types: %{}
...> })
:ok

iex> Crucible.Stage.Schema.validate(%{description: "Missing name"})
{:error, ["name is required and must be an atom"]}