Spectre.Server.Contracts (Spectre v0.4.0)

Copy Markdown View Source

Optional application contracts expressed as ordinary Server callbacks.

assigns_schema/0 returns nil or a field map. Fields are required unless their type is {:optional, type}; undeclared assigns remain permitted. Types are :any, :string, :integer, :number, :boolean, :atom, :map, :list, {:enum, values}, {:list, type}, {:map, fields}, {:tuple, types} and {:one_of, types} for alternative result shapes.

continuation_contracts/0 returns nil or a map from explicit on_result tags to these same schemas. Use tuple and enum types to describe tagged results. A declared empty map rejects all explicit continuation tags. Built-in implicit callbacks remain available. These checks do not prove arbitrary Elixir handler exhaustiveness or infer contracts from catch-all clauses.

Summary

Functions

Checks application assigns without returning values in diagnostics.

Validates contract declarations before starting application work.

Checks retained explicit continuations before recovery or a hot upgrade.

Types

schema()

@type schema() ::
  atom()
  | {:optional, schema()}
  | {:enum, [term()]}
  | {:list, schema()}
  | {:map, map()}
  | {:tuple, [schema()]}
  | {:one_of, [schema()]}

Functions

validate_assigns(server, assigns)

@spec validate_assigns(module(), map()) :: :ok | {:error, map()}

Checks application assigns without returning values in diagnostics.

validate_configuration!(server)

@spec validate_configuration!(module()) :: :ok

Validates contract declarations before starting application work.

validate_retained(server, retained)

@spec validate_retained(module(), map()) :: :ok | {:error, map()}

Checks retained explicit continuations before recovery or a hot upgrade.