Corex.Value (Corex v0.2.0)

View Source

Coercions for the values that reach a component from outside the library.

Imperative API calls carry values that often originate in handle_event params, so a bad value is bad user input as often as it is a developer mistake. Every function here coerces and warns rather than raising: dropping the value keeps the LiveView alive, where raising would take the whole process down mid-render.

Summary

Types

A value an imperative API accepts without narrowing it first.

Functions

Coerces a value to a list of strings silently, for internal callers that already reported the problem or have no context to report it with.

Coerces a value to a list of strings, warning and returning [] otherwise.

Coerces a single-selection value to a string or nil, warning on anything else.

Parses an imperative API value that may arrive as a list or a delimited string.

Types

coercible()

@type coercible() :: term()

A value an imperative API accepts without narrowing it first.

Any term, because the guard is deliberately absent: these functions coerce and warn, so a spec that named only the useful shapes would claim a check the function does not perform.

Functions

coerce_string_list(value)

@spec coerce_string_list(term()) :: [String.t()]

Coerces a value to a list of strings silently, for internal callers that already reported the problem or have no context to report it with.

coerce_string_list(value, context)

@spec coerce_string_list(term(), String.t()) :: [String.t()]

Coerces a value to a list of strings, warning and returning [] otherwise.

coerce_string_value(value, context)

@spec coerce_string_value(term(), String.t()) :: String.t() | nil

Coerces a single-selection value to a string or nil, warning on anything else.

parse_string_list(value, context, opts \\ [])

@spec parse_string_list(term(), String.t(), keyword()) :: [String.t()]

Parses an imperative API value that may arrive as a list or a delimited string.

With graphemes: true a string without commas is split per grapheme, which is what pin input expects. Anything unparseable is coerced to [] with a warning.

value_error(value)

@spec value_error(term()) :: String.t()