fix(profile): save every valid field even when a sibling is invalid

A profile PUT/PATCH validated as one safeParse and wrote as one
transaction, so a single rejected field took every sibling change down
with it - editing height and gender together lost both, and onboarding
lost a whole baseline step to one field. The schema has no cross-field
dependency, so applyProfileUpdate now re-validates each present field
against its own schema and writes whatever validates; only a body where
nothing validates is refused outright, naming the field that blocked it.

The top-level error string also stopped being a raw Zod message
(issues[0].message) that could read "Invalid option: expected one of
"MALE"|"FEMALE"|"OTHER"" on a person's screen. It's now a person-safe
sentence plus a stable errorCode for the web client to localize; the
full per-field detail is relocated (not deleted) into details.issues,
matching the multi-issue envelope the iOS contract already expects from
other write routes.
This commit is contained in:
Marc-André Bombeck committed 2026-07-30 23:25:18 +02:00
1 parent bed634fd67
commit e49bd866dd
5 files changed
+289 -16

No files matched your search

+26
View File
@@ -175,6 +175,32 @@ export function returnAllZodIssues(
);
}
/**
* Like {@link returnAllZodIssues}, but for callers that have already
* reduced a `ZodError` to a caller-composed, person-safe top-level
* `error` sentence and a pre-sanitised issue list (e.g. a shared
* write-path helper that validates outside the route handler, such as
* `applyProfileUpdate`). The multi-issue envelope (`details.issues`) is
* unchanged from `returnAllZodIssues` — this exists so the top-level
* string never has to repeat a raw Zod default message (an enum's
* `Invalid option: expected one of "A"|"B"|"C"` reaching a person's
* screen) while the machine-readable detail is still relocated, not
* dropped. `meta.errorCode` lets the web client resolve a localized
* sentence via `apiErrors.<errorCode>` (see `localizedApiError`).
*/
export function apiValidationError(
message: string,
issues: SanitisedZodIssue[],
status: number = 422,
meta?: ErrorMeta,
): NextResponse {
return buildJsonErrorResponse(
{ data: null, error: message, details: { issues } },
status,
meta,
);
}
/**
* `meta` is additive — clients that ignore it see the unchanged
* `{ data: null, error: <string> }` envelope. New callers can pass