refactor: extract toWizardErrors, adopted by all 3 wizards (RD-20)

Each wizard flattened its per-field error record into the shell's
WizardError[] summary with its own copy of the same loop. Extract one
pure helper, wizard-errors.ts, next to naarStapLabel. Add a spec that
covers a flat record, an empty record, skipped undefined/empty-string
values, the idPrefix, and a skipped nested object.

registratie-wizard.machine.ts changes Errors from an interface to a
type alias, because only a type alias gets an implicit index
signature and is assignable to the helper's Record<string, unknown>
parameter. The other two machines already declare their error maps as
type aliases, so this also makes the three consistent.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
eho
2026-09-04 22:00:04 +02:00
co-authored by Claude Sonnet 5
parent a196a380ce
commit 831940f1b9
9 changed files with 239 additions and 24 deletions
+10 -1
View File
@@ -20,7 +20,7 @@ tested where._
Every bullet below is a real test name from the suite — an `it()` title (frontend) or a test
method name (backend), read as a sentence. Nothing here is hand-written prose: this page
**is** the suite, reshaped for a business reader. 532 frontend behaviours across
**is** the suite, reshaped for a business reader. 538 frontend behaviours across
9 contexts; 261 backend behaviours across 42 test
classes.
@@ -973,6 +973,15 @@ classes.
- unwraps a Success value
- is the fallback for every other state
#### toWizardErrors
- flattens a flat record of field errors
- returns an empty list for an empty record
- skips a value that is undefined
- skips a value that is an empty string
- prefixes every id with idPrefix when given
- skips a value that is a nested object, so the caller can flatten it separately
#### upload lifecycle messages
- queued → progress → complete
@@ -0,0 +1,35 @@
import { describe, it, expect } from 'vitest';
import { toWizardErrors } from './wizard-errors';
describe('toWizardErrors', () => {
it('flattens a flat record of field errors', () => {
expect(toWizardErrors({ straat: 'Verplicht', postcode: 'Ongeldig' })).toEqual([
{ id: 'straat', message: 'Verplicht' },
{ id: 'postcode', message: 'Ongeldig' },
]);
});
it('returns an empty list for an empty record', () => {
expect(toWizardErrors({})).toEqual([]);
});
it('skips a value that is undefined', () => {
expect(toWizardErrors({ straat: undefined })).toEqual([]);
});
it('skips a value that is an empty string', () => {
expect(toWizardErrors({ straat: '' })).toEqual([]);
});
it('prefixes every id with idPrefix when given', () => {
expect(toWizardErrors({ q1: 'Verplicht' }, 'vraag-')).toEqual([
{ id: 'vraag-q1', message: 'Verplicht' },
]);
});
it('skips a value that is a nested object, so the caller can flatten it separately', () => {
expect(toWizardErrors({ straat: 'Verplicht', antwoorden: { q1: 'Verplicht' } })).toEqual([
{ id: 'straat', message: 'Verplicht' },
]);
});
});
@@ -0,0 +1,13 @@
import type { WizardError } from './wizard-shell.component';
/** Flatten a machine's per-field error record into the shell's summary list.
Values that are not a non-empty string are skipped, so a nested group
(the registratie wizard's `antwoorden`) is appended by the caller with its
own `idPrefix` rather than special-cased here. */
export function toWizardErrors(errors: Record<string, unknown>, idPrefix = ''): WizardError[] {
const out: WizardError[] = [];
for (const [k, v] of Object.entries(errors)) {
if (typeof v === 'string' && v) out.push({ id: idPrefix + k, message: v });
}
return out;
}