Module: Ruact::ServerFunctions::ValidationErrors

Defined in:
lib/ruact/server_functions/validation_errors.rb

Overview

Story 13.3 (FR98) — pure normalizer for the Inertia-style validation errors round-trip. Turns whatever a host action has on hand after a save attempt — an ActiveModel-ish record (responds to #errors), a raw ActiveModel::Errors, or a pre-shaped Hash — into ONE canonical wire shape — e.g. "title" => ["Title can't be blank"] and "base" => ["is invalid overall"] collected into one Hash{String=>Array<String>} — attribute names as strings (a base-level error keys under "base"), values arrays of human-readable full messages. A valid record (empty #errors) yields {}, never nil/undefined, so a single client code path handles both success and failure (the "always present" invariant of AC1).

Pure: no Rails constant references at load, no Ruact.config / request reads — duck-typing only (mirrors the caller/builder split of ErrorPayload / BucketTwoPayload). The caller (Ruact::Server#ruact_errors) owns the per-request collector and the bucket/flash wiring; this module only answers "what is the canonical shape of this source?".

Class Method Summary collapse

Class Method Details

.normalize(source) ⇒ Hash{String=>Array<String>}

Normalize a validation-error source into the canonical Hash shape.

Accepts, in precedence order:

- `nil` → `{}`
- a Hash already shaped `{attr => [msgs]}` → keys coerced to String,
scalar values wrapped in a one-element Array, every message
coerced to String; idempotent on already-canonical input.
- an object responding to `#group_by_attribute` (a raw
`ActiveModel::Errors`) → grouped into `{attr => [full_message]}`.
- an object responding to `#errors` (an ActiveModel-ish record) →
normalized from its `#errors`.
- anything else → `{}` (no errors discoverable).

Parameters:

  • source (nil, Hash, #group_by_attribute, #errors)

    the error source

Returns:

  • (Hash{String=>Array<String>})

    the canonical, always-present shape



39
40
41
42
43
44
45
46
# File 'lib/ruact/server_functions/validation_errors.rb', line 39

def normalize(source)
  return {} if source.nil?
  return normalize_hash(source) if source.is_a?(Hash)
  return group_errors(source) if source.respond_to?(:group_by_attribute)
  return normalize(source.errors) if source.respond_to?(:errors)

  {}
end