Typed message schema contracts with version negotiation and backward-compatibility enforcement.
Sub-modules:
PhoenixMicro.Schema.Field— field struct & type validationPhoenixMicro.Schema.Validator— validation enginePhoenixMicro.Schema.Migrator— version migration chainsPhoenixMicro.Schema.Registry— ETS topic→module registry
Defining a schema
defmodule MyApp.Events.PaymentCreated do
use PhoenixMicro.Schema
schema_version 2
topic "payments.created"
field :payment_id, :string, required: true
field :amount_cents, :integer, required: true
field :currency, :string, required: true, default: "USD"
field :metadata, :map, required: false
compatible_with [1]
def migrate(1, payload) do
{amount, rest} = Map.pop(payload, "amount")
Map.put(rest, "amount_cents", round(amount * 100))
end
endPublishing with schema validation
PhoenixMicro.Schema.publish(MyApp.Events.PaymentCreated, %{
payment_id: "pay_123",
amount_cents: 9999,
currency: "USD"
})Decoding in consumers
def handle(message, _ctx) do
{:ok, event} =
PhoenixMicro.Schema.decode(MyApp.Events.PaymentCreated,
message.payload, message.headers)
process(event)
:ok
end
Summary
Callbacks
Migrates a payload from an older version to the current version.
Validates a raw map against the schema.
Functions
Returns all registered schema modules.
Decodes and validates a raw payload against a schema, migrating if needed.
Validates and publishes a message with schema metadata in headers.
Returns {:error, {:schema_validation_failed, errors}} if validation fails.
Returns the schema module registered for a given topic.
Types
@type field_def() :: {atom(), field_type(), field_opts()}
@type field_type() ::
:string | :integer | :float | :boolean | :map | :list | :atom | :any
Callbacks
@callback compatible_versions() :: [pos_integer()]
@callback fields() :: [field_def()]
@callback migrate(from_version :: pos_integer(), payload :: map()) :: map()
Migrates a payload from an older version to the current version.
@callback schema_version() :: pos_integer()
@callback topic() :: String.t()
@callback validate(map()) :: {:ok, map()} | {:error, [validation_error()]}
Validates a raw map against the schema.
Functions
@spec all_schemas() :: [module()]
Returns all registered schema modules.
Decodes and validates a raw payload against a schema, migrating if needed.
Pass the message headers so the migrator can detect older payload versions.
Returns {:ok, validated_payload} or {:error, reason}.
Validates and publishes a message with schema metadata in headers.
Returns {:error, {:schema_validation_failed, errors}} if validation fails.
Returns the schema module registered for a given topic.