AshOaskit.FilterBuilder (AshOasKit v0.2.1)

View Source

Builds OpenAPI filter schemas for Ash resources.

This module generates proper filter parameter schemas that describe the available filter operators for each resource attribute, matching the functionality provided by AshJsonApi.OpenApi.

Overview

JSON:API filtering uses the filter query parameter with a nested object structure. For example:

GET /posts?filter[status]=published&filter[title][contains]=hello

This module generates OpenAPI schemas that describe this structure.

Filter Schema Structure

The generated filter schema is a deepObject style query parameter:

%{
  name: "filter",
  in: :query,
  style: :deepObject,
  explode: true,
  schema: %{
    type: :object,
    properties: %{
      "title" => %{...},
      "status" => %{...},
      "and" => %{...},
      "or" => %{...},
      "not" => %{...}
    }
  }
}

Supported Filter Operators

Equality Operators

  • Direct value: filter[field]=value
  • eq: filter[field][eq]=value
  • ne: filter[field][ne]=value

Comparison Operators

  • gt: Greater than
  • gte: Greater than or equal
  • lt: Less than
  • lte: Less than or equal

Set Operators

  • in: filter[field][in][]=value1&filter[field][in][]=value2
  • not_in: Negation of in

String Operators

  • contains: Substring match
  • starts_with: Prefix match
  • ends_with: Suffix match
  • icontains: Case-insensitive contains
  • istarts_with: Case-insensitive starts_with
  • iends_with: Case-insensitive ends_with

Null Check

  • is_nil: filter[field][is_nil]=true

Boolean Operators

  • and: Array of filter conditions (all must match)
  • or: Array of filter conditions (any must match)
  • not: Single filter condition (must not match)

Integration with Generators

This module is used by the V30 and V31 generators to create filter parameter schemas:

filter_param = FilterBuilder.build_filter_parameter(resource)

Configuration

Filtering can be disabled per-resource via AshJsonApi DSL:

json_api do
  derive_filter? false
end

This module respects that configuration when available.

Summary

Types

Filter operator specification.

Functions

Builds the filter schema for a single attribute.

Builds filter properties for all filterable attributes.

Builds the complete filter query parameter for a resource.

Builds the filter schema for a resource.

Types

operator_schema()

@type operator_schema() :: map()

Filter operator specification.

Contains the JSON Schema for a single filter operator.

Functions

build_attribute_filter_schema(attr)

@spec build_attribute_filter_schema(map()) :: map()

Builds the filter schema for a single attribute.

The schema allows either a direct value or an object with operators.

Parameters

  • attr - The attribute struct

Returns

A JSON Schema for the attribute's filter.

build_attribute_filters(resource)

@spec build_attribute_filters(module()) :: map()

Builds filter properties for all filterable attributes.

Parameters

  • resource - The Ash resource module

Returns

A map of attribute name to filter schema.

build_filter_parameter(resource, opts \\ [])

@spec build_filter_parameter(
  module(),
  keyword()
) :: map() | nil

Builds the complete filter query parameter for a resource.

Parameters

  • resource - The Ash resource module
  • opts - Options (currently unused, reserved for future use)

Returns

A map representing the OpenAPI parameter object for the filter query parameter, or nil if filtering is disabled for the resource.

Examples

iex> param = AshOaskit.FilterBuilder.build_filter_parameter(MyApp.Post)
...> param.name
"filter"
iex> param.in
:query

build_filter_schema(resource, _ \\ [])

@spec build_filter_schema(
  module(),
  keyword()
) :: map()

Builds the filter schema for a resource.

This returns just the schema object without the parameter wrapper, useful for testing or custom parameter construction.

Parameters

  • resource - The Ash resource module
  • opts - Options (currently unused)

Returns

A map representing the JSON Schema for the filter object.