Backpex.FilterValidation (Backpex v0.18.3)

Copy Markdown View Source

Builds and validates filter changesets from URL params.

This module provides the core validation infrastructure for Backpex filters, using Ecto changesets to validate filter values before they are applied to queries.

Summary

Functions

Builds a changeset from filter params and configurations.

Builds the types map for schemaless changeset from filter configurations.

Extracts valid filter values from a changeset.

Functions

build_changeset(params, filter_configs, assigns)

Builds a changeset from filter params and configurations.

Returns a changeset that can be used with Phoenix.Component.to_form/2. The changeset includes all filter values, with validation errors for invalid ones.

Parameters

  • params - Map of filter params from URL (e.g., %{"status" => "active", "date_range" => %{"start" => "2024-01-01"}})
  • filter_configs - Keyword list of filter configurations from the LiveResource
  • assigns - Socket assigns for context (used by filters that need runtime data)

Examples

iex> filter_configs = [status: %{module: MyFilters.StatusSelect}]
iex> params = %{"status" => "active"}
iex> changeset = Backpex.FilterValidation.build_changeset(params, filter_configs, %{})
iex> changeset.valid?
true

build_types(filter_configs, assigns)

Builds the types map for schemaless changeset from filter configurations.

Each filter module must implement type/1 to specify its Ecto type.

valid_values(changeset)

Extracts valid filter values from a changeset.

Returns a map containing only the filters that passed validation. Filters with errors are excluded from the result.

Parameters

Examples

iex> changeset = %Ecto.Changeset{changes: %{status: "active", count: 5}, errors: [count: {"is invalid", []}]}
iex> Backpex.FilterValidation.valid_values(changeset)
%{status: "active"}