Scrutinex.Result (Scrutinex v0.2.0)

Copy Markdown View Source

Validation result, inspired by Ecto changesets.

Contains the validated (and possibly coerced) data alongside any errors. valid? is true when there are no :error-severity errors (warnings alone do not cause valid? to be false).

Summary

Functions

Filters errors by column name or row index.

Returns only errors with severity :error (excludes warnings).

Groups errors by column name with interpolated messages.

Returns only errors with severity :warning.

Types

t()

@type t() :: %Scrutinex.Result{
  data: [map()],
  errors: [Scrutinex.Error.t()],
  valid?: boolean()
}

Functions

errors_for(result, filter)

@spec errors_for(
  t(),
  String.t()
  | [row: non_neg_integer(), check: atom(), severity: :error | :warning]
) :: [Scrutinex.Error.t()]

Filters errors by column name or row index.

Examples

Scrutinex.Result.errors_for(result, "age")
#=> [%Scrutinex.Error{column: "age", ...}]

Scrutinex.Result.errors_for(result, row: 0)
#=> [%Scrutinex.Error{row: 0, ...}]

Scrutinex.Result.errors_for(result, check: :number)
#=> [%Scrutinex.Error{check: :number, ...}]

errors_only(result)

@spec errors_only(t()) :: [Scrutinex.Error.t()]

Returns only errors with severity :error (excludes warnings).

Examples

Scrutinex.Result.errors_only(result)
#=> [%Scrutinex.Error{severity: :error, ...}]

errors_to_map(result)

@spec errors_to_map(t()) :: %{optional(String.t() | nil) => [String.t()]}

Groups errors by column name with interpolated messages.

Returns a map of column names to lists of formatted error messages. Cross-column errors (with column: nil) are grouped under nil.

Examples

Scrutinex.Result.errors_to_map(result)
#=> %{"age" => ["must be greater than 0"], "name" => ["is required"]}

warnings(result)

@spec warnings(t()) :: [Scrutinex.Error.t()]

Returns only errors with severity :warning.

Examples

Scrutinex.Result.warnings(result)
#=> [%Scrutinex.Error{severity: :warning, ...}]