LiveFlow.Validation.Connection (LiveFlow v0.4.0)

Copy Markdown View Source

Helper to validate and create edges from lf:connect_end event params.

Combines validation + edge creation in a single call, reducing boilerplate in parent LiveView handle_event clauses.

Example

def handle_event("lf:connect_end", params, socket) do
  case LiveFlow.Validation.Connection.validate_and_create(socket.assigns.flow, params) do
    {:ok, edge} ->
      flow = State.add_edge(socket.assigns.flow, edge)
      {:noreply, assign(socket, flow: flow)}

    {:error, _reason} ->
      {:noreply, socket}
  end
end

Custom validators

validate_and_create(flow, params,
  validators: [
    &Validation.no_duplicate_edges/2,
    &Validation.no_cycles/2,
    fn flow, params -> Validation.max_connections(flow, params, max: 1) end
  ],
  edge_opts: [animated: true]
)

Summary

Functions

Validates connection params and creates an edge if valid.

Functions

validate_and_create(flow, params, opts \\ [])

@spec validate_and_create(LiveFlow.State.t(), map(), keyword()) ::
  {:ok, LiveFlow.Edge.t()} | {:error, String.t()}

Validates connection params and creates an edge if valid.

Returns {:ok, edge} or {:error, reason}.

Options

  • :validators — list of validator functions (default: Validation.preset(:default))
  • :edge_opts — extra options passed to Edge.new/4