View Source Backstage.Command protocol (backstage v0.0.2)

A mechanism for ensuring consistent validation and dispatching of command structs.

Summary

Types

t()

All the types that implement this protocol.

Functions

Performs data validation before building a command struct and dispatching to the router.

Supports testing with example data to support unit, property, and other tests. A factory implementation also provides a layer of documentation for other developers.

Performs data validation before building a command without dispatching to the router.

Types

@type t() :: term()

All the types that implement this protocol.

Functions

@spec dispatch(params :: map()) :: term()

Performs data validation before building a command struct and dispatching to the router.

@spec factory(params :: Keyword.t()) :: struct()

Supports testing with example data to support unit, property, and other tests. A factory implementation also provides a layer of documentation for other developers.

These factories may be implemented alongside the command struct code or in the :test environment depending on your team's preferences.

@spec validate(params :: map()) :: {:ok, struct()} | {:error, term()}

Performs data validation before building a command without dispatching to the router.

This is useful for providing real-time feedback to users on web interfaces, for example. Ecto.Changeset is useful for performing these validations.

What to Validate

In traditional CRUD applications, an Ecto Changeset sometimes performs database checks for uniqueness and may even include some business requirements. When validating a command, it's best to cast data to expected types and leave business rule checks to the Backstage.Aggregate.