Mentor.Schema behaviour (mentor v0.2.2)

View Source

Defines the Mentor.Schema behaviour for integrating various data structures with the Mentor framework. This behaviour allows for the creation of schema adapters, enabling Mentor to work seamlessly with different data representations such as Ecto schemas, raw structs, and maps.

Modules implementing this behaviour are required to define the following callbacks:

  • definition/1 - Specifies the schema definition, detailing the fields and their types.
  • validate/2 - Validates the provided data against the schema, returning the validated structure or an error term.

Additionally, this module provides functions to parse and validate field documentation, ensuring that all fields are properly documented.

Summary

Functions

Raises a compile-time error indicating which fields are missing documentation in the @moduledoc attribute or the llm_description/0 callback.

Parses the given documentation string to ensure that all fields in the schema are properly documented. If any fields are missing documentation, a compile-time error is raised.

Types

field()

@type field() :: {name :: atom(), type :: atom()}

source()

@type source() :: module() | map()

t()

@type t() :: struct() | map() | module()

Callbacks

definition(source)

@callback definition(source()) :: [field()]

validate(source, data)

@callback validate(source(), data :: map()) :: {:ok, t()} | {:error, term()}

Functions

missing_documentation!(env, fields)

Raises a compile-time error indicating which fields are missing documentation in the @moduledoc attribute or the llm_description/0 callback.

Parameters

  • env - The current macro environment.
  • fields - A list of fields missing documentation.

Examples

iex> Mentor.Schema.missing_documentation!(__ENV__, [:name, :age])
** (CompileError) The following fields are missing documentation either in `@moduledoc` attribute or `llm_description/0` callback: [:name, :age]

parse_llm_description!(doc, schema, env)

@spec parse_llm_description!(binary(), [atom()], Macro.Env.t()) :: :ok

Parses the given documentation string to ensure that all fields in the schema are properly documented. If any fields are missing documentation, a compile-time error is raised.

Parameters

  • doc - The documentation string to parse.
  • schema - A list of fields expected in the schema.
  • env - The current macro environment.

Examples

iex> Mentor.Schema.parse_llm_description!(doc_string, [:name, :age], __ENV__)
:ok