Files
HealthLog/src/lib/export/onboarding-backup-schema.ts
T
Marc-André Bombeck 12749f6839 refactor(backup): give each domain its own slice of the payload schema
The element schemas move out of validations/backup.ts into one
<domain>-backup-schema.ts per domain, beside its export and restore, with
the shared primitives in backup-schema-kit.ts. validations/backup.ts now
composes the slices into backupPayloadSchema in the same key order, with
the same defaults and the checks that span sections, so the parsed
payload, every validation message and the OpenAPI contract are unchanged.

The lazy-schema guard now covers the slices too: a slice is reached only
through the payload schema or another slice, never from a domain's export
or restore. The external-id guard carries the payload's exemption to its
slices, and mutation testing scores the slices with the payload schema.
2026-10-22 08:02:14 +02:00

38 lines
1.6 KiB
TypeScript

/**
* The onboarding slice of the backup payload schema.
*
* Part of the onboarding backup domain, beside its export and restore.
* Loaded only through `src/lib/validations/backup.ts`, which composes every
* slice into `backupPayloadSchema` on first use (the schema is ~12 MB of
* heap; see `backup-schema-lazy-guard.test.ts`).
*/
import { z } from "zod/v4";
import { isoDateTime } from "@/lib/export/backup-schema-kit";
/**
* The needs-based setup state. One row per record.
*
* The three answer objects are validated loosely on purpose: the closed
* vocabularies live in `src/lib/onboarding/needs.ts`, and the restore reads
* every one of them back through the same fail-soft parsers the account
* payload uses. Duplicating the enums here would give the file a second,
* quietly divergent opinion about what an answer is, and the failure mode of
* being stricter here is refusing a whole account's restore over one retired
* chip.
*/
export const onboardingRecordBackupSchema = z
.object({
id: z.string().min(1).optional(),
needs: z.record(z.string(), z.unknown()).default({}),
steps: z.array(z.record(z.string(), z.unknown())).default([]),
firstResult: z.record(z.string(), z.unknown()).nullable().default(null),
// The once-only latch on the module derivation. Carried rather than
// defaulted: a null here would hand the next confirm permission to
// re-apply the questionnaire over every module decision made since.
modulesDerivedAt: isoDateTime.nullable().default(null),
completedAt: isoDateTime.nullable().default(null),
createdAt: isoDateTime.optional(),
updatedAt: isoDateTime.optional(),
})
.passthrough();