Filtr.Helpers (filtr v1.0.1)

Copy Markdown View Source

Helper functions for Filtr schema processing and plugin management.

Summary

Functions

Returns the default error mode configured for the application.

Parses and structures parameter options into a standardized format.

Converts AST from nested param macro calls into a schema map.

Returns a map of type atoms to their corresponding plugin module.

Functions

default_error_mode()

@spec default_error_mode() :: atom()

Returns the default error mode configured for the application.

Reads from the application config key :error_mode under :filtr. Falls back to :fallback if not configured.

parse_param_opts(opts)

@spec parse_param_opts(keyword()) :: keyword()

Parses and structures parameter options into a standardized format.

Separates type and run options from validation options, grouping validators under a :validators key.

Run options: error_mode

render_ast_to_schema(ast, schema \\ %{})

@spec render_ast_to_schema(ast :: term(), schema :: map()) :: map()

Converts AST from nested param macro calls into a schema map.

This function recursively processes the AST generated by nested param blocks, transforming them into a structured schema map suitable for validation.

Examples

# Input AST from:
param :user do
  param :name, :string, required: true
  param :age, :integer, min: 18
end

# Output:
%{
  user: %{
    name: %{type: :string, validators: [required: true]},
     age: %{type: :integer, validators: [min: 18]}
  }
}

Only param macro calls are valid inside nested blocks. Other expressions will raise an ArgumentError.

supported_error_mode?(mode)

@spec supported_error_mode?(mode :: atom()) :: boolean()

supported_error_modes()

@spec supported_error_modes() :: [atom()]

type_plugin_map()

@spec type_plugin_map() :: %{required(atom()) => module()}

Returns a map of type atoms to their corresponding plugin module.

This function builds and caches a mapping of all supported types to the plugin that handle them. The map is cached in :persistent_term for fast access across the application.