View Source Mentor.Ecto.Schema behaviour (mentor v0.1.1)
Provides functionality to integrate Ecto schemas with the Mentor framework, ensuring that schemas include comprehensive field documentation.
This module defines a behaviour that requires implementing a changeset/2
function and utilizes compile-time hooks to verify that all fields in the schema are documented in the module's @moduledoc
.
Usage
To use Mentor.Ecto.Schema
in your Ecto schema module:
defmodule MyApp.Schema do
use Ecto.Schema
use Mentor.Ecto.Schema
import Ecto.Changeset
@primary_key false
embedded_schema do
field :name, :string
field :age, :integer
end
@impl true
def changeset(%__MODULE__{} = source, %{} = attrs) do
source
|> cast(attrs, [:name, :age])
|> validate_required([:name, :age])
|> validate_number(:age, less_than: 100, greater_than: 0)
end
end
Ensure that your module's @moduledoc
includes a "Fields" section documenting each field:
@moduledoc """
Schema representing a person.
## Fields
- `name`: The name of the person.
- `age`: The age of the person.
"""
Custom LLM description
If you don't wanna or can't rely on @moduledoc
to descrive the LLM prompt for your schema, you can alternatively provide a llm_description/0
callback into you schema module that returns a string that represents the prompt it self, like:
@impl true
def llm_description do
"""
## Fields
- `name`: it should be a valid string name for humans
- `age`: it should be a reasonable age number for a human being
"""
end
Summary
Functions
Validates the given data against the specified schema by applying the schema's changeset/2
function.
Callbacks
@callback changeset(Ecto.Schema.t(), map()) :: Ecto.Changeset.t()
@callback llm_description() :: String.t()
Functions
Validates the given data against the specified schema by applying the schema's changeset/2
function.
Parameters
schema
: The schema module implementing theMentor.Ecto.Schema
behaviour.data
: A map containing the data to be validated.
Returns
{:ok, struct}
: If the data is valid and conforms to the schema.{:error, changeset}
: If the data is invalid, returns the changeset with errors.
Examples
iex> data = %{"name" => "Alice", "age" => 30}
iex> Mentor.Ecto.Schema.validate(MyApp.Schema, data)
{:ok, %MyApp.Schema{name: "Alice", age: 30}}
iex> invalid_data = %{"name" => "Alice", "age" => 150}
iex> Mentor.Ecto.Schema.validate(MyApp.Schema, invalid_data)
{:error, %Ecto.Changeset{errors: [age: {"must be less than 100", [validation: :number, less_than: 100]}]}}