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]=helloThis 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]=valuene:filter[field][ne]=value
Comparison Operators
gt: Greater thangte: Greater than or equallt: Less thanlte: Less than or equal
Set Operators
in:filter[field][in][]=value1&filter[field][in][]=value2not_in: Negation ofin
String Operators
contains: Substring matchstarts_with: Prefix matchends_with: Suffix matchicontains: Case-insensitive containsistarts_with: Case-insensitive starts_withiends_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
endThis 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
@type operator_schema() :: map()
Filter operator specification.
Contains the JSON Schema for a single filter operator.
Functions
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.
Builds filter properties for all filterable attributes.
Parameters
resource- The Ash resource module
Returns
A map of attribute name to filter schema.
Builds the complete filter query parameter for a resource.
Parameters
resource- The Ash resource moduleopts- 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
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 moduleopts- Options (currently unused)
Returns
A map representing the JSON Schema for the filter object.