LLMDB.Validate (LLM DB v2025.12.4)

View Source

Validation functions for providers and models using Zoi schemas.

Provides functions to validate individual records or batches of records, handling errors gracefully and ensuring catalog viability.

Summary

Functions

Ensures that we have at least one provider and one model for a viable catalog.

Validates a single model map against the Model schema.

Validates a list of model maps, collecting valid ones and counting invalid.

Validates a single provider map against the Provider schema.

Validates a list of provider maps, collecting valid ones and counting invalid.

Types

validation_error()

@type validation_error() :: term()

Functions

ensure_viable(providers, models)

@spec ensure_viable([LLMDB.Provider.t()], [LLMDB.Model.t()]) ::
  :ok | {:error, :empty_catalog}

Ensures that we have at least one provider and one model for a viable catalog.

Returns :ok if both lists are non-empty, otherwise returns an error.

Examples

iex> ensure_viable([%{id: :openai}], [%{id: "gpt-4o", provider: :openai}])
:ok

iex> ensure_viable([], [%{id: "gpt-4o", provider: :openai}])
{:error, :empty_catalog}

iex> ensure_viable([%{id: :openai}], [])
{:error, :empty_catalog}

validate_model(map)

@spec validate_model(map()) :: {:ok, LLMDB.Model.t()} | {:error, validation_error()}

Validates a single model map against the Model schema.

Examples

iex> validate_model(%{id: "gpt-4o", provider: :openai})
{:ok, %{id: "gpt-4o", provider: :openai, deprecated: false, aliases: []}}

iex> validate_model(%{id: "gpt-4o"})
{:error, _}

validate_models(maps)

@spec validate_models([map()]) :: {:ok, [LLMDB.Model.t()], non_neg_integer()}

Validates a list of model maps, collecting valid ones and counting invalid.

Returns all valid models and the count of invalid ones that were dropped.

Examples

iex> models = [
...>   %{id: "gpt-4o", provider: :openai},
...>   %{id: :invalid, provider: :openai},
...>   %{id: "claude-3", provider: :anthropic}
...> ]
iex> validate_models(models)
{:ok, [%{id: "gpt-4o", ...}, %{id: "claude-3", ...}], 1}

validate_provider(map)

@spec validate_provider(map()) ::
  {:ok, LLMDB.Provider.t()} | {:error, validation_error()}

Validates a single provider map against the Provider schema.

Examples

iex> validate_provider(%{id: :openai})
{:ok, %{id: :openai}}

iex> validate_provider(%{id: "openai"})
{:error, _}

validate_providers(maps)

@spec validate_providers([map()]) :: {:ok, [LLMDB.Provider.t()], non_neg_integer()}

Validates a list of provider maps, collecting valid ones and counting invalid.

Returns all valid providers and the count of invalid ones that were dropped.

Examples

iex> providers = [%{id: :openai}, %{id: "invalid"}, %{id: :anthropic}]
iex> validate_providers(providers)
{:ok, [%{id: :openai}, %{id: :anthropic}], 1}